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

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

kk5522kk 回答:运行时错误:会话图为空。将操作添加到图形

事情是

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

这使输出没有任何错误 还有一件事情可以使急切执行成为可能,以防万一记住在算法启动时必须调用它 有关更多信息,请查阅文档 如果有任何问题,请随时询问。 顺便说一下,我只是tensorflow和keras的初学者。 谢谢!

本文链接:https://www.f2er.com/3159192.html

大家都在问