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

SGD, momentum and Nesterov intuitionAdaGrad and RMSProp recognition and failure regimesAdam and AdamW state and update equations
1

Read

In this order, only the parts listed

0 of 6 done

  1. 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

  2. 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

  3. AdaGrad recognition and failure regimeD2L - optimization and regularization slice

    Sparse Features, Preconditioning and The Algorithm: accumulating denominator and its decay

  4. RMSProp recognition and failure regimeD2L - optimization and regularization slice

    The Algorithm: leaking average fix for the unbounded denominator

  5. Adam with bias correctionD2L - optimization and regularization slice

    The Algorithm and Yogi: bias correction and the divergence fix

  6. AdamW state and update equationsPyTorch torch.optim.AdamW reference

    AdamW algorithm box: decoupled decay step, moments, corrections and defaults

2

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 lr regardless 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_decay as if it were L2 strength. In plain Adam the penalty enters the moments; switch to AdamW for decoupled meaning.
3

Try it yourself

Closed-book, before checking anything

Closed-book, run one Adam step on scalar gradient 2 with 0.001, betas (0.9, 0.999) and 1e-8: 0.2, 0.004, corrected 2 and 4, update 0.001. Then state the general first-step rule: about times the sign of the gradient.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 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. 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. 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. 4.AdamW weight_decay 0.01, lr 0.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.

5

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