我在MNIST数据集上用TensorFlow训练了一个简单的神经网络。代码的训练部分工作正常。但是,当我将单个图像馈送到网络中时,它会给我以下回溯:
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
这是我尝试输入单个数字(从数据集中随机选择(的地方:
#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的新手。
您的feed_dict只为 x 放置值,而不为标签放置值。 您还应该输入 y place_holder,即:{x:image, y:val}
在你的代码中,y
是一个占位符:
x = tf.placeholder('float', shape = [None, 784])
y = tf.placeholder('float') #labels
当你告诉 tensorflow sess.run(y, ...)
时,它计算的是占位符值,而不是推理值(这是在损失函数中与y
进行比较的张量(。这就是它抱怨的原因。
您要计算的是预测的y
值。它不在你的代码片段中,但由于训练有效,应该有一个。这个张量依赖于x
,所以它可以通过输入x
值来评估。
如果在输入正确的 numpy 形状并按照错误消息的建议维护正确的 dtypes(np.int32 或 np.float32(后仍然遇到相同的错误,则以下代码应该可以解决您的问题:
#this code will print the list of placeholders and other variables declared in the memory which is causing your error
[n.name for n in tf.get_default_graph().as_graph_def().node]
#it will reset your declared placeholders so you can start over
tf.reset_default_graph()
这个类似的问题可以通过为每个调试重复重新启动内核来解决,但这是不可行的。