7 routes42 sections324 units

Unit 5.3 · about 5 h

82 / 324

Adaptive optimizers, schedules, clipping and stopping

Adaptive methods carry history as well as parameters. A derivation of that history makes initialization, clipping order and restart errors visible before they affect a training run.

  • derive
  • implement

You can now

  • Derive Adam's zero-initialization bias correction from a finite geometric sum and state its expectation assumptions.
  • Distinguish adaptive preconditioning, coupled \(L_2\) penalties and decoupled weight decay.
  • Implement clipping, schedule state and optimizer restart with deterministic trajectory checks.

Covers

  • Adaptive optimizers, bias correction and weight decay
  • Learning-rate schedules, clipping and stopping

Study, in this order

4 steps

  1. Adaptive preconditioners and bias correction
    D2L - Optimization Algorithms

    Sections 12.7.2 to 12.7.3 (Adagrad), 12.8.1 (RMSProp), and 12.10.1 to 12.10.2 (Adam), equations 12.10.1 to 12.10.4 and the PyTorch adam cell

    Depth · Follow the adjacent Adagrad and RMSProp pages for their algorithms. Skip dataset training and Yogi; second moments are not centered variances.

  2. Schedule definitions
    D2L - Optimization Algorithms

    Sections 12.11.2 and 12.11.3.1 to 12.11.3.4: Schedulers, Factor, Multi Factor, Cosine and Warmup

    Depth · Read the schedule formulas and run the PyTorch learning-rate assignment and scheduler definitions only. Skip Fashion-MNIST training. Define whether your clock counts updates or epochs.

  3. Decoupled decay and optimizer state
    PyTorch optimizer and gradient-clipping contracts

    AdamW algorithm box, eps and weight_decay parameters, state_dict and load_state_dict

  4. Gradient-norm clipping contract
    PyTorch optimizer and gradient-clipping contracts

    clip_grad_norm_: concatenated-gradient norm, in-place modification, max_norm, norm_type and error_if_nonfinite

Focus on

  • With \(m_0=0\), \(m_t=(1-\beta_1)\sum_{i=1}^t\beta_1^{t-i}g_i\). Constant \(\mathbb{E}[g_i]=\bar{g}\) gives \(\mathbb{E}[m_t]=(1-\beta_1^t)\bar{g}\); constant \(\mathbb{E}[g_i^2]\) gives the analogous second-moment correction. Independence is not needed for these expectations.
  • Correction normalizes the finite weights. It does not make \(\hat{m}/\sqrt{\hat{v}}\) an unbiased gradient estimate when gradients change or the denominator is random.
  • Clipping projects \(g\) onto a Euclidean ball: \(g\min(1,c/\|g\|_2)\), with zero mapped to zero. Clip before updating moments. AdamW shrinks parameters separately; adding \(\lambda\theta\) to \(g\) changes both moments instead.
  • A schedule needs a clock and restart state. A small step caused by a tiny learning rate is not evidence of stationarity; check the un-clipped full-objective gradient and use the residual unit's stopping contract.

Reproduce closed-book

Derive the finite-sum correction by induction, checking \(t=1\). For constant \(g=(2,-1)\), \(\beta_1=0.9\) and \(\beta_2=0.999\), hand-compute \(m_3=(0.542,-0.271)\) and \(v_3=(0.011988004,0.002997001)\); corrected moments are (2,-1) and (4,1) in exact arithmetic. Check in NumPy float64 with infinity-norm tolerance 1e-12. Then run ten float64 updates on \(f(\theta)=(\theta_1^2+9\theta_2^2)/2\) from (1,-2), \(m_0=v_0=0\), \(\epsilon=10^{-6}\), decay=0.1, \(\mathit{lr}_t=0.01/(1+0.1(t-1))\), \(t=1,\ldots,10\). Clip \(g\) to Euclidean norm 1 using the exact formula above; update moments, then \(\theta=(1-\mathit{lr}_t\cdot 0.1)\theta-\mathit{lr}_t\hat{m}/(\sqrt{\hat{v}}+\epsilon)\). Result: (0.92127176,-1.91402438), allowing 1e-8 in infinity norm for the displayed rounding. Compare torch.optim.AdamW on CPU with float64, the same clipped gradients, betas=(0.9,0.999), eps=1e-6, weight_decay=0.1, foreach=False and the same per-update lr. On PyTorch 2.14.1 the measured infinity-norm difference is 2.22e-16; allow 1e-12. Do not substitute clip_grad_norm_ in this oracle comparison, because its stabilizing denominator changes the mathematical projection slightly. Save \(\theta\),\(m\),\(v\),\(t\) and the schedule clock after step five and check resumed versus uninterrupted final states within 1e-12. No random draws are needed.

Traps

  • Starting the bias-correction clock at zero, or incrementing it once per coordinate instead of once per update.
  • Putting \(\epsilon\) inside the square root while claiming the Adam formula with \(\epsilon\) outside it.
  • Restoring parameters but not moments and the schedule clock. The next update is then a different algorithmic state.

Practice

The section mini-project covers this: implement adaptive updates, clipping and update-indexed schedules; test finite-sum moments, checkpoint/resume and an un-clipped stationarity residual on the same deterministic quadratic.

Self-test

  1. What assumptions does the bias-correction derivation actually use?

  2. Why is Adam's vhat not the gradient variance?

  3. When does coupled \(L_2\) match decoupled decay?

  4. What is the norm-clipping solution for \(g=(3,4)\) and \(c=2\)?

Completion criterion

Stop when

You can derive the correction with its assumptions and pass moment, decay-placement and restart tests, including the 1e-12 infinity-norm oracle comparison.

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

Leads to

Resources for this unit

  • D2L - Optimization Algorithms
    D2L authorsrequiredimplementation
    Use the convexity, GD, SGD, minibatch, momentum, AdaGrad, RMSProp, Adam and scheduler sections for ML derivations and code.
  • PyTorch optimizer and gradient-clipping contracts
    PyTorchreferencereference
    Use AdamW's algorithm and clip_grad_norm_'s parameters to locate weight decay and clipping precisely. Inspect state_dict for optimizer restart; no network training is assigned.

Also in this section

  • Stanford EE364a - Convex Optimization
    Stanford Engineering Everywhereprimarycourse
    Follow lectures 1-9 and 15-16; use lecture 17 only for KKT systems. Leave full interior-point treatment for later.
  • Convex Optimization
    Boyd & Vandenbergherequiredtheory
    Read Chapters 1-5 and 9 with the course; use Chapter 10 only for equality-constrained Newton steps.
  • Fundamentals of Numerical Computation
    Driscoll & Braunrequirednumerics
    Study §§1.1-1.4, 2.7-2.8, 3.2-3.4 and 4.2-4.3 for floating point, conditioning, stability, least squares and iteration error, in the Python edition.
  • CVXPY basic and DCP tutorials
    CVXPY documentationsupportreference
    Use as the solver oracle after deriving a constrained problem and its KKT conditions by hand.
  • Proximal Algorithms
    Neal Parikh and Stephen Boydreferencetheory
    Read Sections 1.1, 4.2 and the elementwise soft-thresholding formula in 6.5.2, then the lasso formulation on p. 196. Skip 7.1.1's penalty-scaling and Gram-rewrite errors on p. 197; derive the update from 4.2 and 6.5.2.
  • SciPy minimize: BFGS
    SciPyreferencereference
    Read the BFGS method description and gtol, maxiter, c1 and c2 options. Supply an analytic gradient and compare residuals and evaluation counts, not just iterations.
  • PyTorch numerical type information
    PyTorchreferencereference
    Read torch.finfo's eps, max and smallest_normal fields and its subnormal note; inspect float64, float32, float16 and bfloat16 on CPU.
  • The Accuracy of Floating Point Summation
    Nicholas J. Highamreferencetheory
    Read Section 2's recursive-sum error bound and Section 3's pairwise and compensated summation algorithms. Leave the statistical error models and historical machine details aside.
  • Accurately computing running variance
    John D. Cooksupportimplementation
    Read the Welford mean and centered-sum recurrences. Prove the centered-sum invariant and translate the recurrence to NumPy; skip the C++ class.

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