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)

最后运行不报错。

相关推荐
hboot1 小时前
AI工程师第三课 - 机器学习基础
python·scikit-learn·kaggle
冬奇Lab2 小时前
每日一个开源项目(第139篇):Voicebox - 本地运行的开源 ElevenLabs 替代品
人工智能·开源·资讯
冬奇Lab2 小时前
Skill 系列(03):Skill 设计范式——5 个模式让输出从混沌到可预测
人工智能·开源·agent
IT_陈寒4 小时前
Python搞不定字符串编码?这破玩意坑我两小时!
前端·人工智能·后端
大模型真好玩6 小时前
什么是Loop Engineering?最通俗易懂的Loop Engineering核心概念
人工智能·agent·deepseek
叁两6 小时前
前端转型AI Agent该如何学习?(前置篇)
前端·人工智能·node.js
顾林海6 小时前
Agent入门阶段-编程基础-Python:流程控制
python·agent·ai编程
LaiYoung_6 小时前
🎁 送你一套超好用超实用的 FE AI-Coding Skills
前端·人工智能·开源
ZzT9 小时前
怎么做才不会被 AI 替代?
人工智能·程序员