RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
执行sess.run()报错
import tensorflow.compat.v1 as tf
hello = tf.constant('Hello tensorflow')
sess = tf.Session()
print(sess.run(hello))
sess.close()
中文意思:会话图为空。在调用run()之前将操作添加到图中。
问题产生的原因:无法执行sess.run()的原因是tensorflow版本不同导致的,tensorflow版本2.0无法兼容版本1.0.
解决方案:
import tensorflow.compat.v1 as tf
tf.disable_eager_execution() #保证sess.run()能够正常运行
hello = tf.constant('Hello tensorflow')
sess = tf.Session()
print(sess.run(hello))
sess.close()
建议:tf2.X与1.X还是有很多不同,每次错误的地方,都要有意识的记忆,这也是成为优秀程序员的必经之路。不要一股脑的就百度找答案,先理解中文意思,然后尝试自己解决一下,会很有帮助。