解决: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())
相关推荐
IT_陈寒1 小时前
Python搞不定字符串编码?这破玩意坑我两小时!
前端·人工智能·后端
大模型真好玩3 小时前
什么是Loop Engineering?最通俗易懂的Loop Engineering核心概念
人工智能·agent·deepseek
叁两3 小时前
前端转型AI Agent该如何学习?(前置篇)
前端·人工智能·node.js
顾林海3 小时前
Agent入门阶段-编程基础-Python:流程控制
python·agent·ai编程
LaiYoung_3 小时前
🎁 送你一套超好用超实用的 FE AI-Coding Skills
前端·人工智能·开源
ZzT6 小时前
怎么做才不会被 AI 替代?
人工智能·程序员
道友可好6 小时前
从今天开始:你的第一个 Harness Engineering 实践
前端·人工智能·后端
呱呱复呱呱6 小时前
Django CBV 源码解读:一个请求是怎么找到你的 get() 方法的
python·django
小姜前线技术7 小时前
AI回答代码块高亮加一键复制
人工智能
洛阳泰山7 小时前
从 0 到 1.6K Star:一个 Java 开源项目的增长复盘
人工智能·后端·开源