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

查看:171
本文介绍了使用同一张图显示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...

这怎么可能?

推荐答案

您可以重复使用precision节点,但是需要使用两个不同的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天全站免登陆