【问题标题】:Runtime Error : Session Graph is Empty. Add Operations to Graph运行时错误:会话图为空。向图形添加操作
【发布时间】:2019-11-05 13:39:23
【问题描述】:
# Build a graph.
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# Launch the graph in a session.
sess = tf.compat.v1.Session()
# Evaluate the tensor `c`.
print(sess.run(c))
以上代码取自 tensorflow core r2.0 文档
但它给出了上述错误
【问题讨论】:
标签:
tensorflow2.0
keras-2
【解决方案1】:
事情是这样的
tensorflow core r2.0 默认开启了 Eager Execution,所以不需要写 tf.compat.v1.Session() 和使用 .run() 函数
如果我们想使用 tf.compat.v1.Session() 那么我们需要这样做
tf.compat.v1.disable_eager_execution() 在算法的开始。现在我们可以使用 tf.compat.v1.Session() 和 .run() 函数了。
Tensorflow core r2.0 默认启用了 Eager Execution。所以,不改变它
我们只需要更改我们的代码
# Launch the graph in a session.
with tf.compat.v1.Session() as ses:
# Build a graph.
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# Evaluate the tensor `c`.
print(ses.run(c))
这给出了没有任何错误的输出
还有一件事可以使急切执行成为可能,以防万一记住它必须在算法启动时被调用
有关更多信息,请查看文档
如果有任何问题,请随时询问。
顺便说一句,我只是 tensorflow 和 keras 的初学者。
谢谢!