Search code examples
pythontensorflowtensorboard

Getting a simple plot in Tensorboard


I'm trying to a simple plot on tensorboard, just like they have it on homepage, something like this:

enter image description here To understand how this is working I've wrote the following :

    import tensorflow as tf
import numpy as np


x = tf.placeholder('float',name='X')
y=  tf.placeholder('float',name='y')
addition = tf.add(x,y)


with tf.Session() as sess:

    for i in range(100):
        var1=  np.random.rand()
        var2=  np.random.rand()
        print(var1,var2)
        tf.summary.scalar('addition',sess.run(addition, feed_dict={x:var1,y:var2}))               
        writer = tf.summary.FileWriter('Graphs',sess.graph)

while I can see the graph, I can't see any scalar value. Can any explain to me what I'm doing wrong here? PS: I've run all official examples and they are all working but I need to understand this example to be able to work with it. Thanks for any help !

Update

after run @dv3 code the program crashs. and here is what I get:

InvalidArgumentError: You must feed a value for placeholder tensor 'input/x-input' with dtype float
     [[Node: input/x-input = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-5-5cbd77e71936> in <module>()
     14         var2=  np.random.rand()
     15         print(var1,var2)
---> 16         add, s_ = sess.run([addition, summary_op], feed_dict={x:var1,y:var2})
     17         writer.add_summary(s_, i)

Solution

  • So right off the bat, I want to suggest reading this. It goes a little more in detail what a session is.

    As to the code and why it doesn't produce results: you're not initializing the variables. You can do this with: sess.run(tf.global_variables_initializer()). So your code would be:

    import tensorflow as tf
    import numpy as np
    
    x = tf.placeholder('float',name='X')
    y=  tf.placeholder('float',name='y')
    addition = tf.add(x,y)
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(100):
            var1=  np.random.rand()
            var2=  np.random.rand()
            print(var1,var2)
            tf.summary.scalar('addition',sess.run(addition, feed_dict={x:var1,y:var2}))               
            writer = tf.summary.FileWriter('Graphs',sess.graph)
    

    I wouldn't embed sess.run into the summary.scalar call, but for this simple example, you'll get some results.

    Edit: tested and this is actually working:

    import tensorflow as tf
    import numpy as np
    
    x = tf.placeholder('float',name='X')
    y=  tf.placeholder('float',name='y')
    addition = tf.add(x,y, name='add')
    tf.summary.scalar('addition', addition)
    summary_op = tf.summary.merge_all()     
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        writer = tf.summary.FileWriter('Graphs',sess.graph)
        for i in range(100):
            var1=  np.random.rand()
            var2=  np.random.rand()
            print(var1,var2)
            add, s_ = sess.run([addition, summary_op], feed_dict={x:var1,y:var2})
            writer.add_summary(s_, i)
    

    output: enter image description here