Python Tensorflow:必须为占位符tensor';占位符';使用dtype float[但该值是一个float]
我正在学习tensorflow教程,不断遇到以下错误:Python Tensorflow:必须为占位符tensor';占位符';使用dtype float[但该值是一个float],python,numpy,tensorflow,Python,Numpy,Tensorflow,我正在学习tensorflow教程,不断遇到以下错误: InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
以下是回溯:
Traceback (most recent call last):
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 972, in _do_call
return fn(*args)
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 954, in _run_fn
status, run_metadata)
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/contextlib.py", line 66, in __exit__
next(self.gen)
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/framework/errors.py", line 463, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = 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 "neural_network.py", line 48, in <module>
print(sess.run(loss), feed_dict={xs:x_data, ys:y_data})
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 717, in run
run_metadata_ptr)
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 915, in _run
feed_dict_string, options, run_metadata)
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 965, in _do_run
target_list, options, run_metadata)
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 985, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'Placeholder', defined at:
File "neural_network.py", line 21, in <module>
xs = tf.placeholder(tf.float32,[None,1])
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1332, in placeholder
name=name)
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1748, in _placeholder
name=name)
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
op_def=op_def)
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/mddrill/anaconda2/envs/python35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
self._traceback = _extract_stack()
正如您在这里看到的,x_数据和y_数据都是浮点数,所以我不明白为什么会出现这个错误
当我在sess.run(train_step,feed_dict={xs:x_data,ys:y_data})之前添加一个print语句print(type(x_data[0][0]),type(y_data[0][0])
。我明白了:
很明显,它们都是浮点数
我错过了什么 您在
print()
语句中有一个错误。幸运的是,在我使用TensorFlow r0.11时,它作为一个SyntaxError
被我选中
取代
print(sess.run(loss), feed_dict={xs:x_data, ys:y_data})
有,
你使用TensorFlow r0.12吗?这确实节省了我几个小时。我有一个eval()语句,但我忘了传递提要。谢谢!
print(sess.run(loss), feed_dict={xs:x_data, ys:y_data})
print(sess.run(loss, feed_dict={xs:x_data, ys:y_data}))