解决: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())
相关推荐
郑洁文4 分钟前
基于Python的Web命令执行漏洞自动化检测系统
前端·python·网络安全·自动化
人工智能AI技术8 分钟前
【VibeCoding系列教程07】 零代码平台——Bolt.new
人工智能
深蓝电商API10 分钟前
大模型 + 爬虫 = ?我用 AI 做了一个自适应反反爬引擎
人工智能·爬虫
新酱爱学习12 分钟前
手搓 10 个 Skill 后,我把重复劳动收敛成了一套零依赖 CLI 工具
前端·javascript·人工智能
yingjie11024 分钟前
Scanpy vs Seurat 深度对比:Python 与 R 的单细胞分析框架谁更强?
开发语言·python·r语言·生物信息学·单细胞转录组·seurat·scanpy
IT_陈寒29 分钟前
Python的线程池居然把我坑在了垃圾回收这块
前端·人工智能·后端
刘一说35 分钟前
AI科技热点日报 | 2026年6月1日
人工智能·科技
阿里云大数据AI技术35 分钟前
性能提升20倍:阿里云 Milvus 深度优化磁盘索引,重新定义亿级向量检索
人工智能
包子BI大数据1 小时前
3.openclaw小龙虾简单版安装教程
人工智能·python·ai
程序大视界1 小时前
【Python系列课程】Pandas(四):数据统计与排序——describe、sort_values、sample
开发语言·python·pandas