Skip to content
AriadneTechnology

The Outer Ring · Chamber 2 of 8

Gradient Descent: Rolling Downhill

Follow the slope. Derive the update rule, prove when it converges, then make it explode on purpose.

25 min 50 XP + 4 questions + 2 challengesVideoMathLabCode

In this chamber you will

  • Interpret derivatives and gradients as slopes
  • Derive the gradient descent update rule
  • Prove when gradient descent converges on a simple loss
  • Explain why mini-batches make training practical

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:

  1. Feel which direction slopes down most steeply.
  2. Take a step that way.
  3. 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 θ\theta, and your altitude is the loss L(θ)\mathcal{L}(\theta). 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:

dLdθ=lim⁡h→0L(θ+h)−L(θ)h\frac{d\mathcal{L}}{d\theta} = \lim_{h \to 0} \frac{\mathcal{L}(\theta + h) - \mathcal{L}(\theta)}{h}

Its sign tells you which way is uphill. If the derivative is positive, increasing θ\theta increases the loss, so we should decrease θ\theta. If it's negative, we should increase it. Either way, the rule is the same: move against the derivative.

Take the simplest possible loss, L(θ)=θ2\mathcal{L}(\theta) = \theta^2. Its derivative is L′(θ)=2θ\mathcal{L}'(\theta) = 2\theta. At θ=3\theta = 3 the slope is 66: steeply uphill to the right, so we step left, towards the minimum at 00.

Real models have many parameters, so we collect one derivative per parameter into a vector, the gradient:

∇θL=(∂L∂θ1,∂L∂θ2,…,∂L∂θd)\nabla_\theta \mathcal{L} = \left(\frac{\partial \mathcal{L}}{\partial \theta_1}, \frac{\partial \mathcal{L}}{\partial \theta_2}, \dots, \frac{\partial \mathcal{L}}{\partial \theta_d}\right)

The update rule

Now the algorithm fits on one line:

θt+1=θt−η ∇θL(θt)\theta_{t+1} = \theta_t - \eta\,\nabla_\theta \mathcal{L}(\theta_t)
  • θt\theta_t is where we are after tt steps.
  • The learning rate η\eta (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.

Quick check +20 XP

Gradient descent on L(θ)=θ2\mathcal{L}(\theta) = \theta^2 starts at θ0=3\theta_0 = 3 with learning rate η=0.1\eta = 0.1. What is θ1\theta_1?

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 L(θ)=θ2\mathcal{L}(\theta) = \theta^2 the update becomes

θt+1=θt−η⋅2θt=(1−2η) θt⟹θt=(1−2η)t θ0\theta_{t+1} = \theta_t - \eta \cdot 2\theta_t = (1 - 2\eta)\,\theta_t \quad\Longrightarrow\quad \theta_t = (1 - 2\eta)^t\,\theta_0

Everything depends on the factor 1−2η1 - 2\eta. If its size is below 11, then θt\theta_t shrinks to 00 and we converge. If it's above 11, θt\theta_t explodes.

Learning rateFactor 1−2η1 - 2\etaWhat happens
0<η<0.50 < \eta < 0.5between 00 and 11smooth, steady convergence
η=0.5\eta = 0.5exactly 00lands on the minimum in one step
0.5<η<10.5 < \eta < 1between −1-1 and 00zig-zags across the valley, but converges
η=1\eta = 1exactly −1-1bounces between ±θ0\pm\theta_0 forever
η>1\eta > 1below −1-1overshoots further every step and diverges

So on this loss, gradient descent converges exactly when 0<η<10 < \eta < 1. You just proved a convergence theorem.

Play with it

Interactive lab

Gradient descent playground

The gold ball is your parameter θ. Each step moves it by −η·L′(θ), against the slope (the dashed tangent). Try tiny, medium and huge learning rates. Then switch to the bumpy landscape.
-4-3-2-101234θL(θ) = θ²

Press Step to take one step downhill, or Run to watch it go.

step

0

θ

3

L(θ)

9

L′(θ)

6

Challenge: Converge in fiveOn the bowl, start at least 2.5 away from the minimum and reach |θ| < 0.01 within 5 steps.+40 XP
Challenge: Escape the trapOn the bumpy landscape, start from θ₀ ≥ 3 and settle in the deeper valley. (Momentum helps.)+40 XP

Things to try:

  1. On the bowl, compare η=0.01\eta = 0.01 (crawls), 0.30.3 (smooth), 0.90.9 (zig-zags), 11 (bounces forever) and 1.051.05 (explodes). Check each against the table above.
  2. Find the single learning rate that reaches the bottom in exactly one step. (There's a hidden badge in it.)
  3. Switch to the bumpy landscape. From θ0=3.5\theta_0 = 3.5, 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:

vt+1=β vt−η ∇θL(θt),θt+1=θt+vt+1v_{t+1} = \beta\, v_t - \eta\,\nabla_\theta \mathcal{L}(\theta_t), \qquad \theta_{t+1} = \theta_t + v_{t+1}

The velocity vv accumulates past gradients, and β\beta (typically 0.90.9) 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, 2014

One 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 BB instead:

∇θL≈1∣B∣∑i∈B∇θ ℓi\nabla_\theta \mathcal{L} \approx \frac{1}{|B|}\sum_{i \in B} \nabla_\theta\, \ell_i
  • 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 ∣B∣|B|, 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:

∂L∂w=2n∑i=1n(wxi+b−yi) xi,∂L∂b=2n∑i=1n(wxi+b−yi)\frac{\partial \mathcal{L}}{\partial w} = \frac{2}{n}\sum_{i=1}^{n}\big(w x_i + b - y_i\big)\,x_i, \qquad \frac{\partial \mathcal{L}}{\partial b} = \frac{2}{n}\sum_{i=1}^{n}\big(w x_i + b - y_i\big)

And now the machine can do what you did by hand in the lab:

Python
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 (2.64,3.51)(2.64, 3.51): 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 31.731.7, so the safe limit is 2/31.7≈0.0632/31.7 \approx 0.063. The theory from the table above predicts real code.

Watch: gradient descent, visualised

Gradient descent, how neural networks learn3Blue1Brown · 21 min

Key takeaways

  • The gradient ∇θL\nabla_\theta\mathcal{L} points uphill, so gradient descent steps the other way: θ←θ−η∇θL\theta \leftarrow \theta - \eta\nabla_\theta\mathcal{L}.
  • The learning rate decides everything. On θ2\theta^2 it converges iff 0<η<10 < \eta < 1; in general the limit is 2/λmax⁡2/\lambda_{\max}.
  • 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.

0/3
Question 1 of 3 +20 XP

The gradient ∇θL\nabla_\theta \mathcal{L} points in the direction of…

Question 2 of 3 +20 XP

For L(θ)=θ2\mathcal{L}(\theta) = \theta^2 the update is θt+1=(1−2η) θt\theta_{t+1} = (1 - 2\eta)\,\theta_t. What happens with η=1.2\eta = 1.2?

Question 3 of 3 +20 XP

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)
+50 XPGradientLearning RateGradient DescentStochastic Gradient Descent