解决: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 小时前
测控链路:从传感器到上位机
人工智能·嵌入式硬件·fpga开发
人工智能技术咨询.5 小时前
具身智能中的世界模型训练
人工智能
LaughingZhu5 小时前
Product Hunt 每日热榜 | 2026-10-06
人工智能·深度学习·神经网络·搜索引擎·百度
AOI小白新手上路5 小时前
AOI 缺陷检测复现实操指南:Anomalib + MVTec AD(glass)与 YOLOv8 + NEU-DET 两条路线
人工智能·深度学习·yolo
怕浪猫6 小时前
RAG 面试 6 连问,从原理到优化全部覆盖
python·算法·面试
henrylin99996 小时前
RD-AGENT 第一讲 · AI 因子工厂是怎么运转的
人工智能
高洁016 小时前
具身智能中的世界模型训练
人工智能·python·深度学习·机器学习·transformer
无线通信科研笔记6 小时前
IEEE TVT 2026 论文精读与完整复现|相位误差如何重塑近场 RIS 的幅相响应
论文阅读·人工智能·python·算法·论文笔记
Devlive 开源社区6 小时前
AuthX 正式更名 GrantForge:我们重新做了一遍权限管理系统
大数据·人工智能·架构
朝朝辞暮i6 小时前
VLA 系统学习第 1 课:VLA 到底在干什么?
人工智能·python·计算机视觉·vla