【问题标题】:How to examine the results of a tensorflow.data.Dataset based model.train input_fn如何检查基于 tensorflow.data.Dataset 的 model.train input_fn 的结果
【发布时间】:2018-08-03 23:29:07
【问题描述】:

我正在学习如何使用 tf.data.Dataset api。我正在使用谷歌为他们的 coursera tensorflow 类提供的示例代码。具体来说,我正在使用 c_dataset.ipynb 笔记本here.

此笔记本有一个 model.train 例程,如下所示:

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

get_train() 例程最终调用使用 tf.data.Dataset api 的代码和这段代码的 sn-p:

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)

cmets 对发生的事情做了很好的解释。稍后这个例程会像这样返回:

    # 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()

是否可以评估一次迭代的结果?我尝试过这样的事情,但失败了。

# 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)

根据下面鲁本的建议,我添加了这个

我进入了这门课的下一组实验,在那里他们介绍了 tensorboard,我得到了一些图表,但仍然没有输入或输出。话虽如此,这里有一套更完整的源代码。

# 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

【问题讨论】:

    标签: tensorflow google-cloud-datalab


    【解决方案1】:

    我不知道你想提取什么样的信息。如果您对第 N 步感兴趣,作为一般答案:

    1. 如果您想要确切的结果,只需使用model.train(input_fn = get_train(), steps = N) 运行即可。
    2. 在确定的步骤中检查训练模块函数here 中的特定内容。

    如果你搜索 step 你会发现不同的类:

    • CheckpointSaverHook:每 N 步或秒保存一次检查点。
    • LoggingTensorHook:每 N 个局部步骤、每 N 秒或最后打印给定的张量。
    • ProfilerHook:每 N 步或秒捕获一次 CPU/GPU 分析信息。
    • SummarySaverHook:每 N 步保存摘要。
    • 等。 (还有更多,只需检查对您有用的内容)。

    【讨论】:

    • 我正在为单个步骤查找输入数据框的内容。 IE。我想看看特征列。
    • 那么我会选择 LoggingTensorHook。
    • 再次感谢鲁本。我试图弄清楚。我现在正在查看 LoggingTensorHook 页面。
    • 嗯。我正在使用估算器 api。这篇文章有关于如何向其中添加张量板的 cmets。 stackoverflow.com/questions/43782767/…
    • 嗯。这不起作用。我将在下面发布一个不完整的答案,提供更多详细信息。
    猜你喜欢
    • 2012-02-18
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多