解决: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())
相关推荐
木斯佳2 小时前
HarmonyOS 6实战:AI Action富媒体卡片迭代——实现快照分享
人工智能·harmonyos·媒体
芝士爱知识a2 小时前
2026高含金量写作类国际竞赛汇总与测评
大数据·人工智能·国际竞赛·写作类国际竞赛·写作类比赛推荐·cwa·国际写作比赛推荐
ZhengEnCi3 小时前
M3-markconv库找不到wkhtmltopdf问题
python
华农DrLai5 小时前
什么是LLM做推荐的三种范式?Prompt-based、Embedding-based、Fine-tuning深度解析
人工智能·深度学习·prompt·transformer·知识图谱·embedding
2301_764441335 小时前
LISA时空跃迁分析,地理时空分析
数据结构·python·算法
东北洗浴王子讲AI5 小时前
GPT-5.4辅助算法设计与优化:从理论到实践的系统方法
人工智能·gpt·算法·chatgpt
超低空5 小时前
OpenClaw Windows 安装详细教程
人工智能·程序员·ai编程
恋猫de小郭6 小时前
你的代理归我了:AI 大模型恶意中间人攻击,钱包都被转走了
前端·人工智能·ai编程
yongyoudayee6 小时前
2026 AI CRM选型大比拼:四大架构路线实测对比
人工智能·架构
chushiyunen6 小时前
python rest请求、requests
开发语言·python