在Tensorboard中获得简单的情节

时间:2017-04-20 09:48:13

标签: python tensorflow tensorboard

我正在尝试在张量板上绘制一个简单的情节,就像他们在主页上有这样的情节一样:

enter image description here 为了理解这是如何工作的,我写了以下内容:

    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)

虽然我可以看到图表,但我看不到任何标量值。可以向我解释我在这里做错了什么吗? PS:我已经运行了所有正式的例子,他们都在工作,但我需要理解这个例子才能使用它。 谢谢你的帮助 !

更新

运行@ dv3代码后程序崩溃。这就是我得到的:

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)

1 个答案:

答案 0 :(得分:3)

所以马上,我想建议阅读this。它会详细介绍会话的内容。

关于代码及其产生结果的原因:您没有初始化变量。您可以使用sess.run(tf.global_variables_initializer())执行此操作。所以你的代码是:

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)

我不会将sess.run嵌入到summary.scalar调用中,但是对于这个简单的例子,你会得到一些结果。

修改 测试,这实际上是有效的:

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)

输出: enter image description here