tensorflow2.0使用注意事项

欢迎关注

tensorflow2.0使用注意事项—版本兼容问题

(1) 使用较早版本的tensorflow版本语句,新版本不支持,如:tensorflow.Session()。新版本中可以使用兼容的解决方案:tensorflow.compat.v1.Session()

compat.v1:表示兼容版本 1

import tensorflow as tf

print(tf.__version__)
hello=tf.constant('Hello tensorflow')
with tf.compat.v1.Session() as sess:#compat.v1,兼容性设置
    print(sess.run(hello))

(2) v1 版本代码一个一个修改比较麻烦,可以通过禁用 v2 版本,全局使用 v1 版本,用以下语句:


import tensorflow.compat.v1 as tf#设置为v1版本
tf.disable_v2_behavior()#禁用v2版本

#这样执行以下语句就不会报错
print(tf.__version__)#查看tensorflow版本
hello=tf.constant('Hello tensorflow')
with tf.Session() as sess:
    print(sess.run(hello))
# sess=tf.Session()
# print(sess.run(hello))

(3) 数据类型被集中放在dtypes中,tensorflow.dtypes.去调用;数学工具迁移到tensorflow.math.包中:

import tensorflow as tf

v1=tf.constant(8.0,dtype=tf.dtypes.float32)
v2=tf.constant(2.0,dtype=tf.dtypes.float32)
v3=tf.math.multiply(v1,v2)
with tf.compat.v1.Session() as sess:
    print(sess.run(v3))
发布了17 篇原创文章 · 获赞 7 · 访问量 1453

猜你喜欢

转载自blog.csdn.net/qq_40211493/article/details/103318739
今日推荐