解决: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())
相关推荐
糖果店的幽灵几秒前
【langgraph 从入门到精通graphApi 篇】LangGraphAPI 方式调用 - 初识与核心概念
数据库·人工智能·langgraph
念雨思2 分钟前
HarmonyOS AI 应用开发实战:短视频选题灵感 —— AI 驱动的内容创作引擎
人工智能·学习·华为·harmonyos·鸿蒙
FourAu5 分钟前
2026 前端突围指南:从 ESR 边缘渲染到封装 Web AI SDK,聊聊 AI 时代的职业进化
前端·人工智能
前端开发江鸟8 分钟前
模型返回了 JSON,为什么还不能调用工具?Runtime 的结构化输出校验链路
人工智能
Urbano9 分钟前
卫衣生产工艺科普与智能自动化设备应用解析
大数据·运维·人工智能
吴梓穆17 分钟前
Python 基础 正则表达式
python
kp0000024 分钟前
Prompt注入攻击(Prompt Injection Attack)
人工智能·安全·网络安全·信息安全·ai安全
巧克力男孩dd33 分钟前
Python超典型练习题(第一次作业)
开发语言·python·算法
这张生成的图像能检测吗34 分钟前
(论文速度)RE-NET递归事件网络:时态知识图上的自回归结构推理
人工智能·图神经网络·时序模型
larance1 小时前
机器学习特征预处理之删移除无关特征
人工智能·机器学习·数据挖掘