TactivergeLite

Lite / Independent Engineering Research / Week 25

Gradient descent and the learning rate

Blindfolded on a hillside, you can only feel the slope under your feet. How big should each step downhill be?

About 45 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Gradient descent trains neural networks, fits models to data, tunes controllers and sizes components. Its single knob, the learning rate, decides whether it converges quickly, crawls or explodes, and that is a piece of maths you can predict.

Predict

Minimising f(x) = x² (gradient 2x) from x = 1 with learning rate η = 1.1. What happens?

  1. Converges to 0
  2. Oscillates with growing size
  3. Stops at x = 1
  4. Converges to 1.1
Guess first, then reveal

x ← x − 1.1·2x = −1.2x. The sign flips and the size grows 1.2× every step: divergence. For f = x², steps converge only if |1 − 2η| < 1, i.e. 0 < η < 1.

Discover

On the round bowl, find the learning rate that reaches the centre in one step. Switch to the narrow valley with b = 4: raise η until it diverges, and note the value. Then set b = 10 and see why a safe η crawls along the valley floor.

What the interactive shows (text version)

The explorer draws elliptical contours and the path of each gradient step. On f = ½(x² + 4y²) with η = 0.3, the y-coordinate shrinks by |1 − 1.2| = 0.2 per step and x by 0.7; at η = 0.6 the y-factor is |1 − 2.4| = 1.4 and the path diverges.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: Watch for how the cost surface's gradient tells each weight which way to move, and how big a nudge it gets.

The clearest picture of rolling downhill on a cost surface, and of why a step can land you in a local valley.

“Gradient descent, how neural networks learn | Deep Learning Chapter 2” by 3Blue1Brown (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

The update

The gradient ∇f points uphill, steepest. Gradient descent steps the other way: x ← x − η∇f(x). η, the learning rate or step size, scales the step. Repeat until the gradient is (nearly) zero or progress stalls.

Too small, too big, just right

Too small: correct but painfully slow. Too big: overshoot, oscillate, diverge. For a quadratic the boundary is exact: η < 2/L. In practice, start small, increase until the loss stops falling smoothly, then back off; or use a line search that picks η each step.

Narrow valleys

When curvature differs a lot between directions (an ill-conditioned problem), the steep direction limits η and the shallow direction then moves slowly: the zig-zag path in the explorer. Rescaling variables (preconditioning), momentum, or second-order methods fix this.

Explain it another way

Intuitive

Walking downhill in fog: big strides get you down fast on a gentle slope but can leap across a narrow gully and land higher on the other side.

Technical

For L-smooth convex f with η = 1/L, f(x_k) − f* ≤ L‖x₀ − x*‖² / (2k): sublinear O(1/k). For μ-strongly convex f, convergence is linear with rate (1 − μ/L) per step.

Mathematical

f(x) = a(x − c)² has f' = 2a(x − c), so x_{k+1} − c = (1 − 2aη)(x_k − c). Hence x_k = c + (1 − 2aη)^k (x₀ − c), stable for 0 < η < 1/a.

Applied

Least-squares calibration of a sensor, fitting a motor model to step-response data and training a classifier all reduce to minimising a smooth loss; for the quadratic ones, the condition number of the data matrix predicts how slowly plain gradient descent will go.

Derivation: When does gradient descent converge on a quadratic?
  1. Take f(x) = ½·a·x² with a > 0. Then f'(x) = a·x and the update is x_{k+1} = x_k − η·a·x_k = (1 − ηa)·x_k.

  2. So x_k = (1 − ηa)^k · x₀: a geometric sequence. It shrinks to 0 if and only if |1 − ηa| < 1, i.e. 0 < η < 2/a.

  3. Fastest: η = 1/a gives 1 − ηa = 0: one step. For 1/a < η < 2/a it overshoots and oscillates but still converges.

  4. In two dimensions, f = ½(a₁x² + a₂y²) gives two independent factors (1 − ηa₁) and (1 − ηa₂). Stability needs η < 2/max(aᵢ).

  5. With η limited by the steep direction, the shallow direction shrinks by (1 − a_min/a_max) per step at best. The condition number κ = a_max/a_min sets the pace: κ = 100 means about 100 steps per factor e.

  6. For a general smooth f, L (the largest curvature, the Lipschitz constant of the gradient) plays the role of a_max: η < 2/L.

Worked example 1

f(x) = 2(x − 3)², x₀ = 7, η = 0.1. Find x₁ and x₂.

f'(x) = 4(x − 3). x₁ = 7 − 0.1×16 = 5.4. x₂ = 5.4 − 0.1×9.6 = 4.44. Each step multiplies the distance to 3 by 1 − 0.4 = 0.6: 4 → 2.4 → 1.44 ✓.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 4. Gradient descent zig-zags slowly down a long, narrow valley. The most direct cure is…

Apply it

In the Code Lab, fit y = mx + c to ten noisy points by gradient descent on the mean squared error. Predict the largest stable η from the data (the largest eigenvalue of the 2×2 curvature matrix), then test your prediction just below and just above it.

Reflect

How would you choose a learning rate for a problem whose curvature you cannot compute?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Reduced precision: range, resolution and errorConvexity and local minima →