RuntimeError: The Session graph is empty. Add operations to the graph before calling run().解决方法

问题产生的原因:无法执行sess.run()的原因是tensorflow版本不同导致的,tensorflow版本2.0无法兼容版本1.0.
解决办法:
tf.compat.v1.disable_eager_execution()

# import tensorflow as tf   #正常应该是这个
import tensorflow.compat.v1 as tf #兼容本地2.6的tensorflow环境下使用1.0的tensorflow
tf.compat.v1.disable_eager_execution()

#创建一个常量op
m1 = tf.constant([[3,3]])
#创建一个常量op
m2 = tf.constant([[2],[3]])
#创建一个矩阵乘法op,把m1和m2传入
product = tf.matmul(m1,m2)
print(product)

#定义一个会话,启动默认图
sess = tf.compat.v1.Session()
#调用sess的run方法来执行矩阵乘法op
#run(product)触发了图中3个op
result = sess.run(product)
print(result)
sess.close()

参考链接:https://blog.csdn.net/sinat_36502563/article/details/102302392
参考链接:https://ask.csdn.net/questions/657580

你可能感兴趣的:(人工智能,深度学习,tensorflow,python)