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)

最后运行不报错。

相关推荐
m0_515098421 分钟前
如何创建哈希分区表_PARTITION BY HASH解决数据分布不均与热点块
jvm·数据库·python
天地沧海2 分钟前
Encoder-only、Decoder-only、Encoder-Decoder 到底长什么样
人工智能
Flying pigs~~2 分钟前
Dify平台入门指南:开源LLM应用开发平台深度解析
人工智能·开源·大模型·agent·dify·rag
PD我是你的真爱粉3 分钟前
Dify 与 LangGraph 图执行引擎原理对比:从定义层到运行时的架构拆解
人工智能·python·架构
donglianyou4 分钟前
Agent技术详解与实战
python·langchain·agent·langgraph
qq_372906934 分钟前
如何处理SQL循环逻辑_探索递归CTE实现复杂计算
jvm·数据库·python
林深时见鹿v5 分钟前
《后端开发全栈工具安装踩坑指南 & 经验沉淀手册》
java·人工智能·python·oracle
扬帆破浪5 分钟前
察元 WPS AI助手技术手记:从源码构建到各平台安装与上手
人工智能·wps
zero.cyx5 分钟前
更换Live2D模型具体步骤
人工智能·计算机视觉·语音识别
阿星AI工作室6 分钟前
Codex登录又崩了?零基础用CCSwitch秒连教程
人工智能