解决: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())
相关推荐
只猪侠GGBond4 分钟前
从“会诊断”到“能验证”:AI FaultLab 的修复验证闭环设计
人工智能
m4Rk_14 分钟前
【论文阅读】Agent 记忆机制(81):EMR——用情景记忆避免 Agent 在多步推理中反复绕圈
论文阅读·人工智能·学习·开源·github
Zldaisy3d15 分钟前
中科院力学所完成太空增减材制造失重飞行试验
人工智能·制造
帝王铠16 分钟前
【AI】一些AI时代的想法闲聊
人工智能
szxinmai主板定制专家24 分钟前
RK3588+FPGA异构架构|高速数据采集+边缘AI落地应用全解析
人工智能·嵌入式硬件·fpga开发·架构·zynq
李福春1 小时前
降SpringAI阿里第1掌-亢龙有悔-识势选型
人工智能·架构·腾讯云架构师同盟
李航19831 小时前
给自己的图形引擎,配上了AI渲染,做设计真是太方便了
人工智能·python·计算机视觉·ai·ai编程
L@ncor1 小时前
第六章 框架开发实践 · 学习笔记(AutoGen / AgentScope / CAMEL / LangGraph)
python·框架·autogen·langgraph·agentscope
Zhou1411361 小时前
SpringSecurity_02_授权与高级功能
开发语言·python
m0_466525291 小时前
数字认证参加第十四届用户体验大会,分享标准建设思路
大数据·人工智能·科技