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)

最后运行不报错。

相关推荐
Mintopia几秒前
agent-cli 哪家强?别只看“能跑”,要看“能交付”
人工智能
kishu_iOS&AI3 分钟前
PyCharm 结合 uv 进行 AI 大模型开发
人工智能·pycharm·大模型·uv
币之互联万物6 分钟前
LLM 偏好算法解析:大语言模型内容收录倾向与 NEOXGEO 技术底蕴
人工智能·算法·语言模型
weixin_433179336 分钟前
python - 正则表达式Regex
python·正则表达式
Mintopia9 分钟前
衡量AI水平的六个核心指标:别再只看跑分了
人工智能
咚咚王者9 分钟前
人工智能之语言领域 自然语言处理 第十七章 多模态预训练模型
人工智能·自然语言处理
Riemann~~13 分钟前
ros2写一个可以修改参数的node
开发语言·python·ros2·机器人系统
盼小辉丶13 分钟前
PyTorch实战(36)——PyTorch自动机器学习
人工智能·pytorch·深度学习·自动机器学习
郝学胜-神的一滴13 分钟前
PyTorch 张量基础:零张量/一张量/指定值张量全解析
人工智能·pytorch·python
7yewh13 分钟前
Dense / 全连接层 / Gemm — 综合全局特征理解与运用
网络·人工智能·python·深度学习·cnn