问题描述
在Tensorflow 2环境下运行以下代码的时候遇到了”The Session graph is empty. Add operations to the graph before calling run()“的错误。
import tensorflow as tf
a= tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
b= tf.constant([1.0,2.0,3.0], shape=[3], name= 'b')
c= a+ b
sess= tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))
print(sess.run(c))
解决方法
参考链接:https://github.com/OlafenwaMoses/ImageAI/issues/400
将以上代码转成下面的形式便可以运行了。
import tensorflow as tf
with tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True)) as sess:
a= tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
b= tf.constant([1.0,2.0,3.0], shape=[3], name= 'b')
c= a+ b
print(sess.run(c))
参考链接中的英文解释为:
The tensorflow core r2.0 have