Python 使用相同的图形在 TensorFlow 中显示训练和验证的准确性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40146428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show training and validation accuracy in TensorFlow using same graph
提问by Spacey
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
.
我有一个 TensorFlow 模型,该模型的一部分用于评估准确性。该accuracy
是在tensorflow图,它是只发生在另一个节点logits
和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)
...
Also inside that for loop, every say, 100 iterations, I want to evaluate the validationaccuracy. I have a separate feed_dict for this, and I am able to evaluate the validation accuracy very nicely in python.
同样在 for 循环中,每说 100 次迭代,我想评估验证准确性。我为此有一个单独的 feed_dict,并且我能够在 python 中很好地评估验证准确性。
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...
但是,这是我的问题:我想通过使用节点对验证准确度进行另一个总结accuracy
。我不清楚如何做到这一点。因为我有accuracy
节点,所以我应该能够重用它是有道理的,但我不确定如何准确地做到这一点,这样我也可以将验证准确性写成单独的 scalar_summary...
How might this be possible?
这怎么可能?
回答by wih
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.
您可以重用准确度节点,但需要使用两种不同的 SummaryWriter,一种用于训练运行,另一种用于测试数据。此外,您必须将标量摘要的准确性分配给变量。
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')
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.
然后在您的训练循环中,您进行正常训练并使用 train_writer 记录您的摘要。此外,您每 100 次迭代在测试集上运行图表,并使用 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))
You can then point TensorBoard to the parent directory (summaries_dir) and it will load both data sets.
然后,您可以将 TensorBoard 指向父目录 (summaries_dir),它将加载两个数据集。
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 HowTo 的https://www.tensorflow.org/versions/r0.11/how_tos/summaries_and_tensorboard/index.html 中找到
回答by Yibo Yang
To run the same operation but get summaries with different feed_dict data, simply attach two summary ops to that op. Say you want to run accuracy op on both validation and test data and want to get summaries for both:
要运行相同的操作但使用不同的 feed_dict 数据获取摘要,只需将两个摘要操作附加到该操作。假设您想对验证数据和测试数据运行准确率操作,并希望获得两者的摘要:
validation_acc_summary = tf.summary.scalar('validation_accuracy', accuracy) # intended to run on validation set
test_acc_summary = tf.summary.scalar('test_accuracy', accuracy) # intended to run on test set
with tf.Session() as sess:
# do your thing
# ...
# accuracy op just needs labels y_ and input x to compute logits
validation_summary_str = sess.run(validation_acc_summary, feed_dict=feed_dict={x: mnist.validation.images,y_: mnist.validation.labels})
test_summary_str = sess.run(test_acc_summary, feed_dict={x: mnist.test.images,y_: mnist.test.labels})
# assuming you have a tf.summary.FileWriter setup
file_writer.add_summary(validation_summary_str)
file_writer.add_summary(test_summary_str)
Also remember you can always pull raw (scalar) data out of the protobuff summary_str like thisand do your own logging.
还请记住,您始终可以像这样从 protobuff summary_str 中提取原始(标量)数据并进行自己的日志记录。