๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #103

AdamW

AdamW fixes a subtle but real bug in how standard Adam interacts with L2 regularization โ€” and has become the more commonly recommended default over plain Adam for exactly this reason, especially for training Transformers.

The Problem AdamW Fixes

In plain SGD, adding L2 regularization to the loss (\(L + \frac{\lambda}{2}\|\mathbf{w}\|^2\)) is mathematically equivalent to directly shrinking each weight by a fixed proportion every step ("weight decay") โ€” the two are the same thing. But in Adam, L2 regularization is applied by adding \(\lambda w\) to the gradient before it gets divided by \(\sqrt{\hat v_t}\) โ€” which means parameters with a large accumulated \(\hat v_t\) (frequently, strongly updated parameters) end up with their regularization effectively weakened, while parameters with small \(\hat v_t\) get regularized more strongly than intended. The clean SGD-era equivalence between "L2 regularization" and "weight decay" silently breaks under Adam's adaptive scaling.

Formula โ€” Decoupling Weight Decay from the Gradient

\[ w_{t+1} = w_t - \eta\left(\frac{\hat m_t}{\sqrt{\hat v_t}+\epsilon} + \lambda w_t\right) \]

The key change: the weight decay term \(\lambda w_t\) is added directly to the final update, completely separate from the adaptive-scaling machinery (\(\hat m_t\), \(\hat v_t\)) โ€” instead of being folded into the gradient before it gets adaptively scaled. This restores the clean, direct "shrink every weight by a fixed proportion" behavior that true weight decay is supposed to have, regardless of any individual parameter's gradient history.

Code

import torch.optim as optim

# AdamW takes weight_decay as its own explicit, decoupled argument
optimizer = optim.AdamW(model.parameters(), lr=0.001, weight_decay=0.01)

# Contrast with plain Adam's weight_decay, which folds decay into the
# gradient BEFORE adaptive scaling -- exactly the behavior AdamW avoids
optimizer_plain_adam = optim.Adam(model.parameters(), lr=0.001, weight_decay=0.01)

Complete Optimizer Comparison

OptimizerPer-Parameter Adaptive LR?Momentum?Key IdeaTypical Use
Batch GDNoNoExact gradient over the full datasetRare in deep learning; mostly theoretical/small-scale
SGD (mini-batch)NoNoNoisy but fast, frequent updatesSimple baselines; still competitive for some CV tasks with tuning
SGD + MomentumNoYesSmooths oscillation, accelerates consistent directionsClassic CNN training recipes (e.g. ResNet on ImageNet)
Nesterov MomentumNoYes (look-ahead)Anticipates momentum's next position before computing gradientSame as SGD+momentum, slightly improved
AdaGradYes (ever-shrinking)NoPer-parameter rate from cumulative squared gradientsSparse features; rarely used for long training runs
RMSPropYes (adaptive)NoDecaying average fixes AdaGrad's shrinking-rate problemRNNs, historically
AdamYesYesCombines momentum + RMSProp + bias correctionGeneral-purpose default across most architectures
AdamWYesYesAdam with correctly decoupled weight decayModern default, especially for Transformers/LLMs

Common Mistakes

  • Using plain Adam with a non-trivial weight_decay value, unaware that it's not behaving as true weight decay โ€” for any project relying meaningfully on regularization strength, AdamW is the more predictable, theoretically correct choice.
  • Assuming AdamW requires a completely different learning rate range from Adam โ€” in practice, similar learning rates work for both; the difference is specifically in how weight decay is applied, not the core adaptive update.

Interview Relevance

Q: "Why is AdamW generally preferred over plain Adam with weight_decay set, especially for training Transformers?" Plain Adam applies L2 regularization by folding it into the gradient before adaptive per-parameter scaling, which breaks the clean equivalence between L2 regularization and true weight decay that holds for SGD โ€” parameters get inconsistently regularized depending on their gradient history. AdamW decouples weight decay from the adaptive scaling entirely, applying it as a direct, consistent shrinkage to every weight โ€” restoring predictable regularization behavior, which has proven especially important for large-scale Transformer training.

Practice Question

Explain, in your own words, why a parameter with a large \(\hat v_t\) (heavily, frequently updated) ends up under-regularized when weight decay is folded into the gradient the way plain Adam does it.

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

AdamW โ€“ FAQs

Quick answers about learning AdamW in Deep Learning.

This free note from CodingNow 2.0 explains AdamW in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including AdamW, is 100% free with no signup required.
With focused practice, most students grasp AdamW in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now