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)

最后运行不报错。

相关推荐
一点一木19 小时前
🚀 2026 年 4 月 GitHub 十大热门项目排行榜 🔥
人工智能·github
做怪小疯子20 小时前
华为笔试0429
python·numpy
Warson_L20 小时前
Dictionary
python
淡海水20 小时前
【AI模型】常见问题与解决方案
人工智能·深度学习·机器学习
HIT_Weston21 小时前
65、【Agent】【OpenCode】用户对话提示词(费米估算)
人工智能·agent·opencode
njsgcs21 小时前
我的知识是以图片保存的,我的任务状态可能也与图片有关,我把100张知识图片丢给vlm实时分析吗
人工智能
星爷AG I21 小时前
20-4 长时工作记忆(AGI基础理论)
人工智能·agi
#卢松松#21 小时前
用秒悟(meoo)制作了一个GEO查询小工具。
人工智能·创业创新
zandy101121 小时前
Agentic BI 架构实战:当AI Agent接管数据建模、指标计算与可视化全链路
人工智能·架构
数字供应链安全产品选型21 小时前
关键领域清单+SBOM:834号令下软件供应链的“精准治理“逻辑与技术落地路径
人工智能·安全