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

今天在跑Tensorflow测试程序的时候,遇到了一下问题。

测试程序

import tensorflow as tf
a = tf.constant([1.0, 2.0, 3.0], name="a")
b = tf.constant([3.0, 4.0, 3.0], name="b")
result = a + b
sess = tf.Session()
print(sess.run(result))

报错1:
在这里插入图片描述
原因:tensorflow版本问题

解决办法:
使用

sess = tf.compat.v1.Session()

注意Session后面的括号,我之前忘了加括号就报错。

报错2:
在这里插入图片描述
原因:还是因为版本问题

解决办法:
在前面加上这一行代码

tf.compat.v1.disable_eager_execution()

最后的测试程序是:

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
a = tf.constant([1.0, 2.0, 3.0], name="a")
b = tf.constant([3.0, 4.0, 3.0], name="b")
result = a + b
result2 = a + b
sess = tf.compat.v1.Session()
print(sess.run(result))

后记:tensorflow版本问题太多…对我很不友好…但是也反映一个问题,我只知道是版本问题,也不知道为什么会出现版本问题,后续学习可以关注一下。

参考链接:
https://blog.csdn.net/qq_33440324/article/details/94200046
https://blog.csdn.net/weixin_38410551/article/details/103631977

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值