Search code examples
pythontensorflowtensorboard

No scalar data in tensorboard


I have been reading tensorboard documentation about scalars and I have a problem with presenting it in tensorboard.

I have pip install tensorflow in windows 10

my code looks like this:

import tensorflow as tf

a = tf.constant(7, name='test_variable')
tf.summary.scalar('variable', a)

with tf.Session() as sess:
    tf.summary.FileWriter('my_folder', graph=sess.graph)
    X = tf.global_variables_initializer()
    sess.run(X)

I see there is a file in my_folder

in command prompt: tensorboard --logdir=my_folder --port 6006

out:

C:\Users\MM>tensorboard --logdir=my_folder --port 6006
Starting TensorBoard b'54' at http://DESKTOP-9S2D9VF:6006
(Press CTRL+C to quit)

When I open browser i get:

No scalar data was found. 
Probable causes: etc. etc.

Solution

  • You need to run the summary_op and pass the result to the FileWriter. For example:

    import tensorflow as tf
    
    a = tf.constant(7, name='test_variable')
    tf.summary.scalar('variable', a)
    summary_op = tf.summary.merge_all()
    
    with tf.Session() as sess:
        summary_writer = tf.summary.FileWriter('/tmp/summary', graph=sess.graph)
        X = tf.global_variables_initializer()
        sess.run(X)
        summary = sess.run(summary_op)
        summary_writer.add_summary(summary)
    

    From the official documentation:

    Then, you can just run the merged summary op, which will generate a serialized Summary protobuf object with all of your summary data at a given step. Finally, to write this summary data to disk, pass the summary protobuf to a tf.summary.FileWriter.