必须输入占位符张量

必须输入占位符张量

本文介绍了InvalidArgumentError:必须输入占位符张量"Placeholder_1"的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用MNIST数据集上的TensorFlow训练了一个简单的神经网络.该代码的训练部分工作正常.但是,当我将单个图像馈入网络时,它会产生以下回溯:

I trained a simple neural network with TensorFlow on the MNIST dataset. The training portion of the code works fine. However, when I feed a single image into the network, it gives me the following traceback:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1021, in _do_call
    return fn(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1003, in _run_fn
    status, run_metadata)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 88, in __exit__
    next(self.gen)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 469, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.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"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tfbasics.py", line 113, in <module>
    classification = sess.run(y, feed_dict)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 766, in run
    run_metadata_ptr)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 964, in _run
    feed_dict_string, options, run_metadata)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1014, in _do_run
    target_list, options, run_metadata)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1034, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.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 'Placeholder_1', defined at:
  File "tfbasics.py", line 20, in <module>
    y = tf.placeholder('float') #labels
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1587, in placeholder
    name=name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2043, in _placeholder
    name=name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2240, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1128, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): 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"]()]]

这是我的变量:

x = tf.placeholder('float', shape = [None, 784])
y = tf.placeholder('float') #labels

在这里我要输入一个数字(从数据集中随机选择):

Here is where I am trying to input a single number (randomly chosen from the dataset):

#pick random number
num = randint(0, mnist.test.images.shape[0])
img = mnist.test.images[num]

#format the image
inp = np.asarray(img)
inp = np.transpose(inp)
image = np.expand_dims(inp, axis=0) # shape  : (1, 784)


#feed the image into the session
with tf.Session() as sess:
    feed_dict = {x: image}
    classification = sess.run(y, feed_dict)
    print(classification)

任何帮助将不胜感激!我是TensorFlow的新手.

Any help would be appreciated! I am new to TensorFlow.

推荐答案

在您的代码中,y占位符:

x = tf.placeholder('float', shape = [None, 784])
y = tf.placeholder('float') #labels

当您告诉tensorflow sess.run(y, ...)时,它是在计算占位符值,而不是推论值(这是在损失函数中与y相比较的张量).这就是它抱怨的原因.

When you tell tensorflow sess.run(y, ...), it's computing the placeholder value, not the inference value (that's the tensor that y is compared to in the loss function). That's why it's complaining.

您要计算的是预测的y.它不在您的代码段中,但是由于培训有效,因此应该有一个.此张量取决于x,因此只需输入x值即可对其进行评估.

What you want to compute instead is the predicted y value. It's not in your code snippet, but since the training works, there should be one. This tensor depends on x, so it can be evaluated just by feeding x value.

这篇关于InvalidArgumentError:必须输入占位符张量"Placeholder_1"的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 20:16