解决: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 分钟前
「踩坑实录」给 LangGraph Agent 接两个 FastMCP,我被 langchain 偷偷改了工具名
前端·人工智能·后端
阿里云大数据AI技术3 分钟前
穹彻智能 × MaxCompute:具身智能 UMI 数据产线的全模态计算实践
人工智能
联犀8 分钟前
ai时代复杂项目的golang项目如何更好的与ai共舞:monorepo+gozero
开发语言·人工智能·golang
飞Link24 分钟前
灵巧手是什么?从机械结构、驱动方式到控制原理,认识机器人的精细操作能力
人工智能·python·机器人
阿里云大数据AI技术33 分钟前
从“扫文件”到“找数据行”:EMR Serverless StarRocks 支持 Paimon Global Index
人工智能
武子康1 小时前
AI通关了,游戏就算通过测试了吗?
人工智能·游戏·机器学习
Seiran1 小时前
CPU 突然打满,罪魁祸首是条用了三年的正则
人工智能
用户0332126663671 小时前
使用 Python 快速查找并高亮 PDF 中的文本
python
用户019027581611 小时前
用 pandas-ta 给 A 股算技术指标(配合 AlphaFeed)
python
mechanical_expert1 小时前
以SILVERTHIN SC047CP0为例计算等截面薄壁轴承摩擦损耗和启动扭矩
人工智能·机器人