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)

最后运行不报错。

相关推荐
71777717 小时前
研发平台选哪家比较好:2026年主流平台对比与Gitee选型指南
人工智能·gitee
u13013017 小时前
GitHub 热榜项目:日榜(2026-09-21)
人工智能·github
2601_9556624617 小时前
短剧多人对白怎么配?2026年多角色配音工具对比
大数据·人工智能·音视频·语音识别
梦想很大很大17 小时前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
2601_9622186117 小时前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法
拉你进_教堂17 小时前
小龙虾技能之【Parkinson‘s & Epileptic Behavior Recognition Skill | 帕金森癫痫行为识别技能】简介
人工智能·计算机视觉·多模态大模型·openclaw小龙虾技能
Jasmin Tin Wei17 小时前
ai辅助逆向
python
慧都小项17 小时前
当边缘AI上产线:QtitanDocking 如何让工业 HMI 实现多视图协同
人工智能·qt·ui·边缘计算·qt6.3
全栈弄潮儿²⁰²⁴17 小时前
AI Agent 开发实战(30):限流、缓存与成本控制
人工智能·gpt·缓存·agent·限流·agi·成本控制