In order to log a simple value val
to a TensorBoard summary I need to
val = 5
test_writer.add_summary(sess.run(tf.scalar_summary('test', val)), global_step)
Is
sess.run(tf.scalar_summary('test', val))
really necessary to get val
added as a summary?
Here's another (perhaps slightly more up-to-date) solution with the tf.Summary.FileWriter class:
summary_writer = tf.summary.FileWriter(logdir=output_dir)
value = tf.Summary.Value(tag='variable name', simple_value=value)
summary_writer.add_event(summary=tf.summary.Event(tf.Summary([value]),
wall_time=time.time(),
step=global_step))
Then you can create your SummarySaverHook as such:
summary_hook = tf.train.SummarySaverHook(
summary_writer=summary_writer,
summary_op=your_summary_op)
which you can pass to your MonitoredTrainingSession. An example of a summary_op is tf.summary.merge_all()
NOTE: You will have to wait for the FileWriter to flush for it to appear in your events file. You can force it by calling summary_writer.flush()
A simpler solution:
summary_writer = tf.summary.FileWriter(output_dir)
summary = tf.Summary()
summary.value.add(tag='name of var', simple_value=value)
summary_writer.add_summary(summary, global_step)
summary_writer.flush()
You can construct the summary by yourself, like
from tensorflow.core.framework import summary_pb2
value = summary_pb2.Summary.Value(tag="Accuracy", simple_value=0.95)
summary = summary_pb2.Summary(value=[value])
you can then add summary
using add_summary
like in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With