You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Keras新Adam优化器报错:decay参数已弃用,求解决(人脸口罩检测)

解决Adam优化器decay参数弃用报错

问题原因

新版TensorFlow/Keras的Adam优化器已移除decay参数,直接传入该参数会触发报错。

两种修复方案

方案1:使用legacy版本的Adam优化器

直接替换为旧版兼容的Adam,可继续使用decay参数,修改后的代码如下:

# 导入legacy版本的Adam
from tensorflow.keras.optimizers.legacy import Adam

optimizer = Adam(lr=learning_rate, decay=learning_rate / epoch)

方案2:使用学习率调度器实现衰减(推荐)

这是新版Keras官方推荐的方式,通过学习率调度器定义衰减规则,代码修改如下:

from tensorflow.keras.optimizers import Adam
from tensorflow.keras.optimizers.schedules import ExponentialDecay

# 定义学习率衰减规则
initial_learning_rate = learning_rate
lr_schedule = ExponentialDecay(
    initial_learning_rate,
    decay_steps=len(training_data) // batch_size * epoch,  # 总训练步数:单轮步数×总轮数
    decay_rate=1 - (learning_rate / epoch)/initial_learning_rate,  # 匹配原decay的等价衰减率
    staircase=True
)

# 将调度器传入Adam作为学习率参数
optimizer = Adam(learning_rate=lr_schedule)

额外说明

方案2中,decay_steps和decay_rate的计算是为了近似你原本decay=learning_rate/epoch的效果,可根据实际训练需求调整参数数值。

内容的提问来源于stack exchange,提问作者Zool techno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 00:52:05