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)

最后运行不报错。

相关推荐
henrylin99996 小时前
RD-AGENT 第一讲 · AI 因子工厂是怎么运转的
人工智能
高洁016 小时前
具身智能中的世界模型训练
人工智能·python·深度学习·机器学习·transformer
无线通信科研笔记6 小时前
IEEE TVT 2026 论文精读与完整复现|相位误差如何重塑近场 RIS 的幅相响应
论文阅读·人工智能·python·算法·论文笔记
Devlive 开源社区6 小时前
AuthX 正式更名 GrantForge:我们重新做了一遍权限管理系统
大数据·人工智能·架构
朝朝辞暮i6 小时前
VLA 系统学习第 1 课:VLA 到底在干什么?
人工智能·python·计算机视觉·vla
鲲穹AI种草6 小时前
AI 壁纸生成工具怎么选?鲲穹 AI 壁纸工具功能实测与横向对比
人工智能·壁纸生成工具
科技林总6 小时前
向量与重排模型
人工智能
楚楚2516 小时前
2026企业AI办公工具选型指南:落地评估与效果衡量体系
大数据·人工智能
小马9267 小时前
2026年9月30日热点速览:AI智能体大战升级、房贷贴息新政落地、硬科技多点突破
人工智能·科技·chatgpt
2601_962885727 小时前
如何用 Python 自动识别股票的支撑位与压力位?
开发语言·python