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)

最后运行不报错。

相关推荐
机器之心1 分钟前
高德纳:「震惊!震惊!」Claude破解《计算机程序设计艺术》难题
人工智能·openai
石臻臻的杂货铺17 分钟前
GPT-5.4 发布:Computer Use 超越人类,Tool Search 让 Agent 用工具省了一半 token
人工智能
gustt20 分钟前
使用 LangChain 构建 AI 代理:自动化创建 React TodoList 应用
人工智能·llm·agent
碳基沙盒28 分钟前
OpenClaw 浏览器自动化配置完全指南
人工智能
Baihai_IDP44 分钟前
在 Anthropic 的这两年,我学会了 13 件事
人工智能·程序员·llm
IT_陈寒2 小时前
JavaScript这5个隐藏技巧,90%的开发者都不知道!
前端·人工智能·后端
IvanCodes2 小时前
Anthropic突然开火:中国大模型被指大规模蒸馏攻击
人工智能·llm
风象南3 小时前
AI 写代码效果差?大多数人第一步就错了
人工智能·后端
树獭非懒13 小时前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
KaneLogger13 小时前
【Agent】openclaw + opencode 打造助手 安装篇
人工智能·google·程序员