All courses

Gradient descent and the learning rate

Apply the update θ ← θ − η∇𝓛. Predict what happens when η is too small or too large on a simple quadratic.

Builds on: Gradients and Jacobians

▶ Watch this lesson Start the whole course

What the animation says

One step downhill

Training lowers the loss by nudging the parameters, called theta. The gradient points where the loss rises fastest, but only right where you stand. So step the opposite way: theta becomes theta minus eta times the gradient. Eta is the learning rate. It scales how far each step goes. Too small and you crawl. Too big and you can jump right past the bottom.

Each step multiplies θ

Test it on the simplest bowl: loss equals two theta squared. Its slope is four theta. Start at theta equals one. With eta of zero point one, the step is zero point four, so theta lands at zero point six. Theta minus eta times four theta is just theta times one minus four eta. Here that factor is zero point six. Every step multiplies by the same factor: one, zero point six, zero point three six. Now try eta of zero point four. What does theta do from here?

Where it breaks

Minus zero point six: theta goes one, minus zero point six, zero point three six. It zigzags inward. The sign of the factor says flip or not. Its size says shrink or grow. In general the factor is one minus eta times the curvature, a. Past one over a, it flips. Past two over a, its size tops one, and theta blows up. Same bowl, so a is four. Tap the learning rate that makes theta blow up.

Two directions, one η

Real models have many directions, each with its own curvature. Here one direction has curvature ten, the other zero point one. One eta serves both. The steep one needs eta below two over ten: zero point two. At zero point one five, after a hundred steps, the steep direction is done. The flat one is still at zero point two two. So push eta to zero point two one, to speed up the flat one. What happens?

The ceiling and the crawl

The steep direction explodes to about fourteen thousand. The flat one only reached zero point one two. So gradient descent is one rule, repeated: step against the gradient, scaled by eta. On a bowl with curvature a, each step multiplies the distance to the bottom by one minus eta a. Past one over a, it zigzags. Past two over a, it blows up. The steepest direction sets the ceiling on eta. The flattest sets how long training takes.

From a gradient to an actual update

Training means lowering the loss $\mathcal{L}(\theta)$ by adjusting the parameters $\theta$. The gradient $\nabla\mathcal{L}$ tells you which way the loss rises fastest *at the current point*. Two questions remain. How do you turn that into a concrete change to $\theta$? And how big should each step be, given that the gradient only describes the loss close to where you stand?

Step downhill, but not too far

Move $\theta$ a little in the direction opposite the gradient, which is the steepest way down. The step is the slope scaled by a knob $\eta$, the learning rate. A small $\eta$ trusts the local slope only nearby, so the loss drops safely but slowly. A somewhat larger $\eta$ jumps past the bottom but lands lower, zigzagging inward. A much larger $\eta$ lands higher than you started, and each jump makes it worse.

The update rule and the quadratic test case

$$\theta \leftarrow \theta - \eta\,\nabla\mathcal{L}(\theta)$$ Take $\mathcal{L}(\theta)=\tfrac{a}{2}\theta^2$ with curvature $a>0$ (its second derivative: how fast the slope changes). The gradient is $a\theta$, so each step gives $$\theta_{\text{new}} = (1-\eta a)\,\theta.$$ - $0<\eta<1/a$: $\theta$ shrinks smoothly to 0. - $\eta=1/a$: $\theta$ reaches 0 in one step. - $1/a<\eta<2/a$: $\theta$ flips sign each step but still shrinks. - $\eta=2/a$: the factor is $-1$, so $\theta$ flips between $\pm\theta$ forever. - $\eta>2/a$: $|1-\eta a|>1$, so $\theta$ blows up.

Four learning rates on 𝓛 = 2θ²

Here $a=4$, the gradient is $4\theta$, and we start at $\theta=1$. The factor $1-4\eta$ multiplies $\theta$ at every step. - $\eta=0.1$: factor $0.6$ gives $1, 0.6, 0.36, \dots$ (slow, steady) - $\eta=0.25$: factor $0$ gives $1, 0$ (done in one step) - $\eta=0.4$: factor $-0.6$ gives $1, -0.6, 0.36, \dots$ (zigzags inward) - $\eta=0.6$: factor $-1.4$ gives $1, -1.4, 1.96, \dots$ (diverges) The limit is $2/a = 0.5$.

Steep and flat directions share one η

With several parameters, each direction can have its own curvature, but one $\eta$ serves all of them. Below, $\mathcal{L}=\tfrac12(10\theta_1^2+0.1\theta_2^2)$. The steepest direction caps $\eta$ below $2/a_{\max}=0.2$. Under that cap, the flattest direction crawls. If you push $\eta$ past the cap to speed up the flat direction, the steep one explodes.
import numpy as np
a = np.array([10.0, 0.1])        # curvature per direction
def run(eta, steps=100):
    th = np.array([1.0, 1.0])
    for _ in range(steps):
        th = th - eta * a * th   # grad of 0.5*sum(a*th**2)
    return th
print(run(0.15))  # ~[0.0, 0.22]: steep done, flat slow
print(run(0.21))  # ~[1.4e4, 0.12]: |1-2.1|>1, blows up

Remember

Gradient descent repeats $\theta \leftarrow \theta - \eta\nabla\mathcal{L}$. On a quadratic with curvature $a$, each step multiplies the distance to the minimum by $1-\eta a$. It converges only if $\eta<2/a$, and it oscillates once $\eta>1/a$. The steepest direction sets the ceiling on $\eta$, and the flattest direction sets how long training takes.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.