Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add an arbitrary value to a TensorFlow summary?

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?

like image 984
orome Avatar asked May 30 '16 16:05

orome


2 Answers

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()
like image 138
Jean Pouget-Abadie Avatar answered Nov 08 '22 14:11

Jean Pouget-Abadie


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.

like image 7
etarion Avatar answered Nov 08 '22 14:11

etarion