AttributeError: module ‘tensorflow‘ has no attribute ‘contrib‘解决办法

在TensorFlow2 环境下执行1.X版本的代码时报错:

AttributeError: module 'tensorflow' has no attribute 'contrib'

当然第一时间想到的是利用 如下代码 来解决问题(大部分情况都是这样),

python 复制代码
tf.compat.v1

但是又出现以下报错

AttributeError: module 'tensorflow.compat.v1' has no attribute 'contrib'

表示这个库中也没有该函数。

查阅资料以及实验发现,是选择库的问题,需要修改如下:

python 复制代码
# 原始代码
lstm_enc = tf.contrib.rnn.LSTMCell(num_units)

# 修改后代码
lstm_enc = tf.compat.v1.nn.rnn_cell.LSTMCell(num_units)

最后运行不报错。

相关推荐
硅谷秋水14 小时前
基于仿真的感知类人运动:苹果HALO 方法
人工智能·机器学习·计算机视觉·语言模型·机器人
撸码猿14 小时前
数据采集场景的语序点选验证码:90.2% 整词准确率的本地 OCR 实践
python·ocr
用户8425581308714 小时前
同一件事,两行日志差八小时
人工智能
敢敢のwings14 小时前
使用FACT给机器人补上“手感“
人工智能·机器人
用户03321266636714 小时前
使用 Python 在 Excel 中创建折线图和条形图
python·excel
电科_银尘15 小时前
【大模型算法与工程 32】全量微调与参数高效微调:如何选择
人工智能·算法·机器学习
深海鱼肝油ya15 小时前
Agent工具系统——Tool Calling & Schema
人工智能·大模型·agent·schema·智能体·工具调用·toolcalling
雨中飘荡的记忆15 小时前
一个人、两个 AI,我做出了一张可以点亮的中国美食地图
人工智能·后端
邂逅and回眸15 小时前
TinyML 入门:KB 级内存里怎么跑神经网络
人工智能·深度学习·神经网络
tellmewhoisi15 小时前
深度学习1番外篇:sigmoid为啥比Tanh收敛的慢
人工智能·深度学习