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)

最后运行不报错。

相关推荐
恋猫de小郭22 分钟前
AI 正在造就你的「认知卸载」,但是时代如此
前端·人工智能·ai编程
飞哥数智坊8 小时前
我的“龙虾”罢工了!正好对比下GLM、MiniMax、Kimi 3家谁更香
人工智能
风象南9 小时前
很多人说,AI 让技术平权了,小白也能乱杀老师傅 ?
人工智能·后端
董董灿是个攻城狮10 小时前
大模型连载1:了解 Token
人工智能
花酒锄作田12 小时前
使用 pkgutil 实现动态插件系统
python
RoyLin13 小时前
沉睡三十年的标准:HTTP 402、生成式 UI 与智能体原生软件的时代
人工智能
needn15 小时前
TRAE为什么要发布SOLO版本?
人工智能·ai编程
毅航15 小时前
自然语言处理发展史:从规则、统计到深度学习
人工智能·后端
前端付豪15 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
ursazoo15 小时前
写了一份 7000字指南,让 AI 帮我消化每天的信息流
人工智能·开源·github