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)

最后运行不报错。

相关推荐
钱栈up8 小时前
GitHub热榜实测|Sherlock:一个用户名搜遍全网
python·开源·github
AAA修煤气灶刘哥8 小时前
Agent 面试题|上下文压缩怎么兼顾 token 开销和决策效果?
人工智能·后端·面试
geovindu8 小时前
python: Gradient Boosting
开发语言·后端·python·算法·boosting
AAA修煤气灶刘哥8 小时前
Agent 面试题|工具调用结果不符合预期,什么时候该重规划?
人工智能·后端·面试
卿卿的产品经理日记8 小时前
【AI产品经理实战】Day 48|AI 回答变短了,到底是谁的功劳?
人工智能·笔记·aigc·产品经理
用户3061429218119 小时前
codex 配置第三方模型:四个字段填完,随时能切回官方
人工智能
似水流年QC9 小时前
WebMP 入门:让 AI 更好地使用网页
人工智能
丘山录9 小时前
Mac mini 16G 跑 Ollama,8 GiB 就崩,剩下全靠换页
人工智能
老炮写标书9 小时前
把招标文件丢给大模型抽条款:JSON 输出、长文本切分和置信度兜底,我踩过的五个坑
人工智能
亦暖筑序9 小时前
准备升级 AgentScope Java 2.0.5?这几个会话坑先避开
人工智能·后端·agent