使用相同的图形在 TensorFlow 中显示训练和验证的准确性 [英] Show training and validation accuracy in TensorFlow using same graph

查看:32
本文介绍了使用相同的图形在 TensorFlow 中显示训练和验证的准确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TensorFlow 模型,该模型的一部分用于评估准确性.accuracy 只是张量流图中的另一个节点,它接收 logitslabels.

I have a TensorFlow model, and one part of this model evaluates the accuracy. The accuracy is just another node in the tensorflow graph, that takes in logits and labels.

当我想绘制训练精度时,这很简单:我有类似的东西:

When I want to plot the training accuracy, this is simple: I have something like:

tf.scalar_summary("Training Accuracy", accuracy)
tf.scalar_summary("SomethingElse", foo)
summary_op = tf.merge_all_summaries()
writer = tf.train.SummaryWriter('/me/mydir/', graph=sess.graph)

然后,在我的训练循环中,我有类似的东西:

Then, during my training loop, I have something like:

for n in xrange(1000):
  ...
  summary, ..., ... = sess.run([summary_op, ..., ...], feed_dict)
  writer.add_summary(summary, n)
  ...

也在 for 循环中,每次迭代 100 次,我想评估验证的准确性.我为此有一个单独的 feed_dict,并且我能够在 python 中很好地评估验证准确性.

Also inside that for loop, every say, 100 iterations, I want to evaluate the validation accuracy. I have a separate feed_dict for this, and I am able to evaluate the validation accuracy very nicely in python.

然而,这是我的问题:我想通过使用 accuracy 节点为验证准确性制作另一个摘要.我不清楚如何做到这一点.因为我有 accuracy 节点,所以我应该能够重用它是有道理的,但我不确定如何准确地做到这一点,这样我也可以将验证准确性写成单独的 scalar_summary...

However, here is my problem: I want to make another summary for the validation accuracy, by using the accuracy node. I am not clear on how to do this though. Since I have the accuracy node it makes sense that I should be able to re-use it, but I am unsure how to do this exactly, such that I can also get the validation accuracy written out as a separate scalar_summary...

这怎么可能?

推荐答案

您可以重复使用精度节点,但您需要使用两种不同的 SummaryWriter,一种用于训练运行,一种用于测试数据.此外,您还必须将标量摘要分配给变量以确保准确性.

You can reuse the the accuracy node but you need to use two different SummaryWriters, one for the training runs and one for the test data. Also you have to assign the scalar summary for accuracy to a variable.

accuracy_summary = tf.scalar_summary("Training Accuracy", accuracy)
tf.scalar_summary("SomethingElse", foo)
summary_op = tf.merge_all_summaries()
summaries_dir = '/me/mydir/'
train_writer = tf.train.SummaryWriter(summaries_dir + '/train', sess.graph)
test_writer = tf.train.SummaryWriter(summaries_dir + '/test')

然后在您的训练循环中,您将进行正常训练并使用 train_writer 记录您的摘要.此外,您每 100 次迭代就在测试集上运行图表,并使用 test_writer 仅记录准确性摘要.

Then in your training loop you have the normal training and record your summaries with the train_writer. In addition you run the graph on the test set each 100th iteration and record only the accuracy summary with the test_writer.

# Record train set summaries, and train
summary, _ = sess.run([summary_op, train_step], feed_dict=...)
train_writer.add_summary(summary, n)
if n % 100 == 0:  # Record summaries and test-set accuracy
  summary, acc = sess.run([accuracy_summary, accuracy], feed_dict=...)
  test_writer.add_summary(summary, n)
  print('Accuracy at step %s: %s' % (n, acc))

然后您可以将 TensorBoard 指向父目录 (summaries_dir),它将加载两个数据集.

You can then point TensorBoard to the parent directory (summaries_dir) and it will load both data sets.

这也可以在 TensorFlow HowTo 的 https://www.tensorflow.org/versions/r0.11/how_tos/summaries_and_tensorboard/index.html

This can be also found in the TensorFlow HowTo's https://www.tensorflow.org/versions/r0.11/how_tos/summaries_and_tensorboard/index.html

这篇关于使用相同的图形在 TensorFlow 中显示训练和验证的准确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆