改为图执行模式
TensorFlow2虽然和TensorFlow1.x有较大差异,不能直接兼容。但实际上还是提供了对TensorFlow1.x的API支持
TensorFlow 2中执行或开发TensorFlow1.x代码,可以做如下处理:
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
简单两步即可
举例
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
node1 = tf.constant(3.0)
node2 = tf.constant(4.0)
node3 = tf.add(node1,node2)
print(node3)
由于是图执行模式,这时仅仅是建立了计算图,但没有执行
定义好计算图后,需要建立一个Session,使用会话对象来实现执行图的执行
sess = tf.Session()
print("node1:",sess.run(node1))
print("node2:",sess.run(node2))
print("node3:",sess.run(node3))
Session.close()
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有