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)

最后运行不报错。

相关推荐
IvanCodes13 分钟前
OpenClaw保姆级安装教程:windows&ubuntu
人工智能
Serverless社区1 小时前
AgentRun实践指南:Agent 的宝藏工具—All-In-One Sandbox
人工智能
AngelPP1 小时前
拆解 OpenClaw 上下文引擎:一个 AI Agent 是如何管理"记忆"的
人工智能
老纪的技术唠嗑局1 小时前
OpenClaw + 6 个 Agent 运转半个月,从聊天到干活的完整工程实践
人工智能
Guo白给1 小时前
openclaw飞书部署手册
人工智能
itpretty1 小时前
如何用 skill-creator 创建、测试和优化 skill
人工智能·claude
蓝戒札记1 小时前
效率神器 NotebookLM:构建高质量知识库的完整教程与落地最佳实践
人工智能
chaors1 小时前
Langchain入门到精通0x02:ICEL
人工智能·langchain·ai编程
会员源码网2 小时前
Python中生成器函数与普通函数的区别
python
吴佳浩2 小时前
什么是算力?
人工智能·pytorch·llm