解决: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())
相关推荐
刘立军5 分钟前
如何选择FAISS的索引类型
人工智能·算法·架构
@zulnger6 分钟前
python 学习笔记(多线程和多进程)
笔记·python·学习
Master_清欢16 分钟前
jupyter新增行数
ide·python·jupyter
gravity_w17 分钟前
Hugging Face使用指南
人工智能·经验分享·笔记·深度学习·语言模型·nlp
好奇龙猫18 分钟前
【人工智能学习-AI-MIT公开课第 19. 架构:GPS、SOAR、包容架构】
人工智能·学习·架构
特立独行的猫a28 分钟前
告别碎片化笔记:基于n8n-mcp的AI写作助手实战
人工智能·笔记·ai写作·n8n·n8n-mcp
羸弱的穷酸书生29 分钟前
python中各种数据类型的转换方法
python
oioihoii30 分钟前
构建高并发AI服务网关:C++与gRPC的工程实践
开发语言·c++·人工智能
范桂飓37 分钟前
大模型分布式训练框架 Megatron-LM
人工智能·分布式
D___H38 分钟前
Part8_编写自己的解释器
python