TensorFlow数据运算错误——The Session graph is empty. Add operations to the graph before calling run()

在利用TensorFlow2.x中进行计算时,可能会出现这种错误“RuntimeError: The Session graph is empty. Add operations to the graph before calling run()”

问题产生原因:

  1. tensorflow版本不同导致的,tensorflow版本2.0无法兼容版本1.0.
  2. 未对计算后的结果进行 tf.constant() 转换

解决办法:

tf.compat.v1.disable_eager_execution()

示例代码:

将原始代码与改进后的代码进行对比,即可发现问题所在:

1.原始问题代码:
# TensorFlow实现加法运算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
# 开启绘画
with tf.compat.v1.Session() as sess:   # 这是TensorFlow1.x里面的方法,2.x已经不用了
c_t_value = sess.run(c_t)
print("c_t_value:\n", c_t_value)
2.修改后代码:
# TensorFlow实现加法运算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
# 开启绘画
tf.compat.v1.disable_eager_execution()
c_t = tf.constant(c_t)
sess = tf.compat.v1.Session()
c_t_value = sess.run(c_t)
print("c_t_value:\n", c_t_value)

本文是在原有解决方案的基础上进行的细化,和大家一起讨论学习。
参考文章:https://blog.csdn.net/weixin_38410551/article/details/103631977

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值