解决: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())
相关推荐
oort12322 分钟前
吃上了自家的细糠,还挺丝滑,用起来手感还行,OortCloud发布新版AI编程平台,下载 OortCodex,Token多,免费薅
大数据·开发语言·人工智能·ai编程
AI小码28 分钟前
把动作「画」给视频世界模型,跨本体双向推演,李飞飞参与
大数据·人工智能·算法·ai·大模型·音视频·编程
AI多Agent协作实战派38 分钟前
AI多Agent协作系统实战(二十三):Agent读HEARTBEAT.md不读AGENTS.md——openclaw的文件加载之谜
前端·数据库·人工智能·uni-app
元Y亨H43 分钟前
Pandas 解析 Excel 导致的内存溢出(MemoryError)
python·excel
爱研究的小梁1 小时前
乾元通聚合路由及管理平台支持全面适配信创
网络·人工智能·信息与通信
水如烟1 小时前
孤能子视角:智能系统的持续学习——从关系场存续到模块化生长
人工智能
做一个码农都是奢望1 小时前
0722 LLM及其科研进展
人工智能
饼饼学习空间智能1 小时前
2026数字孪生国产化进入“交付验证期”:五类头部企业能力与项目选型分析
人工智能
金銀銅鐵2 小时前
[Python] 用 turtle 来绘制瑞典国旗
python
Albert Edison2 小时前
【GUI 自动化测试】Pywinauto 常见操作
自动化测试·python·pywinauto