7 routes42 sections324 units

Unit 3.5 · about 5 h

189 / 324

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.

  • derive
  • implement

You can now

  • 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 intuition
  • AdaGrad and RMSProp recognition and failure regimes
  • Adam and AdamW state and update equations

Study, in this order

6 steps

  1. Optimizers from GD to Adam
    Understanding 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 \(\eta/(1-\beta)\) effective step; Theoretical Analysis

  3. AdaGrad recognition and failure regime
    D2L - optimization and regularization slice

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

  4. RMSProp recognition and failure regime
    D2L - optimization and regularization slice

    The Algorithm: leaking average fix for the unbounded denominator

  5. Adam with bias correction
    D2L - optimization and regularization slice

    The Algorithm and Yogi: bias correction and the divergence fix

  6. AdamW state and update equations
    PyTorch 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 \(v \leftarrow \beta v + g\). Same direction, different scale: heavy-ball steps larger by \(1/(1-\beta)\).
  • 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 \(\beta_1 = 0.9\) the correction fades within a few dozen steps; for \(\beta_2 = 0.999\) it still multiplies \(v\) by about 10 at step 100 (\(1 - 0.999^{100}\) is about 0.095) and fades only after a few thousand steps.
  • AdamW applies weight decay before the adaptive step, outside \(m\) and \(v\). The decay rate then means the same thing at every coordinate regardless of gradient scale.

Reproduce closed-book

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

Traps

  • Mixing UDL and PyTorch momentum hyperparameters. \(\beta\) 0.9 means an effective step 10 times larger under heavy-ball than under UDL's averaged form at the same \(\eta\).
  • 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.

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

Self-test

  1. Heavy-ball momentum \(\beta\) 0.9, \(\eta\) 0.1, constant gradient \(g\). What is the steady-state step?

  2. Why does AdaGrad stall on dense deep-learning objectives, and what does RMSProp change?

  3. Adam's first step on gradient 2 with default betas and \(\eta\) 0.001 is about 0.001 regardless of gradient size. Show why.

  4. AdamW weight_decay 0.01, lr 0.001. How much does a weight of 0.5 shrink per step from decay alone?

Completion criterion

Stop when

Your from-scratch momentum and AdamW match the framework step for step on a quadratic, and you convert a \(\beta\) between UDL and heavy-ball forms without notes.

Mark passed, then open 3.6 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • Understanding Deep Learning - fitting, gradients and regularization
    Simon J. D. Princeprimarytheory
    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 slice
    D2L authorsrequiredimplementation
    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 reference
    PyTorchreferencereference
    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

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels