tensorflow2 RuntimeError: The Session graph is empty. Add operations to the graph before calling ru

出现问题

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

原因

tensorflow2 版本更新

解决方法

加入tf.compat.v1.disable_eager_execution()如下:

import tensorflow as tf #引入模块
tf.compat.v1.disable_eager_execution()

x = tf.constant([[1.0, 2.0]]) #定义一个 2 阶张量等于[[1.0,2.0]]
w = tf.constant([[3.0], [4.0]]) #定义一个 2 阶张量等于[[3.0],[4.0]]
y = tf.matmul(x, w) #实现 xw 矩阵乘法
print (y) #打印出结果

#tf.compat.v1.Session() as session
with tf.compat.v1.Session() as sess:
    print (sess.run(y))

猜你喜欢

转载自blog.csdn.net/qq_40575024/article/details/105862921