Tensor.graph is meaningless when eager execution is enabled.解决办法TensorFlow2.0 使用1.x的代码时报错,

作为一个刚刚入门TensorFlow的小白,看着1.x的教程用着2.0也是一种挑战吧,第一次接触graph就报错
Tensor.graph is meaningless when eager execution is enabled.
查找了很多资料,就是说2.0好像是为了更安全对graph有一个默认的 eager execution is enabled by default,叫急切执行什么的,不管他有什么好处,我们为了跑通我们的代码,所以我们需要禁止它,2.x的TensorFlow就会返回执行1.x
所以在程序前面加上一句这个

tf.compat.v1.disable_v2_behavior()

然后将

tf.get_default_graph()

替换成

tf.compat.v1.get_default_graph()

对于session也会遇见这个问题,需要在tf后面添加一个compat.v1.,这个目的是应用version 1中的函数
在这里插入图片描述
其实可以根据以上知道我们可以有个通用解决办法,在TensorFlow2.x上运行1.x的代码我们在import TensorFlow的时候直接写成这样

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

就可以解决了,不过官网提供了一个脚本方法,能讲1.x的代码自动升级为2.x,这里就不详细介绍了

不过会有一个警告disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
我们拒绝了2.0的优化哈哈哈,所以给我们警告了,不知道影响大不大

发布了15 篇原创文章 · 获赞 35 · 访问量 3398

猜你喜欢

转载自blog.csdn.net/weixin_44065323/article/details/103590852