解决:AttributeError: module ‘tensorflow‘ has no attribute ‘***‘

问题产生的原因是当前Python使用的Tensorflow库为2.0最新版本,而源代码使用的是1.0版本,在不降低版本的情况下运行代码需要做些调整:

找到报错的地方,在报错的attribute前面加上compat.v1.

举例说明:

源码:注意这个tf

python 复制代码
with tf.gfile.GFile(graph_filename, 'rb') as f:

更改后:

python 复制代码
with tf.compat.v1.gfile.GFile(graph_filename, 'rb') as f:

contrib特殊处理,学习自这里

当报错为AttributeError: module 'tensorflow' has no attribute 'contrib'

找到报错的代码,一般为:

python 复制代码
initializer = tf.contrib.layers.xavier_initializer()

此时不能用上面的方法修改代码,因为Tensorflow2.0版本把contrib库取消了,因此我们使用tf.initializers.GlorotUniform() 进行初始化,代码改为:

python 复制代码
initializer=tf.initializers.GlorotUniform())
相关推荐
冬奇Lab19 分钟前
RAG 系列(八):RAG 评估体系——用数据说话
人工智能·llm
landyjzlai1 小时前
蓝迪哥玩转Ai(8)---端侧AI:RK3588 端侧大语言模型(LLM)开发实战指南
人工智能·python
我叫黑大帅3 小时前
如何通过 Python 实现招聘平台自动投递
后端·python·面试
其实防守也摸鱼3 小时前
CTF密码学综合教学指南--第九章
开发语言·网络·python·安全·网络安全·密码学·ctf
ZhengEnCi3 小时前
05-自注意力机制详解 🧠
人工智能·pytorch·深度学习
砚底藏山河3 小时前
Python量化开发:2026最佳实时股票数据API接口推荐与对比
开发语言·windows·python
前端程序媛-Tian3 小时前
前端 AI 提效实战:从 0 到 1 打造团队专属 AI 代码评审工具
前端·人工智能·ai
weixin_417197054 小时前
DeepSeek V4绑定华为:一场飞行中换引擎的国产算力革命
人工智能·华为