The hiker in the fog
Imagine you're on a mountainside in thick fog and you want to reach the valley. You can't see more than a metre, but you can feel the ground tilt under your boots. A reasonable strategy:
- Feel which direction slopes down most steeply.
- Take a step that way.
- Repeat until the ground feels flat.
That is the whole algorithm. In machine learning, the mountain is the loss landscape from Chamber 1, your position is the parameter vector , and your altitude is the loss . All we need is a precise way to "feel the slope".
Slopes, derivatives and gradients
The derivative measures how much the loss changes when you nudge a parameter a tiny amount:
Its sign tells you which way is uphill. If the derivative is positive, increasing increases the loss, so we should decrease . If it's negative, we should increase it. Either way, the rule is the same: move against the derivative.
Take the simplest possible loss, . Its derivative is . At the slope is : steeply uphill to the right, so we step left, towards the minimum at .
Real models have many parameters, so we collect one derivative per parameter into a vector, the gradient:
The update rule
Now the algorithm fits on one line:
- is where we are after steps.
- The learning rate (Greek eta) sets the step size.
- The minus sign sends us downhill.
We repeat until the gradient is close to zero. At the bottom of a valley the ground is flat, so the steps shrink to nothing on their own.
Gradient descent on starts at with learning rate . What is ?
A tiny proof: when does it converge?
Let's do something researchers do all the time: analyse an algorithm on a problem simple enough to solve exactly. For the update becomes
Everything depends on the factor . If its size is below , then shrinks to and we converge. If it's above , explodes.
| Learning rate | Factor | What happens |
|---|---|---|
| between and | smooth, steady convergence | |
| exactly | lands on the minimum in one step | |
| between and | zig-zags across the valley, but converges | |
| exactly | bounces between forever | |
| below | overshoots further every step and diverges |
So on this loss, gradient descent converges exactly when . You just proved a convergence theorem.
Play with it
Interactive lab
Gradient descent playground
Press Step to take one step downhill, or Run to watch it go.
step
0
θ
3
L(θ)
9
L′(θ)
6
Things to try:
- On the bowl, compare (crawls), (smooth), (zig-zags), (bounces forever) and (explodes). Check each against the table above.
- Find the single learning rate that reaches the bottom in exactly one step. (There's a hidden badge in it.)
- Switch to the bumpy landscape. From , small learning rates get trapped in the shallow valley: a local minimum. Large ones bounce chaotically. Plain gradient descent cannot escape… so read on and turn on momentum.
Momentum: rolling, not walking
Plain gradient descent is a hiker who stops dead after every step. Momentum turns the hiker into a heavy ball that remembers how fast it was moving:
The velocity accumulates past gradients, and (typically ) sets how much of it survives each step. Momentum smooths out zig-zags in narrow ravines and can carry the ball over small bumps. That's exactly what you need to escape the trap in the lab.
Modern optimisers build on this idea. Adam combines momentum with a separate, adaptive step size for every parameter, and it's the default choice across much of deep learning.
Adam: A Method for Stochastic OptimizationDiederik P. Kingma, Jimmy Ba · arXiv; ICLR 2015, 2014One of the most cited papers in machine learning. After Chamber 6, it makes a great first paper to read end to end.
Stochastic gradient descent
There's a practical catch. The loss averages over the entire dataset, so computing its exact gradient means touching every example, perhaps millions of them, for every single step.
Stochastic gradient descent (SGD) estimates the gradient from a small random mini-batch instead:
- The estimate is noisy but unbiased: on average it points the right way.
- Each step is far cheaper, so you can take many more of them.
- The noise can even help, jostling the parameters out of sharp or flat regions. It's also why real training curves look jagged.
Some vocabulary you'll see in every paper: the batch size is , one iteration is one update, and one epoch is one full pass through the training data.
Gradient descent for our line
Back to the coffee-and-code data from Chamber 1. Differentiating the mean squared error with respect to each parameter gives:
And now the machine can do what you did by hand in the lab:
import numpy as np
x = np.array([0.5, 1.5, 2.0, 3.0, 3.5, 4.5, 5.5, 6.5])
y = np.array([4.4, 5.2, 9.6, 10.5, 13.7, 13.8, 19.2, 21.1])
w, b = 1.0, 9.0 # the same poor start as the Chamber 1 lab
lr = 0.02
for step in range(2000):
err = w * x + b - y
grad_w = 2 * np.mean(err * x)
grad_b = 2 * np.mean(err)
w -= lr * grad_w
b -= lr * grad_b
print(round(w, 3), round(b, 3)) # 2.874 2.486, the least-squares line
Two research-flavoured observations. First, after 200 steps it's still at : this little problem has a condition number of about 68, so one direction converges slowly. Second, try lr = 0.07 and it explodes. The largest curvature here is about , so the safe limit is . The theory from the table above predicts real code.
Watch: gradient descent, visualised
Key takeaways
- The gradient points uphill, so gradient descent steps the other way: .
- The learning rate decides everything. On it converges iff ; in general the limit is .
- Local minima can trap plain gradient descent. Momentum helps the ball roll through.
- SGD trades exact gradients for cheap, noisy mini-batch estimates, and that trade powers modern deep learning.
Checkpoint
Prove it to the labyrinth
Answer every question to clear this chamber. First-try answers earn the most XP.
The gradient points in the direction of…
For the update is . What happens with ?
What is the main advantage of mini-batch (stochastic) gradient descent over full-batch gradient descent?
End of the chamber
Clear this chamber
- Questions in this chamber (0/4 solved)Next unsolved
- Bonus: Converge in five (+40 XP)
- Bonus: Escape the trap (+40 XP)