我在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的新手。
答案 0 :(得分:0)
您的feed_dict仅为x而不是标签的值。你也应该为y place_holder投注,即:{x:image,y:val}
答案 1 :(得分:0)
在您的代码中y
是占位符:
x = tf.placeholder('float', shape = [None, 784])
y = tf.placeholder('float') #labels
当你告诉tensorflow sess.run(y, ...)
时,它会计算占位符值,而不是推理值(这是y
在损失函数中与之比较的张量)。这就是为什么它会抱怨。
您想要计算的是预测y
值。它不在您的代码段中,但由于培训有效,因此应该有一个。这个张量取决于x
,因此可以通过提供x
值来评估它。