关于tensorflow 中module ‘tensorflow‘ has no attribute ‘xxx‘问题的根本解决方法。

在用tensorflow复现github上面的代码时,经常会出现tensorflow版本不对的情况,如下图所示。
关于tensorflow 中module ‘tensorflow‘ has no attribute ‘xxx‘问题的根本解决方法。原代码为:

FLAGS = tf.flags.FLAGS

很多博主的解决方法是添加以下两行代码:
`import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()
`
但在很多情况下它不起作用。
我们都知道这是因为tensorflow版本的问题,所以最好的解决方法是去tensorflow的官网去查看对应的函数改版前后的变化。

tensorflow的网站:
https://www.tensorflow.org/api_docs/python

进入网站
关于tensorflow 中module ‘tensorflow‘ has no attribute ‘xxx‘问题的根本解决方法。

输入要查询的函数
关于tensorflow 中module ‘tensorflow‘ has no attribute ‘xxx‘问题的根本解决方法。

点击第二行进入
关于tensorflow 中module ‘tensorflow‘ has no attribute ‘xxx‘问题的根本解决方法。

看得到
关于tensorflow 中module ‘tensorflow‘ has no attribute ‘xxx‘问题的根本解决方法。
原来的代码由于tensorflow版本的原因,改为了:
关于tensorflow 中module ‘tensorflow‘ has no attribute ‘xxx‘问题的根本解决方法。
因此,有问题的代码可以更改为:

FLAGS = tf.compat.v1.flags.FLAGS

成功运行。

基本所有的module ‘tensorflow’ has no attribute ‘xxx’都是由于tensorflow版本原因导致的,以上解决方法是可以推广到类似问题的

tensorflow的官网可能需要科学上网才能进得去,如果实在进不去,这种问题据我的经验,一般在出现问题的函数所在的代码行的tf后直接添加.compat.v1也可以运行成功,如上面的例子一样。

**以下是一个类似的例子:

saver = tf.train.Saver(dict(self.weights, **self.biases))

在这种情况下,应该改为

saver = tf.compat.v1.train.Saver(dict(self.weights, **self.biases))

**

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2022年3月23日
下一篇 2022年3月23日

相关推荐