Unit 5 of 8 · about 5 hours · derive, implement
Optimizers from SGD to AdamW
The optimizer is the difference between a loss that falls and one that stalls or explodes. Its state also persists across restarts, so a wrong mental model corrupts both training and resume.
You'll be able to
- Write the SGD, heavy-ball momentum, Nesterov, AdaGrad, RMSProp, Adam and AdamW updates with their state.
- Derive the effective step of momentum and the bias correction of Adam.
- Implement momentum and AdamW from primitives and match reference trajectories on a quadratic.
Covers
Read
In this order, only the parts listed
0 of 6 done
- Optimizers from GD to AdamUnderstanding Deep Learning - fitting, gradients and regularization
Chapter 6, Sections 6.1 through 6.4 (pp. 77 to 90): gradient descent, SGD, momentum with Nesterov, and Adam with bias correction
- Momentum (heavy-ball form)D2L - optimization and regularization slice
The Momentum Method and Effective Sample Weight: heavy-ball update and the effective step; Theoretical Analysis
- AdaGrad recognition and failure regimeD2L - optimization and regularization slice
Sparse Features, Preconditioning and The Algorithm: accumulating denominator and its decay
- RMSProp recognition and failure regimeD2L - optimization and regularization slice
The Algorithm: leaking average fix for the unbounded denominator
- Adam with bias correctionD2L - optimization and regularization slice
The Algorithm and Yogi: bias correction and the divergence fix
- AdamW state and update equationsPyTorch torch.optim.AdamW reference
AdamW algorithm box: decoupled decay step, moments, corrections and defaults
Focus on
- UDL writes momentum as an exponential average of gradients, while D2L and PyTorch use heavy-ball . Same direction, different scale: heavy-ball steps larger by .
- AdaGrad divides by the root of all past squared gradients, so its steps decay toward zero even far from the optimum on dense problems. RMSProp's leaking average keeps the denominator current.
- Adam debiases both moments because zero initialization drags early averages toward zero. For the correction fades within a few dozen steps; for it still multiplies by about 10 at step 100 ( is about 0.095) and fades only after a few thousand steps.
- AdamW applies weight decay before the adaptive step, outside and . The decay rate then means the same thing at every coordinate regardless of gradient scale.
Common mistakes · 3
- Mixing UDL and PyTorch momentum hyperparameters. 0.9 means an effective step 10 times larger under heavy-ball than under UDL's averaged form at the same .
- Carrying a learning rate between SGD and Adam. Adam moves each coordinate by about
lrregardless of gradient scale, while an SGD step scales with the gradient, so a good SGD rate says nothing about a good Adam rate. - Tuning Adam's
weight_decayas if it were L2 strength. In plain Adam the penalty enters the moments; switch to AdamW for decoupled meaning.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Heavy-ball momentum 0.9, 0.1, constant gradient . What is the steady-state step?Answer
settles at , so the step is 0.1 times . The effective learning rate is 10 times the nominal one.
2.Why does AdaGrad stall on dense deep-learning objectives, and what does RMSProp change?Answer
Its denominator sums every past squared gradient and grows without bound, driving per-coordinate rates toward zero. RMSProp replaces the sum with a leaking average at 0.9, so old gradients stop counting.
3.Adam's first step on gradient 2 with default betas and 0.001 is about 0.001 regardless of gradient size. Show why.Answer
, ; corrected , ; update , about 0.001. Debiasing restores the gradient scale, so the step is times the sign.
4.AdamW
weight_decay0.01,lr0.001. How much does a weight of 0.5 shrink per step from decay alone?Answer. The shrinkage is proportional to the weight and independent of gradient history.
Practice
- Deep-ML 169: Implement AdamW Optimizer Step
Problem 169: implement the decoupled AdamW step; cross-check the decay placement against the PyTorch AdamW reference
This is the section's single Deep-ML drill; use changed constants and verify the decay sits outside the moments.
- D2L Adam: Exercises
Exercises: the bias-correction-free rewrite and the Adam-diverges and Yogi-converges case
You're done when
Your from-scratch momentum and AdamW match the framework step for step on a quadratic, and you convert a between UDL and heavy-ball forms without notes.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 9 more in the section
- Understanding Deep Learning - fitting, gradients and regularizationSimon J. D. Prince · primary
Use Chapters 6-9 and targeted parts of Chapter 20 for optimization, initialization, measurement, regularization and why training works. Re-derive the update rules you keep in the trainer.
- D2L - optimization and regularization sliceD2L authors · required
Use MLP implementation, dropout, deep generalization, numerical stability, optimization introduction, SGD, minibatch SGD, momentum, AdaGrad, RMSProp, Adam and learning-rate scheduler. Implement momentum, AdamW, initialization, dropout, one normalization, clipping and one schedule.
- PyTorch torch.optim.AdamW referencePyTorch · reference
Read the AdamW algorithm box for the decoupled weight-decay step, the bias-corrected moments and the defaults, and follow the Decoupled Weight Decay Regularization link for the argument.
Also in this section
- PyTorch - optimization and automatic mixed precision examplesPyTorch · reference
Check optimizer, scheduler, gradient clipping and AMP API semantics. Preserve the correct order for zeroing, scaling, backward, unscaling, clipping, stepping and updating.
- Deep-ML candidate - optimization and normalization problemsDeep-ML · optional
After the platform admission check, use at most one of 146 Momentum, 169 AdamW, 197 Global-Norm Clipping, 109 LayerNorm or 115 BatchNorm as a changed-prompt drill.
- TrenTorch - training mechanics questionsTrenTorch · support
Solve only the questions the units name: Weight Initialization, Layer Normalization, Label Smoothing, Gradient Accumulation, and Warmup and Cosine Decay. Read the tests first; they run in the browser IDE.
- PyTorch GroupNorm referencePyTorch · reference
Read how input channels are split into
num_groupsgroups and which axes each group normalizes; comparenum_groups1 againstnum_channels. - PyTorch RMSNorm referencePyTorch · reference
Read the RMS formula and which axes
normalized_shapecovers; note the missing mean centering and bias. - PyTorch CrossEntropyLoss referencePyTorch · reference
Read Shape, Parameters (
weight,ignore_index,reduction,label_smoothing) and both Examples; extend the class-index example withignore_indexand a classweight. - Cyclical Learning Rates for Training Neural Networks (Smith, 2017)arXiv · reference
Read the abstract and the range test: increase the rate linearly for a few epochs and read usable bounds off the loss curve.
- Deep Learning Tuning Playbook FAQ: debugging optimization failuresGoogle Research · reference
Read How can optimization failures be debugged and mitigated (identifying unstable workloads, warmup, clipping) and Batch normalization implementation details.
- Hands-On Machine Learning with Scikit-Learn and PyTorchAurelien Geron (O'Reilly, 2025) · support
Read the Chapter 11 sections named in the units for the PyTorch side of initialization, normalization, clipping and schedules.