解决: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())
相关推荐
明月_清风18 分钟前
发现一个超系统的 AI Agent 学习地图 —— Agent Atlas 推荐
人工智能·后端·agent
百度Geek说1 小时前
商业客户端Harness资产管理与应用实践
人工智能
MacroZheng1 小时前
同事问我:"Claude Code经常失忆,不怕它把项目搞炸?",我:"怕,三个Markdown文件给它装个永不丢失的外置大脑!"
java·人工智能·后端
小酒星小杜1 小时前
一个人,也能做出有人设、能追更、可商业化的漫画内容 IP
人工智能·产品·设计
字节跳动视频云技术团队1 小时前
从出片到出海:AI MediaKit 重塑短剧全链路生产力
人工智能·音视频开发
一点一木1 小时前
憋了7周没动静,OpenClaw 2.0带着16000个PR杀回来了
人工智能·github
anyup2 小时前
DeepSeek Harness 从零上手:从认识到写出第一个插件
人工智能·openai·deepseek
学习星球3 小时前
AI Agent 成本工程实战:从 OpenAI Codex 的 8 个“烧 Token“Bug 学起
人工智能·设计模式·微信小程序
65岁退休Coder4 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
一点一木5 小时前
🚀 2026 年 8 月 GitHub 十大热门项目排行榜 🔥
人工智能·github