Python Tensorflow:InvalidArgumentError:必须为占位符tensor';yy';数据类型为int32

Python Tensorflow:InvalidArgumentError:必须为占位符tensor';yy';数据类型为int32,python,tensorflow,Python,Tensorflow,作为Tensorflow的新手,我很难理解在这个框架中如何管理占位符 如果我第一次运行上面的代码,它将返回9(正确值)。 但是如果我在同一个jupyter会话中再次运行它,我会得到下面的错误。这就好像全局变量(在本例中为占位符)没有得到清理,尽管我使用“with”关闭会话 堆栈跟踪: import tensorflow as tf y_hat = tf.constant(36, name='y_hat') # Define y_hat constant. Set to 36

作为Tensorflow的新手,我很难理解在这个框架中如何管理占位符

如果我第一次运行上面的代码,它将返回9(正确值)。 但是如果我在同一个jupyter会话中再次运行它,我会得到下面的错误。这就好像全局变量(在本例中为占位符)没有得到清理,尽管我使用“with”关闭会话

堆栈跟踪:

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为tf
的右下方添加行
tf.reset\u default\u graph()
,这样每次运行代码时tensorflow图都会重置。那么您将不会得到这个错误

顺便说一下,您实际上不需要将
loss
指定为变量。你可以跑了

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)

上面的代码打印9。

没有完成我的答案。无论如何,我想用tf。变量,以查看其行为。用你的方法(我以前用过),我没有遇到任何问题。因此,不知何故,他们的行为确实有所不同。
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}))