运行时错误:会话图为空.将操作添加到图形


# 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文档但它给出了上述错误

问题是

tensorflow核心r2.0默认情况下启用了急切执行,因此不需要编写tf.compat.v1.Session((和使用.run((函数如果我们想使用tf.compat.v1.Session((,那么我们需要这样做tf.compat.v1.disable_eager_execution((。现在我们可以使用tf.compat.v1.Session((和.run((函数。

Tensorflow核心r2.0默认启用了急切执行。所以,不改变它我们只需要更改我们的代码

# 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))

这样可以在没有任何错误的情况下输出还有一件事要让急切的执行成为可能,以防记住它必须在算法启动时调用欲了解更多信息,请查看文档如果有任何问题,请随时询问。顺便说一句,我只是网球和keras的初学者。非常感谢。

最新更新