RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
运行以下代码出错
import tensorflow.compat.v1 as tf
a = tf.constant(1.0)
b = tf.constant(2.0)
c = a * b
with tf.Session() as sess:
print(sess.run(c))
报错
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
解决方法
添加默认graph,解决问题
import tensorflow.compat.v1 as tf
g = tf.Graph()
with g.as_default():
a = tf.constant(1.0)
b = tf.constant(2.0)
c = a * b
with tf.Session() as sess:
print(sess.run(c))