程序报错:AttributeError: module 'tensorflow' has no attribute 'xxx' 解决办法

执行程序遇到报错:AttributeError: module ‘tensorflow’ has no attribute ‘xxx’ 解决办法

报错例如:
AttributeError: module ‘tensorflow’ has no attribute ‘reset_default_graph’
AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’
AttributeError: module ‘tensorflow’ has no attribute ‘sub’
。。。等等
在这里插入图片描述
报错原因:
Tensorflow1.x版本和2.x版本部分方法名不兼容。
一定是你安装使用的是Tensorflow2.0及以上版本,而在程序中使用了Tensorflow1.x版本的方法或者情况相反。

解决办法(一):

1.如果你导入Tensorflow模块的代码为:

import tensorflow 

替换为:

import tensorflow.compat.v1

2.如果你导入Tensorflow模块的代码为:

import tensorflow as tf

则替换为:

import tensorflow.compat.v1 as tf

解决办法(二):

如果上面的办法(一)还是没有解决问题的话,有以下两个硬核方案可选择:

1、卸载当前高版本的Tensorflow,安装1.x版本的Tensorflow。

2、将代码中不兼容的方法命,更改为对应Tensorflow版本的方法名。

发布了15 篇原创文章 · 获赞 4 · 访问量 831

猜你喜欢

转载自blog.csdn.net/qq_41320433/article/details/104198059