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)

最后运行不报错。

相关推荐
承渊政道14 小时前
【Python编程快速上手—让繁琐工作自动化】(列表:把数据排成队,从下标取值到深拷贝)
开发语言·python·pycharm·自动化·列表
GoldenSpider.AI14 小时前
讲透 Claude Code 的 Code Mode:原理、流程、用法与 24 轮实测
人工智能·ai编程·claude code
oak隔壁找我14 小时前
一款运行在 DSH(DeepSeek Harness)中的工作区文件管理插件
人工智能
大胖爱学习_MJ14 小时前
Agent 如何安全地拿数据库密码?基于 HashiCorp Vault 的动态凭证下发
经验分享·python
江屿风14 小时前
AI Agent架构全解析:从规划到记忆的智能跃迁
人工智能·笔记·架构·agent·c/s·mcp工作流
ZONGYUAN-ROOT14 小时前
【API配额】多源 LLM API 配额池化实战:轮换调度、变化量监控、故障自愈一套方案(Python 完整实现)
人工智能
syso_稻草人14 小时前
qwen-image-2.1 高清放大拆解:Qwen21LatentUpscale 潜空间 2x + denoise 0.35 修细节,12 组场景实测
人工智能·stable diffusion·prompt·comfyui·qwen-image2.1
tryCbest14 小时前
Python 爬虫从入门到实战(一):基础篇——从零写出第一个爬虫
开发语言·爬虫·python
无小道14 小时前
Python语句的特色
python·语句