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)

最后运行不报错。

相关推荐
一只肥瘫瘫15 小时前
编码器 PLL 角度与速度观测器原理
人工智能·算法
AI风控技术指南15 小时前
大模型安全围栏厂商评估:以数美科技为例拆解能力、架构和 POC 指标
大数据·网络·人工智能
weixin_5316708915 小时前
Interlude起来:久坐提醒软件为什么需要登录?有没有完全本地运行的Mac休息提醒软件?
人工智能·macos·swift
熊猫钓鱼>_>15 小时前
从“串数据“焦虑到 Space 自由,我用 Agent Bucket 智能体桶管游戏素材
开发语言·人工智能·游戏·agent·bucket·workbuddy·space
机器人猎头David15 小时前
机器视觉在机器人行业中有哪些实际应用?
人工智能·机器人·招聘·机器人猎头·猎头公司
2501_9369603615 小时前
1.3 RAG检索增强生成实战:手把手教你落地AI技术
人工智能
skywalk816315 小时前
atomic chat量化居然比unsloth 更小更强
人工智能
元岳数字人小元15 小时前
极简运维体系,数字人一体机适配中小场景升级
运维·人工智能·开源·人机交互·交互·源代码管理
qq_5137280415 小时前
Alert 原生弹窗处理
python