Skip to content

Chapter 16: Optimization

Training a model means minimizing an average loss using noisy gradients. Everything an optimizer does answers one of three questions. Which direction should the step go? That is the gradient, smoothed by momentum. How far should it go in each coordinate? That is the learning rate, which Adam scales per parameter. How should the step size change over time? That is the schedule. Convex theory gives the vocabulary and the convergence rates. Neural networks break its assumptions, but its intuitions survive, and the tools in this chapter are the ones every model in the series trains with.

Concepts from other sections (20)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

The chapter starts with plain gradient descent, where the theory is exact: on a quadratic, the step must stay under 2/L2/L, and the number of steps grows with the condition number κ=L/μ\kappa = L/\mu. The gap shrinks by about 1−1/κ1 - 1/\kappa per step. Next it wraps that update behind a common Optimizer protocol and adds momentum, Nesterov, and weight decay. Then it builds Adam and AdamW, where bias correction fixes the first steps and decoupled weight decay keeps regularization out of the adaptive scale. Last come learning-rate schedules and gradient clipping, which often decide whether a run trains at all. Two problem sets check the derivations by hand. The second one also covers Lagrangians, duality, and the KL-regularized objective behind direct preference optimization.

Optimizers are stateful objects, and that fact runs through the whole chapter. To resume a run bit for bit, the checkpoint must hold the moments, the step count, and the position in the schedule. Each implementation is checked against PyTorch’s trajectory, so recipes written for PyTorch carry over unchanged.

Learning objectives

  • Derive the stability limit η<2/L\eta < 2/L and the 1−1/κ1 - 1/\kappa rate of gradient descent, and use a backtracking line search when LL is unknown.
  • Implement SGD with momentum, Nesterov momentum, and coupled weight decay behind a resumable Optimizer protocol.
  • Implement Adam and AdamW with bias correction and decoupled weight decay, and match PyTorch step for step.
  • Build cosine, warmup-stable-decay, and Noam schedules and global-norm gradient clipping, with correct phase boundaries.
  • Solve KKT, duality, and KL-regularized problems by hand, including the derivation behind DPO.

You should be comfortable with gradients and Hessians from Calculus 3, eigenvalues from Linear Algebra, and geometric series from Precalculus, which explain both bias correction and warmup schedules. The gradients these optimizers consume come from Matrix Calculus and Autodiff. Its section Hessian-vector products and the recompute-versus-memory schedule supplies the curvature tools for going beyond first-order methods.

  1. Convexity, smoothness, gradient descent, and Armijo line search derives the step-size limit 2/L2/L and the 1−1/κ1 - 1/\kappa rate, then builds gradient descent and a backtracking line search that handle divergence and NaN correctly.
  2. The Optimizer protocol: SGD, momentum, Nesterov, weight decay defines the four-method interface every optimizer shares and builds a PyTorch-compatible SGD that resumes bit for bit.
  3. Adam and AdamW: bias correction and decoupled weight decay derives Adam’s two moving averages, the bias correction, and why weight decay must be decoupled, then implements both to match torch exactly.
  4. Learning-rate schedules (cosine, WSD, Noam) and gradient clipping builds three schedules as pure functions of the step, along with global-norm clipping that keeps the update direction.
  5. Optimization problem set, part a: convexity, GD rates, momentum, Adam has 17 problems with worked answers on convexity, gradient descent rates, momentum, and Adam.
  6. Optimization problem set, part b: constrained optimization, duality, and DPO has 18 optional problems on KKT conditions, duality, and the KL-regularized optimum that DPO relies on.

In AI Engineering, Training loop and the autograd bigram trains with these optimizers, and every later model in that book does too. Training and Post-Training in AI Engineering covers optimizer memory and large-scale training practice. Beyond this chapter, the same ideas extend to curvature. The top Hessian eigenvalue can be found by power iteration on Hessian-vector products, and gradient descent stays stable only while η<2/λmax⁡\eta < 2/\lambda_{\max}, which is the edge of stability. Muon orthogonalizes the momentum of 2D weights with Newton-Schulz iterations.