我是Tensorflow的新手。下面给出了有问题的代码:
import tensorflow as tf
g = tf.Graph()
with g.as_default():
x = tf.constant(8, name="x_const")
y = tf.constant(5, name="y_const")
my_sum = tf.add(x, y, name="x_y_sum")
with tf.Session() as sess:
print(my_sum.eval())
上面的代码在 PyCharm 中运行没有任何错误,给出正确的结果。但是当我从命令行尝试相同的代码时,我得到了一些错误,我提供了屏幕截图。 错误的屏幕截图
我无法理解错误的原因。错误中的主行指出:
"Cannot use the default session to evaluate tensor: the tensor's graph is
different from the session's graph.Pass an explicit session to
'eval(session=sess)'"
但即使我给出一个明确的会话,它也会显示几乎相同的错误。我也不明白为什么它在 PyCharm IDE 中运行而没有任何错误。
我的系统:
- 视窗 7 旗舰版(64 位(
- 4GB 内存
- 英特尔奔腾 2020M 处理器
- 英特尔核芯显卡
- 蟒蛇 3.6.5(64 位(
- 张量流版本 1.5.0
将图形传递给会话。
with tf.Session(graph=g) as sess:
print(my_sum.eval())