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)

最后运行不报错。

相关推荐
IT_陈寒7 小时前
Redis内存爆了,原来我漏掉了这个致命配置
前端·人工智能·后端
luckdewei8 小时前
FastAPI 资产管理系统实战:复杂 ORM 关联、Alembic 迁移与 N+1 查询优化
python
用户3521802454758 小时前
🎆从 Prompt 到 Skill:让 Spring AI Agent 学会"装新技能"
人工智能·spring boot·ai编程
米小虾9 小时前
手把手教你搭建第一个生产级AI Agent:从选型到实战的完整指南
人工智能·agent
任沫9 小时前
Agent之Function Call
javascript·人工智能·go
米小虾9 小时前
2026年AI Agent全面爆发:从开源生态到企业级应用的进化之路
人工智能·agent
用户6919026813399 小时前
Vibe Coding 开发项目的基本范式
人工智能·设计模式·代码规范
To_OC9 小时前
别再跟 AI 死磕 prompt 了,我写了个 Loop 让它自己改到满意为止
人工智能·aigc·agent
血小溅10 小时前
三大 AI 编码框架深度对比:GSD vs OpenSpec vs Superpowers
人工智能·后端
武子康13 小时前
调查研究-186 LangChain 和 LangGraph 的区别:从快速构建 Agent 到生产级工作流编排
人工智能·langchain·llm