0

I am learning how to use the tf.data.Dataset api. I am using the sample code provided by google for their coursera tensorflow class. Specifically I am working with the c_dataset.ipynb notebook here.

This notebook has a model.train routine which looks like this:

model.train(input_fn = get_train(), steps = 1000)

The get_train() routine eventually calls code which uses the tf.data.Dataset api with this snippet of code:

filenames_dataset = tf.data.Dataset.list_files(filename)
# read lines from text files 
# this results in a dataset of textlines from all files
textlines_dataset = filenames_dataset.flat_map(tf.data.TextLineDataset)
# Parse text lines as comma-separated values (CSV)
# this does the decoder function for each textline
dataset = textlines_dataset.map(decode_csv)

The comments do a pretty explanation of what happens. Later this routine will return like so:

    # return the features and label as a tensorflow node, these
    # will trigger file load operations progressively only when
    # needed.
    return dataset.make_one_shot_iterator().get_next()

Is there anyway to evaluate the result for one iteration? I tried to something like this but it fails.

# Try to read what its using from the cvs file.
one_batch_the_csv_file = get_train()

with tf.Session() as sess:
  result = sess.run(one_batch_the_csv_file)

print(one_batch_the_csv_file)

Per the suggestion from Ruben below, I added this

I moved on to the next set of labs in this class where they introduce tensorboard and I get some graphs but still no inputs or outputs. With that said, here is a more complete set of source.

# curious he did not do this
# I am guessing because the output is so verbose
tf.logging.set_verbosity(tf.logging.INFO)  # putting back in since, tf.train.LoggingTensorHook mentions it

def train_and_evaluate(output_dir, num_train_steps):

  # Added this while trying to get input vals from csv.
  # This gives an error about scafolding
  # summary_hook = tf.train.SummarySaverHook()
  # SAVE_EVERY_N_STEPS,
  #  summary_op=tf.summary.merge_all())



  # To convert a model to distributed train and evaluate do four things
  estimator = tf.estimator.DNNClassifier(                         # 1. Estimator
                          model_dir = output_dir,
                          feature_columns = feature_cols, 
                          hidden_units=[160, 80, 40, 20],
                          n_classes=2,
                          config=tf.estimator.RunConfig().replace(save_summary_steps=2)  # 2. run config
                                                                                          # ODD. he mentions we need a run config in the videos, but it was missing in the lab
                                                                                          # notebook.  Later I found the bug report which gave me this bit of code.
                                                                                          # I got a working TensorBoard when I changed this from save_summary_steps=10 to 2.
                          )# 


  # .. also need the trainspec to tell the estimator how to get training data
  train_spec = tf.estimator.TrainSpec(
                                    input_fn = read_dataset('./taxi-train.csv', mode = tf.estimator.ModeKeys.TRAIN), # make sure you use the dataset api
                                    max_steps = num_train_steps)
#                                    training_hook=[summary_hook])       # Added this while trying to get input vals from csv.

  # ... also need this
  # serving and training-time inputs are often very different
  exporter = tf.estimator.LatestExporter('exporter', serving_input_receiver_fn = serving_input_fn)




  # .. also need an EvalSpec which controls the evaluation and
  # the checkpointing of the model since they happen at the same time
  eval_spec = tf.estimator.EvalSpec(
                                    input_fn = read_dataset('./taxi-valid.csv', mode = tf.estimator.ModeKeys.EVAL), # make sure you use the dataset api
                                    steps=None, # evals on 100 batches
                                    start_delay_secs = 1, # start evaluating after N secoonds. orig was 1.  3 seemed to fail?
                                    throttle_secs = 10, # eval no more than every 10 secs. Can not be more frequent than the checkpoint config specified in the run config.
                                    exporters = exporter)  # how to export the model for production.

  tf.estimator.train_and_evaluate(
                                estimator,
                                train_spec,  # 3. Train Spec
                                eval_spec)   # 4. Eval Spec


OUTDIR = './model_trained'
shutil.rmtree(OUTDIR, ignore_errors = True) # start fresh each time
TensorBoard().start(OUTDIR)
# need to let this complete before running next cell 


# call the above routine
train_and_evaluate(OUTDIR, num_train_steps = 6000)  # originally 2000. 1000 after reset shows only projectors
netskink
  • 4,033
  • 2
  • 34
  • 46

1 Answers1

0

I do not know exactly what kind of information you want to extract. If you are interested in step N, as a general answer:

  1. If you want exactly the results, just run with model.train(input_fn = get_train(), steps = N).
  2. Check train module functions here for specific content in a determined step.

If you search for step you will find different classes:

  • CheckpointSaverHook: Saves checkpoints every N steps or seconds.
  • LoggingTensorHook: Prints the given tensors every N local steps, every N seconds, or at end.
  • ProfilerHook: Captures CPU/GPU profiling information every N steps or seconds.
  • SummarySaverHook: Saves summaries every N steps.
  • Etc. (there are more, just check what can be useful for you).
Rubén C.
  • 1,098
  • 6
  • 16