Skip to content

1.6 Optimization

ready≈ 10.5 hoursNeeds: 1.3Unlocks: 2.1, 3.3, 6.4
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Step-size explorer)
  4. 4 Do
  5. 5 Check

Training a model means minimising a loss L(θ)L(\boldsymbol\theta) L(θ)L(\boldsymbol{\theta})lossThe scalar we minimise; lower is better. Often an average over training examples.Introduced in 1.6 Optimization. over the parameters θ\boldsymbol{\theta}parametersAll learnable parameters of a model, collected into one vector.Introduced in 1.6 Optimization. UDL uses φ (phi) for model parameters; CS229 and most papers use θ.. Gradient descent repeats one step: θ←θ−η∇L(θ)\boldsymbol\theta \leftarrow \boldsymbol\theta - \eta\nabla L(\boldsymbol\theta), with learning rate η\etalearning rateStep size in gradient descent: θ ← θ − η ∇L(θ).Introduced in 1.6 Optimization. UDL and CS229 write α for the learning rate; MML calls it the step-size γ (§7.1). (MML §7.1; UDL §6.1). Everything interesting is in the choice of η\eta.

Near a minimum, almost every smooth loss looks like a quadratic bowl, and on a quadratic you can solve gradient descent exactly. The derivation below shows each eigen-direction of the curvature A\mathbf{A} shrinking by ∣1−ηλi∣|1-\eta\lambda_i| per step. So gradient descent converges if and only if η<2/λmax⁡\eta < 2/\lambda_{\max}: the steepest direction sets the speed limit. The flattest direction then crawls at rate 1−ηλmin⁡1-\eta\lambda_{\min}. The ratio κ=λmax⁡/λmin⁡\kappa = \lambda_{\max}/\lambda_{\min}, the condition number, decides how slow that is (Goh, Why Momentum Really Works). The step-size explorer lets you see each regime: creep, oscillate, bounce, diverge.

A convex function has no local minima other than global ones, so gradient descent with a small enough step finds the best answer (MML §7.3). Neural network losses are not convex: where you start and how big your steps are decide which valley you end in. Try the quartic in the widget.

Two fixes you implement here. Momentum keeps a running sum of past gradients, v←βv+∇L\mathbf{v}\leftarrow\beta\mathbf{v}+\nabla L, and steps along v\mathbf{v}. In a long narrow valley, the zig-zags cancel and progress along the valley adds up (UDL §6.3; Goh). Backtracking line search starts with a big step and shrinks it until the loss drops “enough” (the Armijo condition), so no single η\eta has to work everywhere (backtracking line search; MML §7.1.1 motivates adapting the step size). Module 3.3 adds Adam, which also scales each coordinate separately.

When does gradient descent converge on a quadratic?step 1 of 5
  1. f(x)=12x⊤Ax−b⊤x,∇f=Ax−b,x∗=A−1bf(\mathbf{x}) = \tfrac12\mathbf{x}^\top\mathbf{A}\mathbf{x} - \mathbf{b}^\top\mathbf{x}, \quad \nabla f = \mathbf{A}\mathbf{x} - \mathbf{b}, \quad \mathbf{x}^* = \mathbf{A}^{-1}\mathbf{b}

Step-size explorer

Convergence, oscillation, divergence

Static view on small screens. Open this page on a laptop to interact.

on f = x², each step multiplies x by (1 − 2η) = 0.400: creeps monotonically to the minimumstable for η below 2/L = 1.00 final x = 0.0000

Slide η up from small: slow creep, then one-step convergence at η = 1/L, then oscillation, then divergence past 2/L. On the non-convex quartic, the start point and η decide which valley you land in.

Gradient descent iterates on 1-D functions (as a zig-zag over the curve) and on 2-D surfaces (as a path over contours). Change the learning rate on a log scale, plus the start point and the condition number. Keyboard: Tab through the controls; arrow keys adjust sliders; in 2-D, Tab to the start point and use arrows.

Implement gradient descent and momentum (returning whole trajectories), the stability limit on the learning rate for a quadratic, backtracking line search, and the Rosenbrock gradient derived by hand. The last test runs your line search all the way down the Rosenbrock valley.

Gradient descent stubs/m1_6_optimization.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m1_6_optimization.py and run mlc check 1.6. The tests are identical; import the results on the progress page.

Hints · gradient_descent 0/3 revealed
Hints · backtracking_line_search 0/3 revealed

Locally: mlc check 1.6.

1. The Hessian of a quadratic has eigenvalues 0.5 and 8. Gradient descent with a fixed learning rate converges for every rate below what limit?
2. On f(x) = x², gradient descent with η = 0.75 will…
3. Why is an ill-conditioned problem (large κ) slow for gradient descent?
4. What does momentum do in a long, narrow valley?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.