把Python变量记录到TensorFlow的summary中
在用tensorboard可视化时,遇到过一个坑:打算每训练一定步数,将验证集准确率记录下来。
一般做法如下:
...
# 1、计算准确率
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 2、添加scalar,汇总
tf.summary.scalar('accuracy', accuracy)
merged = tf.summary.merge_all()
...
# 3、运行,写入log
summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False))
test_writer.add_summary(summary, i)
上面的做法有个问题,在计算准确率时传入的是整个数据集,如果数据集很大,内存会不够用。一般需要分多个batch,每次输入一个batch,计算预测正确的样本数,然后求和除以总样本数:
steps_per_epoch = data.num_examples // FLAGS.batch_size
num_examples = steps_per_epoch * FLAGS.batch_size
true_count = 0
for i in range(steps_per_epoch):
images_feed, labels_feed = data.next_batch(FLAGS.batch_size)
true_count += sess.run(correct, feed_dict={images_pl: images_feed, labels_pl:labels_feed, training_pl: False})
validate_accuracy = true_count / num_examples
但是这样得到的结果validate_accuracy是一个变量,如何把普通的Python变量加到summary中?
官方APIhttps://tensorflow.google.cn/api_docs/python/tf/summary/FileWriter#add_summary中描述如下:
You can pass the result of evaluating any summary op, using tf.Session.run or tf.Tensor.eval, to this function. Alternatively, you can pass a tf.Summary protocol buffer that you populate with your own data. The latter is commonly done to report evaluation results in event files.
实现如下:
summary = tf.Summary()
summary.value.add(tag='val_acc', simple_value=validate_accuracy)
summary_writer.add_summary(summary, i)
最后运行结果:
————————————————
版权声明:本文为CSDN博主「qiumokucao」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qiumokucao/article/details/81489460