解决: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())
相关推荐
198******126344 小时前
2026深度解读:Work Agent长程任务的技术机制与落地能力
人工智能
老金带你玩AI5 小时前
别只让AI解释,让它做个你能看懂的东西
人工智能
iffy15 小时前
Deepseek hardness 桌面版 + ollama
人工智能
Xiaofeng36935 小时前
知漫剧入门科普:一站式工作台一句话生成漫剧
人工智能
“AI国潮设计-小江”5 小时前
《Python+SDXL实战:用ControlNet精准控制“英歌舞翻糖吐司”构图,附批量生成脚本》
开发语言·人工智能·python·prompt·aigc
wenwen_chao5 小时前
Python学习与开发资源合集:实用技巧与工具推荐
python
MobotStone5 小时前
万物皆插件:DeepSeek Harness 底层揭秘,脚手架如何蜕变为产品
人工智能
youdexiang6 小时前
Android录音软件时间关键词检索功能分析
java·人工智能
WUYOUGYLU6 小时前
大模型时代:人与智能的共同进化
人工智能
netkiller-BG7NYT6 小时前
基于图像识别的人工智能中医望诊方案
人工智能·百度