【问题标题】:Error when computing summaries in TensorFlow在 TensorFlow 中计算摘要时出错
【发布时间】:2016-05-08 22:48:45
【问题描述】:

我正在尝试使用 TensorFlow 生成摘要并使用 TensorBoard 将其可视化。但是,我收到了一个我不理解的错误 (InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float)。

这是我的程序的完整源代码:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

import tensorflow as tf

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)

_ = tf.histogram_summary("weights", W)
_ = tf.histogram_summary("biases", b)
_ = tf.histogram_summary("y", y)


y_ = tf.placeholder(tf.float32, [None, 10])

with tf.name_scope("xent") as scope:
    cross_entropy = -tf.reduce_sum(y_*tf.log(y))
    _ = tf.scalar_summary("cross entropy", cross_entropy)

with tf.name_scope("train") as scope:
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

init = tf.initialize_all_variables()

sess =  tf.Session()
sess.run(init)

with tf.name_scope("test") as scope:
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    _ = tf.scalar_summary("accuracy", accuracy)

merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter("/tmp/mnist_nn", sess.graph_def)

for i in range(1000):
    if (i % 10) == 0:
        feed = {x: mnist.test.images, y_: mnist.test.labels}
        result = sess.run([merged, accuracy], feed_dict=feed) 
        summary_str = result[0]
        acc = result[1]
        print("Accuracy at step %s: %s" % (i, acc))
    else:
        batch_xs, batch_ys = mnist.train.next_batch(100)
        feed = {x: batch_xs, y_: batch_ys}
        sess.run(train_step, feed_dict=feed)

print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))

但是,当我尝试运行上述代码时,出现以下错误:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-23-584a7bc91816> in <module>()
     39     if (i % 10) == 0:
     40         feed = {x: mnist.test.images, y_: mnist.test.labels}
---> 41         result = sess.run([merged, accuracy], feed_dict=feed)
     42         summary_str = result[0]
     43         acc = result[1]

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict)
    366 
    367     # Run request and get response.
--> 368     results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
    369 
    370     # User may have fetched the same tensor multiple times, but we

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_run(self, target_list, fetch_list, feed_dict)
    442         # pylint: disable=protected-access
    443         raise errors._make_specific_exception(node_def, op, error_message,
--> 444                                               e.code)
    445         # pylint: enable=protected-access
    446       six.reraise(e_type, e_value, e_traceback)

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'Placeholder_1', defined at:

从错误看来,我的源代码中的占位符没有被输入适当的值。据我所知,我正在为所有占位符(xy_)提供值。

如果您需要,我会将完整日志添加到此问题中。

我还发现,当我第一次获取 mnist 时,它确实有效(输出如下),但仍然没有生成 TensorBoard 可视化:

Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
Tensor("MergeSummary/MergeSummary:0", shape=TensorShape([]), dtype=string)
merged
Accuracy at step 0: 0.098
Accuracy at step 10: 0.7404
Accuracy at step 20: 0.8041
Accuracy at step 30: 0.814 ...

【问题讨论】:

    标签: python tensorflow tensorboard


    【解决方案1】:

    从您的错误消息看来,您使用的是 IPython。使用 IPython 构建 TensorFlow 模型时的一个缺陷是,tf.merge_all_summaries() 之类的函数将记住在当前会话中创建的每个摘要,包括因错误而失败的单元格。这是 TensorFlow 使用 默认图表 收集进程中创建的所有操作、摘要等的结果,除非您明确指定图表。我怀疑您对tf.merge_all_summaries() 的调用返回的结果超过了您在代码中创建的三个直方图摘要,并且较旧的直方图摘要将依赖于 previously created 占位符。

    有两种主要方法可以解决这个问题。最简单的是显式合并摘要,而不是使用tf.merge_all_summaries()

    weights_summary = tf.histogram_summary("weights", W)
    biases_summary = tf.histogram_summary("biases", b)
    y_summary = tf.histogram_summary("y", y)
    
    merged = tf.merge_summary([weights_summary, biases_summary, y_summary])
    

    另一种方法是在构建模型之前设置显式默认图表。如果您想将模型拆分到多个 IPython 单元中,这会很尴尬,但也应该可以:

    # Sets a new default graph, and stores it in `g`.
    with tf.Graph().as_default() as g:  
    
        x = tf.placeholder(tf.float32, [None, 784])
        # ...
    
        # Summaries are added to `g`.
        _ = tf.histogram_summary("weights", W)
        _ = tf.histogram_summary("biases", b)
        _ = tf.histogram_summary("y", y)
    
        # ...
    
        # `merged` contains only summaries from `g`.
        merged = tf.merge_all_summaries()
    
        # ...
    

    【讨论】:

    • 不使用交互式会话是“最佳实践”吗?这两种解决方案似乎都不理想。人们会希望有一种“清除”默认图表的方法
    • 您可以通过调用tf.reset_default_graph() 来清除默认图形,但任何以前创建的tf.InteractiveSession 对象都不会看到任何新创建的节点。如果您有跨多个单元格的复杂 IPython 会话,我建议您避开任何隐式依赖于“所有”先前创建的对象(tf.merge_all_summaries()tf.initialize_all_variables())的函数。
    【解决方案2】:

    对于 keras 用户

    在使用 TensorBoard 回调并从头开始拟合 new 模型时,您可能会遇到此错误。在这种情况下,解决方案是调用

    from keras import backend as K
    K.clear_session()
    

    在创建新模型之前。用 keras 2.1.5 和 tensorflow 1.6.0 验证

    【讨论】:

    • 希望我能更多地支持这个,这应该在 keras 手册中得到高度重视。也咬我。
    猜你喜欢
    • 1970-01-01
    • 2022-06-27
    • 2010-12-13
    • 1970-01-01
    • 2021-05-22
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多