Skip to content

Gradient descent

Make a loss small by stepping against its gradient, and see the learning rate decide whether the steps crawl, glide, zig-zag or fly off.

Helps to know first
This topic

First, the picture

You stand on a hillside in fog and want to reach the valley floor. You can only feel the slope under your feet, so you take a step downhill, feel again, and step again. Below, a ball does that on a map of a loss, four times, with four step sizes. Before you play it: which run reaches the bottom first?

Step downhill, and choose the step size

The loss (θ₁ − 2)² + 5(θ₂ − 1)², from (−2, 3); four learning rates.

Contours of the loss, from 1 at the centre to 36; the bottom is at (2, 1). The ball starts at (−2, 3), where the loss is 36.

learning rate η
–
step
0
loss
36.0
0.00 / 31.70 s
Describe this picture

Two panels. The first is a contour map of the loss J(θ)=(θ1−2)2+5(θ2−1)2J(\boldsymbol\theta)=(\theta_1-2)^2+5(\theta_2-1)^2, with θ1\theta_1 across from −4 to 8 and θ2\theta_2 up from −3.5 to 5.5 at equal scales: six ellipses, where the loss is 1, 4, 9, 16, 25 and 36, round a small cross at the bottom, (2,1)(2,1). The second plots the loss against the step, from 0 to 30 across and from 0 to 40 up; a loss above 40 is drawn on the top edge with a small upward arrow. In the clip the contours draw outward and a ball appears at (−2,3)(-2,3), where the loss is 36. An arrow from the ball shows the gradient, (−8,20)(-8,20), pointing uphill; a second arrow shows the step, the opposite way, which does not point at the bottom. Then four runs, one after another, at 6 steps a second, each leaving a trail that dims and stays: learning rate 0.02 (tiny steps, and after 30 the loss is still 1.38), 0.08 (it glides in, loss 0.0005 after 30 steps), 0.17 (big steps overshoot the valley floor, yet this run reaches the bottom fastest) and 0.21 (each swing is larger than the last, and it leaves the map at step 9). The last frame lights the cross: the runs that settle all end on it, and an arrow from the end of the 0.02 run, still on its way, points to it. Readouts give the learning rate, the step and the loss, or “off the scale” and “off the map”. After the clip a slider, “Learning rate, η”, runs from 0.01 to 0.25 in steps of 0.01 and starts at 0.21; each change replays the run from the start. With reduced motion the four runs are shown as small maps side by side.

Walking downhill

The picture is a map seen from above. The two numbers a model adjusts are θ1\theta_1 and θ2\theta_2, its parameters; on these pages θ\theta is a parameter, never an angle. I write the pair as θ=(θ1,θ2)\boldsymbol\theta=(\theta_1,\theta_2). The loss J(θ)J(\boldsymbol\theta) measures how wrong the model is with those parameters, and each ellipse joins the points with one value of the loss. This loss is a bowl, lowest at (2,1)(2,1).

The slope under the ball is the gradient, from Partial derivatives and the gradient: the arrow of the two slopes,

∇J=(∂J∂θ1, ∂J∂θ2)=(2(θ1−2), 10(θ2−1)),\nabla J=\Big(\frac{\partial J}{\partial\theta_1},\ \frac{\partial J}{\partial\theta_2}\Big)=\big(2(\theta_1-2),\ 10(\theta_2-1)\big),

which points straight uphill. At the start, (−2,3)(-2,3), it is (−8,20)(-8,20). To go down, step the other way. One step is

θt+1=θt−η ∇J(θt).\boldsymbol\theta_{t+1}=\boldsymbol\theta_t-\eta\,\nabla J(\boldsymbol\theta_t).

Here tt counts the steps taken, not time, and the bold θt\boldsymbol\theta_t is where the ball stands after tt steps; the plain θ1\theta_1 and θ2\theta_2 stay its two entries. The positive number η\eta (eta) is the learning rate: how far to go per unit of slope. Repeating the step is gradient descent.

Now the answer to the question. The third run, at 0.17, zig-zags across the valley and still reaches the bottom first. The first crawls and is still short after 30 steps, the second glides in, and the fourth swings wider each time and leaves the map. Every run that settles ends on the same cross. The step size changes the path, never the bottom. Drag η\eta from 0.01 up and find where a run first leaves the map: 0.21.

Watch out

A bigger learning rate is not always faster. Past a limit set by the bowl, each step makes things worse.

One direction first

To see why, take a bowl with one parameter, J(θ)=a(θ−c)2J(\theta)=a(\theta-c)^2 with a>0a>0: lowest at θ=c\theta=c. Its slope is J′(θ)=2a(θ−c)J'(\theta)=2a(\theta-c), and here θt\theta_t is the one parameter after tt steps, so a step gives

θt+1−c=θt−c−2aη(θt−c)=(1−2aη)(θt−c).\theta_{t+1}-c=\theta_t-c-2a\eta(\theta_t-c)=(1-2a\eta)(\theta_t-c).

Every step multiplies the distance from the bottom by the same factor, 1−2aη1-2a\eta. If the factor is between −1 and 1, the distance shrinks to 0. That happens for 0<η<1/a0<\eta<1/a. A negative factor means the step jumps past the bottom to the other side, an overshoot. At η=1/a\eta=1/a the factor is −1, and the ball bounces between the same two points for ever. Beyond it the factor is below −1, and each swing is larger than the last: the run diverges.

One parameter, four step sizes

The loss (θ − 2)², from θ = −1; five steps at each learning rate.

η = 0.25: the factor 1 − 2η is 0.5. Each step goes against the slope of the tangent, by η times the slope.

learning rate η
0.25
factor 1 − 2η
0.5
step
0
θ
−1
Learning rate, η
0.00 / 29.20 s
Describe this picture

One panel: the curve J(θ)=(θ−2)2J(\theta)=(\theta-2)^2, with θ\theta across from −6 to 10. Under its θ\theta axis is a lane of five rows, one per step, where each step is drawn as an arrow from where it starts to where it lands. The clip plays all four learning rates in turn, 0.25, 0.75, 1 and 1.25, five steps each from θ=−1\theta=-1: at each step the tangent to the curve appears, then the step, and the point lands on the curve. Each run replaces the one before. Readouts give the learning rate, the factor 1−2η1-2\eta, the step and θ\theta. After the clip a four-way choice, “Learning rate, η”, replays any one of the runs from θ=−1\theta=-1. With reduced motion the four runs are shown side by side. At 0.25 the distance to the bottom halves each step. At 0.75 every step overshoots by half the distance. At 1 the point bounces between −1 and 5 for ever. At 1.25 the swings grow: the run leaves the frame at step 3, at θ=12.125\theta=12.125, and the caption says that by step 5 it would be at 24.8.

Doubling the loss doubles aa, so the largest safe learning rate halves. The step size and the steepness of the loss always go together.

Two directions, one step size

The bowl on the map is two one-direction bowls added together: a=1a=1 along θ1\theta_1 and a=5a=5 along θ2\theta_2. The gradient’s two entries do not mix, so each step multiplies

θ1−2 by 1−2η,θ2−1 by 1−10η.\theta_1-2\ \text{by}\ 1-2\eta,\qquad \theta_2-1\ \text{by}\ 1-10\eta.

Both factors must lie between −1 and 1. Along θ1\theta_1 that allows η<1\eta<1, along θ2\theta_2 only η<0.2\eta<0.2. The more sharply curved direction sets the limit: η<1/amax=0.2\eta<1/a_\text{max}=0.2, where amaxa_\text{max} is the larger of the two. The run at 0.21 is a little past it: its factor along θ2\theta_2 is −1.1, and each swing across the valley is 10 % larger than the last.

No single η\eta is perfect in both directions, because the two curvatures differ by a factor of 5. The best I can do is make the two factors equally small: 1−2η=−(1−10η)1-2\eta=-(1-10\eta) gives η=1/6\eta=1/6, with factor 2/32/3 along θ1\theta_1 and −2/3-2/3 along θ2\theta_2. The run at 0.17 is close to that: factors 0.66 and −0.7. Its zig-zag across the valley is the price of shrinking the slow direction fast. The run at 0.08 never zig-zags, but its slow factor, 0.84, leaves it behind.

The step arrow at the start also misses the bottom. The gradient is steep along θ2\theta_2, where the bowl is steep, so the step points mostly across the valley rather than along it to the bottom. Steepest down is not straight to the bottom unless the bowl is round or the ball sits on one of its axes.

In linear algebra · the bowl and its eigenvalues

The bowl here is the quadratic form of a positive definite matrix: its matrix of second derivatives, the Hessian, is diag(2,10)\mathrm{diag}(2,10). For any such bowl the safe step is η<2/λmax\eta<2/\lambda_\text{max}, with λmax\lambda_\text{max} the largest eigenvalue of the Hessian; here 2/10=0.22/10=0.2. Iterative solvers runs steepest descent on the same kind of bowl, and Conditioning names the ratio of the curvatures that causes the zig-zag.

When to stop

Gradient descent does not know it has arrived. Three stopping rules are common, and programs often use more than one:

  • the gradient is small: ∥∇J∥\lVert\nabla J\rVert falls below a chosen tolerance, since at the bottom of a bowl the gradient is zero;
  • the loss has stopped falling: it changes by less than a tolerance between steps;
  • a budget: a fixed number of steps, whatever happens.

On a bowl there is one bottom, and every run that settles finds it. A loss with several valleys is different: the bottom a run reaches can depend on where it starts. This page keeps to bowls.

Worked example

Take the map’s bowl with η=0.15\eta=0.15. The factors are 1−2(0.15)=0.71-2(0.15)=0.7 along θ1\theta_1 and 1−10(0.15)=−0.51-10(0.15)=-0.5 along θ2\theta_2.

Step 0. The ball starts at (−2,3)(-2,3), where J=42+5⋅22=36J=4^2+5\cdot2^2=36 and ∇J=(−8,20)\nabla J=(-8,20).

Step 1. It moves to (−2,3)−0.15 (−8,20)=(−0.8,0)(-2,3)-0.15\,(-8,20)=(-0.8,0), where J=2.82+5⋅12=12.84J=2.8^2+5\cdot1^2=12.84.

Step 2. There ∇J=(−5.6,−10)\nabla J=(-5.6,-10), so the ball moves to (0.04,1.5)(0.04,1.5), where J=1.962+5⋅0.52=5.0916J=1.96^2+5\cdot0.5^2=5.0916.

Step 3. There ∇J=(−3.92,5)\nabla J=(-3.92,5), so it moves to (0.628,0.75)(0.628,0.75), where J=1.3722+5⋅0.252=2.1949J=1.372^2+5\cdot0.25^2=2.1949.

The factor rule checks every step: θ1−2=−4(0.7)t\theta_1-2=-4(0.7)^t and θ2−1=2(−0.5)t\theta_2-1=2(-0.5)^t. So θ2\theta_2 zig-zags about 1, its distance halving each time, while θ1\theta_1 creeps up towards 2.

How many steps? For θ1\theta_1 to come within 0.01 of 2, I need 4(0.7)t<0.014(0.7)^t<0.01, that is 0.7t<0.00250.7^t<0.0025, so t>ln⁡0.0025/ln⁡0.7=16.8t>\ln0.0025/\ln0.7=16.8: 17 steps. By then θ2\theta_2 is within 0.0001 of 1.

The angle the step misses by. The first step points along (8,−20)(8,-20); the bottom lies along (4,−2)(4,-2) from the start. The cosine of the angle between them is

8⋅4+(−20)(−2)82+202 42+22=72464 20=0.7474,\frac{8\cdot4+(-20)(-2)}{\sqrt{8^2+20^2}\,\sqrt{4^2+2^2}}=\frac{72}{\sqrt{464}\,\sqrt{20}}=0.7474,

so the step points 41.6° (0.727 rad) away from the bottom.

One direction. With J=(θ−2)2J=(\theta-2)^2 from θ=−1\theta=-1: at η=0.25\eta=0.25 the points are −1, 0.5, 1.25, 1.625, and the distance halves; at η=1\eta=1 they are −1, 5, −1, 5, and the ball bounces; at η=1.25\eta=1.25 they are −1, 6.5, −4.75, 12.125, −13.1875, 24.78, and the distance is multiplied by 1.5 each step with its sign flipping, θt−2=−3(−1.5)t\theta_t-2=-3(-1.5)^t.

Try it yourself. With η=0.1\eta=0.1 on the map’s bowl, the factor along θ2\theta_2 is 0: the first step lands on θ2=1\theta_2=1 at once. With η=0.2\eta=0.2 it is −1, and θ2\theta_2 bounces between 3 and −1 for ever.

Where you’ll meet this

Fitting models. Nearly every model in this course is fitted by some form of gradient descent, from logistic regression to networks with many layers. The loss is rarely a perfect bowl, but the learning rate behaves as it does here.

Large least-squares problems. When a system is too large to solve by elimination, steepest descent and its relatives walk down the bowl of squared error, one matrix-vector product per step.

Adaptive filters. An echo canceller adjusts its taps a little at every sample, a small step down the bowl of squared error.

In signal processing · step size of an adaptive filter

Adaptive filters: LMS in the signal processing course takes one small downhill step per sample, with μ\mu for the step size where this page writes η\eta. Its too-big step diverges for the same reason as the run at 0.21.

Reference card

QuantityFormulaNotes
Updateθt+1=θt−η∇J(θt)\boldsymbol\theta_{t+1}=\boldsymbol\theta_t-\eta\nabla J(\boldsymbol\theta_t)η\eta the learning rate, tt the step
One-direction bowlθt+1−c=(1−2aη)(θt−c)\theta_{t+1}-c=(1-2a\eta)(\theta_t-c)for J=a(θ−c)2J=a(\theta-c)^2
Safe step0<η<1/a0<\eta<1/abounces at 1/a1/a, diverges beyond
Two directionsη<1/amax\eta<1/a_\text{max}the most sharply curved direction sets it
General bowlη<2/λmax\eta<2/\lambda_\text{max}λmax\lambda_\text{max}: largest eigenvalue of the Hessian
Best single step1−2η=−(1−10η)1-2\eta=-(1-10\eta), η=1/6\eta=1/6for (θ1−2)2+5(θ2−1)2(\theta_1-2)^2+5(\theta_2-1)^2
Stoppingsmall ∥∇J∥\lVert\nabla J\rVert, flat loss, or a step budget
Rulethe step size changes the path, not the bottomon a bowl

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look