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)

最后运行不报错。

相关推荐
耿雨飞4 小时前
第三章:LangChain Classic vs. 新版 LangChain —— 架构演进与迁移指南
人工智能·架构·langchain
BizViewStudio4 小时前
甄选 2026:AI 重构新媒体代运营行业的三大核心变革与落地路径
大数据·人工智能·新媒体运营·媒体
俊哥V4 小时前
AI一周事件 · 2026年4月8日至4月14日
人工智能·ai
m0_377618235 小时前
Golang怎么连接MySQL数据库_Golang MySQL连接教程【总结】
jvm·数据库·python
GitCode官方5 小时前
G-Star Gathering Day 杭州站回顾
人工智能·开源·atomgit
LN花开富贵5 小时前
【ROS】鱼香ROS2学习笔记一
linux·笔记·python·学习·嵌入式·ros·agv
宇擎智脑科技5 小时前
开源 AI Agent 架构设计对比:Python 单体 vs TypeScript 插件化
人工智能·openclaw·hermes agent
weixin_586061465 小时前
C#怎么通过反射获取类属性_C#如何动态读取元数据【进阶】
jvm·数据库·python
Jurio.6 小时前
本机开发 + 多机执行的极简远端运行工具
linux·git·python·github·远程工作
skywalk81636 小时前
pytest测试的时候这是什么意思?Migrating <class ‘kotti.resources.File‘>
前端·python