解耦权重衰减:AdamW 如何修正了一个被误叫了十年的名字
- 关联论文:1711.05101
- 作者:flyP
- 更新:2026-11-05
一句话结论
对 SGD 而言 L2 正则与 weight decay 等价,但对 Adam 这类自适应梯度算法不等价——因为 L2 的梯度被自适应缩放器一起归一化了;AdamW 把 weight decay 从梯度更新里拆出来独立施加,让 Adam 的泛化性能提升 15%(相对 test error),并把学习率与权重衰减两个超参彻底解耦。
解决什么真问题
这篇论文的起点是一个非常具体、非常有工程价值的困惑。
2017 年前后,Adam 已是训练前馈与循环网络的默认优化器,但一个刺眼的反差长期存在:在 CIFAR-10、CIFAR-100 这样的图像分类上,SOTA 依然由 SGD with momentum 拿下。Wilson et al. (2017) 更系统地指出,自适应梯度方法在图像分类、字符级语言建模、句法分析等多样任务上泛化不如 SGD with momentum。
原因众说纷纭:有的归咎于尖锐局部极小值(Keskar et al. 2016),有的认为是自适应梯度方法本身有内在缺陷(Wilson et al. 2017)。本文提出了一个完全不同的答案,而且这个答案出乎意料地朴素:
不是 Adam 不好,是主流框架里那个叫"weight decay"的东西,其实根本不是 weight decay。
具体说,几乎所有深度学习库(当时的 TensorFlow、Theano、torch 等)在实现"权重衰减"时,实际做的是把 L2 正则项 λ/2·‖θ‖² 加进 loss,然后算梯度——也就是把 λθ 混进了梯度里,再让 Adam 用它自己的自适应机制去缩放这个梯度。作者一针见血:
"While common implementations of these algorithms employ L2 regularization (often calling it 'weight decay' in what may be misleading due to the inequivalence we expose)"
于是问题变成了:同样的正则化意图,在 SGD 下有效,在 Adam 下是不是就失效了? 论文给出的答案与诊断都是肯定的。
核心方法:把衰减从梯度里摘出来
先把概念理清楚
论文回顾了 weight decay 的原始定义(Hanson & Pratt, 1988),权重按指数衰减:
θ_{t+1} = (1 − λ)·θ_t − α·∇f_t(θ_t)
而 L2 正则是把 λ'/2·‖θ‖² 加进 loss。
Proposition 1(对 SGD 两者等价):SGD 在带 weight decay λ 的 batch loss 上走过的步,与在无 weight decay 但 f_t^reg(θ) = f_t(θ) + (λ'/2)‖θ‖² 且 λ' = λ/α 的 loss 上走过的步完全相同。
但这里有个容易被跳过的陷阱,论文专门点出来:"if there is an overall best weight decay value λ, the best value of λ' is tightly coupled with the learning rate α"。也就是说,即便在 SGD 下,L2 系数与学习率也是耦合的——这正是后来 §4.2 会在 SGD 上也观察到"最优超参盆地沿对角线分布"的原因。
Proposition 2:对自适应梯度,不等价
设优化器在无 weight decay 时迭代为 θ_{t+1} ← θ_t − α·M_t·∇f_t(θ_t),带 weight decay 时为 θ_{t+1} ← (1−λ)θ_t − α·M_t·∇f_t(θ_t),其中 M_t ≠ k·I。那么:
不存在任何 L2 系数 λ',使得在
f_t + (λ'/2)‖θ‖²上跑该优化器,等价于在f_t上跑该优化器并加 weight decay λ。
直观解释(论文原文):
"when Adam is run on a loss function f plus L2 regularization, weights that tend to have large gradients in f do not get regularized as much as they would with decoupled weight decay, since the gradient of the regularizer gets scaled along with the gradient of f."
两类正则化的行为差异被总结为:
- L2 正则:loss 的梯度与正则项梯度一起被自适应缩放器归一化,于是梯度历史幅度大的权重
x(对应的s大)被相对更少地正则化。 - 解耦 weight decay:只有 loss 的梯度被自适应,衰减项独立施加,所有权重以同一速率 λ 被正则化,梯度大的权重实际上被正则化得更多。
Proposition 3:固定预条件子下的等价刻画
为了把差异说清楚,论文给了一个可解析的特例(固定预条件子 M_t = diag(s)^{-1}):
f_t^sreg(θ) = f_t(θ) + (λ'/(2α))·‖θ ⊙ √s‖², λ' = λ/α
即:解耦 weight decay 等价于一种按 √s 缩放的 L2 正则——s_i 大的参数被相对更多地正则化。
论文诚实标注了这个命题的局限:"this proposition does not directly apply to practical adaptive gradient algorithms, since these change the preconditioner matrix at every step",但它提供的直觉是对的。
AdamW 的实现
改动极小——把 weight decay 项从梯度里移出,直接作用在参数上:
# AdamW(Algorithm 2, line 12),与 Adam 的唯一差别在分解方式
g_t = ∇f_t(θ_{t-1}) # 注意:不再加 λθ
m_t = β1·m_{t-1} + (1−β1)·g_t
v_t = β2·v_{t-1} + (1−β2)·g_t²
m̂ = m_t/(1−β1^t); v̂ = v_t/(1−β2^t)
η_t = schedule_multiplier(t) # 可为 fixed / decay / warm restart
# Adam : θ_t = θ_{t-1} − η_t( α·m̂/(√v̂+ε) + λ·θ_{t-1} ) ← λθ 在被缩放的括号内
# AdamW: θ_t = θ_{t-1} − η_t·α·m̂/(√v̂+ε) − η_t·λ·θ_{t-1} ← 衰减项独立
一行公式的差别,效果却很大。对应的 SGD 版本是 SGDW:把 λθ 从梯度中移出后,直接在动量更新之后施加 −η_t·λ·θ_{t-1}(Algorithm 1 line 9)。
Bayesian filtering 视角(§3)
论文还借 Aitchison (2018) 的统一理论给了一个更优雅的解释:把随机优化看成贝叶斯滤波,状态转移先验取 P(θ_{t+1}|θ_t) = N((I−A)θ_t, Q),其中 A 是防止参数无界增长的正则化项。当 A = λ×I 时,这恰好就是解耦 weight decay——每步把均值估计乘以 (1−λ)。
论文特别注明:"While full credit for this theory goes to Aitchison, we summarize it here"——这是引用而非原创,做得比较克制。
关键实验与数据
实验设置
沿用 Gastaldi (2017) 的 Shake-Shake 3-branch ResNet 与代码(fb.resnet.torch)。基网络为 2×64d ResNet(11.6M 参数)与 2×96d ResNet(25.6M 参数)。batch size 恒为 128,CIFAR 用标准数据增强。另在 ImageNet32x32(120 万张 32×32 图)上验证。Gastaldi (2017) 用该设置在 CIFAR-10 上达到过 2.86% 的 SOTA error。
§4.1 三种学习率调度下的对比(Figure 1)
用 2×64d ResNet 在 CIFAR-10 上训 100 epoch,固定 lr / step-drop(在 epoch 30/60/80 下降)/ cosine annealing 三种调度,各配 L2 与解耦 weight decay 两版。
结论:解耦 weight decay 在所有学习率调度下都优于 L2 正则,且"larger differences for better learning rate schedules"——调度越好差距越大。另外"decoupled weight decay leads to a more separable hyperparameter search space",且"cosine annealing clearly outperforms the other learning rate schedules",后续实验因此全部改用 cosine annealing。
§4.2 超参解耦验证(Figure 2)
这是论文最关键的实验,它在 SGD 上也做了对照,所以结论不局限于 Adam。
SGD + L2(左上):最优超参盆地"is not aligned with the x-axis or y-axis but lies on the diagonal"。论文举了个具体例子:某个最优点 α = 1/2, λ = 1/8 × 0.001,单独改 α 或 λ 都会变差。论文顺势给出对历史经验的重新解读:"This coupling of initial learning rate and L2 regularization factor might have contributed to SGD's reputation of being very sensitive to its hyperparameter settings."
SGDW(右上):两者解耦。即使学习率没调好(比如取 1/1024),固定它只优化 weight decay 也能得到好值(1/4 × 0.001)。
Adam + L2(左下):论文的诊断落地了——"Adam did not benefit from it at all: its best results obtained for non-zero L2 regularization factors were comparable to the best ones obtained without the L2 regularization, i.e., when λ = 0"。在 Adam 里加 L2 正则几乎等于没加。这是全文最尖锐的一句话。
AdamW(右下):"AdamW largely decouples weight decay and learning rate. The results for the best hyperparameter settings were substantially better than the best ones of Adam with L2 regularization and rivaled those of SGD and SGDW."
§4.3 泛化性验证(Figure 3)
跑 1800 epoch 的长训练,固定初始学习率 0.001(Adam 的默认值),比较 Adam 的 12 组 L2 设置与 AdamW 的 7 组 normalized weight decay 设置。
关键在于作者不满足于"AdamW 更好"而去追问为什么更好:"Next, we investigated whether AdamW's better results were only due to better convergence or due to better generalization."
结论:AdamW 不仅训练损失更低("Adam and AdamW often coincided for the first half of the training run, AdamW often led to lower training loss and test errors"),而且"AdamW did not only yield better training loss but also yielded better generalization performance for similar training loss values"。这排除了"只是收敛更好"的平凡解释。ImageNet32x32 上结论一致。
§4.4 AdamWR 与量化收益(Figure 4)
把 warm restarts(Loshchilov & Hutter, 2016)加到 SGDW/AdamW 上,得到 SGDWR/AdamWR。AdamWR 在 CIFAR-10 与 ImageNet32x32 上把 AdamW 的进度"up to a factor of 10"地加快(以第一次 restart 为准)。
核心量化数字(原文):"For the default learning rate of 0.001, AdamW achieved 15% relative improvement in test error compared to Adam both on CIFAR-10 and ImageNet32x32."
论文补充:"These improvements closed most of the gap between Adam and SGDWR on CIFAR-10 and yielded comparable performance on ImageNet32x32."
§4.5 外部验证
论文列举了其他团队独立使用 AdamW 的结果,其中 Völker et al. (2018) 提供了直接对照:
- Deep4Net 上:AdamW 73.68% vs Adam 71.37%
- ResNet 上:AdamW 72.04% vs Adam 61.34%(论文称"statistically significantly higher")
Radford et al. (2018) 用 AdamW 训练 Transformer 架构,在多项 NLU benchmark 上取得新的 SOTA;Wang et al. (2018) 用 AdamW 做人脸检测,在 WIDER FACE 上"obtaining almost 10x faster predictions than the previous state of the art algorithms while achieving comparable performance"。Zhang et al. (2018) 则在 SGD / Adam / K-FAC 三种优化器、ResNet 与 VGG 两种架构上系统对比,报告解耦 weight decay "consistently outperformed L2 regularization in cases where they differ"。
落地状态
原文摘要即说明:"Our proposed decoupled weight decay has already been adopted by many researchers, and the community has implemented it in TensorFlow and PyTorch; the complete source code for our experiments is available at https://github.com/loshchil/AdamW-and-SGDW"。
亮点与局限
亮点
- 问题定位极准:把一个模糊的现象("Adam 泛化不如 SGD")归因到一个非常具体的实现细节(库的
weight_decay参数实际做的是 L2)。这个归因是可验证、可修复、且修复成本极低的。 - ** Proposition 2 给出了严格的不可能性证明**,不是"我们试了 AdamW 效果更好"的经验报告。这让结论具有数学约束力。
- 结论不限于 Adam:SGDW 的存在让"超参解耦"成为优化器设计的通用目标,并顺手解释了 SGD 为何被诟病超参敏感。
- 把"更好"追问成了"更好泛化":Figure 3 的设计(控制训练损失相同再比 test error)排除了最常见的平凡解释。
- 即时可用:算法伪代码清晰,代码开源,且 TensorFlow/PyTorch 当时就已实现。今天
torch.optim.AdamW就是这个方案。
局限(明文负面发现)
- 实验覆盖面窄:全部实验基于 CIFAR-10 / ImageNet32x32 / CIFAR-100,网络是 Shake-Shake ResNet。没有 ImageNet 全分辨率、没有 Transformer、没有 LLM。论文自己划定了范围:"We also demonstrate that our proposed decoupled weight decay has already been adopted by many researchers"——即用外部引用补上覆盖面,但那些结果不是本文实验得出的。
- 对现代 large-language-model 场景没有讨论:今天 weight decay 与 L2 在 LLM 训练中的差异、decoupled decay 与 warmup / LR schedule 的交互、wd 与 batch size 的耦合,本文完全没有触及。这直接限制了它的当下适用性。
- Adam 的许多其他"改进"没有被重新审视:既然 L2 在 Adam 下几乎失效,那 ResiDual、AMSGrad、AdamW 的各种变体、以及
weight_decay与amsgrad交互的修正(PyTorch 后来的decoupled_weight_decay与fully_decoupled之分)中的复杂关系,本文没有展开。 - Proposition 3 依赖固定预条件子,作者明说不适用于实际算法("does not directly apply to practical adaptive gradient algorithms")。它的解释力是启发式的,不是严格证明。
- "Adam 在 L2 下毫无收益"这个说法略强:论文的证据是"its best results obtained for non-zero L2 regularization factors were comparable to the best ones obtained without the L2 regularization",即在他们的搜索网格与设置下 L2 没有帮助——不等于 L2 对 Adam 在所有任务上都无效。
- λ 的最优值随训练时长变化:论文观察 "the larger the runtime/number of batch passes to be performed, the smaller the optimal weight decay",这意味着固定的
weight_decay超参在训练预算变化时需要重调——论文给了方向但没给通用公式。
对工程落地的启发
- 优先检查你的训练代码到底做了什么。很多框架里
weight_decay参数实现的是 L2 而不是解耦衰减。要确认:在 PyTorch 里weight_decay参数就是解耦的(AdamW),但如果你把正则项手动加进 loss 再传weight_decay=0,那你跑的就是论文批评的"伪 AdamW"。这个坑非常常见。 - 在 Adam 上做 L2 正则基本是无效功,这是论文最可操作的结论。如果你的 Adam 训练泛化不佳,把 L2 换成解耦 weight decay 是成本最低的尝试之一。
- 超参搜索空间的可分离性本身就是优化目标。AdamW/SGDW 的意义不只是最终精度更高,还包括调参更容易——这在超算力有限的现实里可能比那 15% 更值钱。
- 别忘了 cosine annealing:论文实测它明显优于 fixed 和 step-drop,且调度越好、解耦衰减的收益越大。把这两件事一起用。
- warm restart 能放大收益:AdamWR 把 AdamW 的速度提升最多 10 倍,在训练预算紧张时值得考虑。
- weight decay 需要随训练时长重调:跑得更久就该用更小的 λ。工程上可以按"计划训练多少 step"来反推 λ,而不是抄默认值。
- 但别把它当银弹:论文的证据全在小型 CNN + CIFAR 级别数据集上。现代 LLM 训练里,L2 与解耦 decay 的差异通常小于学习率调度、数据配比、batch size 这些因素;这篇的价值更多在"知道为什么 Adam 该用 AdamW",而不是"照抄超参"。
与同方向工作的关系
- vs. Adam (Kingma & Ba, 2014):AdamW 完全建立在 Adam 之上,唯一区别是 weight decay 的施加方式(Algorithm 1 vs 2 的 line 9 / line 12)。
- vs. Hanson & Pratt (1988):weight decay 的原始定义来源,论文直接引用其公式 (1)。
- vs. Aitchison (2018):提供了 §3 的贝叶斯滤波统一理论。论文明确把理论credit让给对方:"While full credit for this theory goes to Aitchison, we summarize it here to shed some light on why weight decay may be favored over L2 regularization"。这是少见的、把他人理论贡献讲清楚的写法。
- vs. Loshchilov & Hutter (2016) (SGDR / warm restarts):warm restart 与 cosine annealing 都来自作者自己前一篇工作,本文在其上构造了 SGDWR / AdamWR。
- vs. Wilson et al. (2017):那篇提出自适应梯度泛化更差的经验证据,本文则指出其中一个可修复的成因,并提供了一个修复方案。
- vs. Zhang et al. (2018):同期独立工作,在 SGD/Adam/K-FAC × ResNet/VGG 上对比,结论一致("consistently outperformed L2 regularization in cases where they differ")。两篇互补。
- vs. Decoupled Weight Decay 的后续演进(PyTorch 生态):PyTorch 后续把
weight_decay与decoupled_weight_decay分开,又引入了fully_decoupled(用于 LARS/LAMB 等层级式优化器),说明"解耦"本身还有层次——本文讨论的是最基础的一层。 - vs. 现代 LLM 训练实践:像 LAMB、LARS 这类把 weight decay 融入自适应更新结构的方法,某种程度上是"解耦"思路的延伸;而像 Chinchilla 那类工作则从数据-参数配比角度给出另一套正则化直觉。与本文的关系是"问题同源、答案不同"。
适合谁读
- 所有用 Adam/AdamW 训练的工程师:这是理解
weight_decay参数到底在做什么的必读材料,能直接省掉一批"调了半天超参没用的下午"。 - 优化器方向的研究者:Proposition 2 的不可能性证明和 Proposition 3 的 scale-adjusted 刻画,是这条线上被引用最多的技术贡献之一。
- 要复现或微调 LLM 训练栈的团队:能理解哪些"看起来一样的正则化"在不同优化器下语义完全不同。
- 在做 LLM/大模型训练基础设施的人:AdamWR 的 anytime performance 提升思路(10 倍加速)值得借鉴。
- 不太适合:只做推理部署、不碰训练过程的人;以及想找"最新正则化技巧"的人——这篇是 2017 年的工作,具体的现代配方需要结合后续文献。
待核查与存疑
- Figure 1/2 的具体超参网格数值(α 与 λ 的取值范围、最优点的具体坐标)以图形呈现,本次核对的 HTML 文本提取只给出了图注与正文描述,图中逐格数值未逐一抄录;文中引用的具体点(如
α = 1/2, λ = 1/8*0.001、1/1024、1/4*0.001)均来自 §4.2 正文文字。 - §4.4 的 "up to a factor of 10" 提速与 "15% relative improvement in test error" 均来自 Figure 4 的文字描述,未回查该图的逐点数值;论文标注这两项均有对应的 Supplement Figure 5/6。
- normalized weight decay 的"formal definition in Appendix B.1"未在本次核对的正文片段中展开,该 rescaling 公式的具体形式原文未在正文明确。
- Table 7(ViT 论文中的 ResNet + Adam/SGD 对比)与本篇无直接关系,未混入。
- PyTorch 生态中
decoupled_weight_decay/fully_decoupled的具体语义差异属框架后续演进,本文正文未涉及,属补充说明而非论文事实。