TensorFlow技术解析与实战

TensorFlow技术解析与实战


在阅读《TensorFlow技术解析与实战》(ISBN 978-7-115-45613-7)的过程中,发现全书是基于TensorFlow 1.1版本来着手的,而当前普遍已经使用TensorFlow 2.x版本,因此我将自己在阅读过程中以及代码实现过程中产生的问题以及寻找到的解决方法记录下来,方便其他的读者在阅读过程中遇到相同的问题时可以更加快速的得以解决。

TensorFlow环境的准备

在tensorflow1.x 版本下的代码需修改后才能使用

tensorflow 1.x版本下的代码:

import tensorflow as tf
sess = tf.Session()
a = tf.constant(10)
b = tf.constant(22)
print(sess.run(a+b))

在2.x版本下运行上述代码会产生错误:
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

问题产生的原因及解决方案来自于博客1
原因:无法执行sess.run()的原因是tensorflow版本不同导致的,tensorflow版本2.x无法兼容版本1.x.
解决方案:tf.compat.v1.disable_eager_execution()

tensorflow 2.x版本下的正确运行代码为:

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
sess = tf.compat.v1.Session()
a = tf.constant(10)
b = tf.constant(22)
print(sess.run(a+b))

  1. https://blog.csdn.net/weixin_38410551/article/details/103631977 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值