![](/img/trans.png)
[英]tensorflow InvalidArgumentError: You must feed a value for placeholder tensor with dtype float
[英]Tensorflow: InvalidArgumentError: You must feed a value for placeholder tensor 'yy' with dtype int32
import tensorflow as tf
y_hat = tf.constant(36, name='y_hat') # Define y_hat constant. Set to 36.
yy = tf.placeholder(tf.int32, shape=[])
loss = tf.Variable((yy - y_hat)**2, name='loss') # Create a variable for the loss
init = tf.global_variables_initializer()
with tf.Session() as session:
session.run(tf.global_variables_initializer(), feed_dict = {yy: 39})
print(session.run(loss, feed_dict={yy: 39}))
作为Tensorflow的新手,我很难理解在这个框架中如何管理占位符。
如果我第一次运行上面的代码,它将返回9(正确的值)。 但是如果我在同一个jupyter会话中再次运行它,我会得到以下错误。 就好像我使用“with”来关闭会话一样,全局变量(在本例中为占位符)没有得到清理
堆栈跟踪:
InvalidArgumentError: You must feed a value for placeholder tensor 'yy' with dtype int32
[[Node: yy = Placeholder[dtype=DT_INT32, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'yy', defined at:
File "/opt/conda/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/opt/conda/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
知道发生了什么以及如何解决它? 谢谢
在import tensorflow as tf
右下方添加行tf.reset_default_graph()
import tensorflow as tf
,这样每次运行代码时都会重置张量流图。 那你就不会得到这个错误。
顺便说一下,您并不需要将loss
指定为变量。 你可以跑
import tensorflow as tf
y_hat = tf.constant(36, name='y_hat')
yy = tf.placeholder(tf.int32, shape=[])
loss = (yy - y_hat)**2
with tf.Session() as session:
print(session.run(loss, feed_dict={yy: 39}))
上面的代码打印9。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.