解决: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())
相关推荐
抱抱宝7 分钟前
大模型应用开发教程08 | 构建完整 RAG 应用(Chroma/FAISS 实战)
人工智能·gpt·prompt·agent
美团技术团队30 分钟前
KDD‘26 美团学术论文精选及KDD Cup‘26 DataAgents赛道冠军思路解读
人工智能
AKAMAI38 分钟前
当AI模型超出存储增长时
人工智能·云计算
卷无止境1 小时前
在 awesome-fastapi 里,哪些库值得一看?
后端·python
DS随心转小程序1 小时前
ChatGPT 文字怎么转为 word?解析各类转换方案,AI 导出鸭成为高效文档转换新选择
人工智能·chatgpt·word·豆包·deepseek·ai导出鸭
zhanghaha13141 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
乌恩大侠1 小时前
【AI-RAN】硬件产品:DELL 前传交换机
人工智能·spark·aerial·o-ru·ai-ran
Python私教2 小时前
API 输出模型怎么设计:从 model_dump() 到显式展示层
python·fastapi
Python私教2 小时前
本地 AI 工具服务该绑定 127.0.0.1 还是 0.0.0.0?
python·fastapi