Maths toolkit7 / 17

Lesson 7 · Calculus

Many knobs: partial derivatives and the gradient

The problem

In the last chapter we tuned one weight, minutes per km w1w_1, and held the traffic weight at w2=6w_2 = 6. In practice you don't know either weight. You have two knobs, and the error depends on both.

The error is the same as before. For each of the three deliveries, take the miss (prediction minus real time), square it, and add up the squares:

E(w1,w2)=∑i(w⋅xi−ti)2E(w_1, w_2) = \sum_{i} (\mathbf{w} \cdot \mathbf{x}_i - t_i)^2

Here xi\mathbf{x}_i is delivery ii's features, (km, traffic level), and tit_i is its real time. The deliveries are A =(2,0)= (2, 0) in 8 minutes, B =(5,1)= (5, 1) in 26 and C =(3,2)= (3, 2) in 24. The error is zero at the true weights (4,6)(4, 6) and grows as you move away.

Take the guess w=(3,5)\mathbf{w} = (3, 5). The predictions are 6, 20 and 19. The misses are −2-2, −6-6 and −5-5, so

E(3,5)=4+36+25=65E(3, 5) = 4 + 36 + 25 = 65

Which way should you turn each knob, and by how much? We'll use this point, (3,5)(3, 5), all through the chapter.

A surface and its map

With two inputs, the error is a surface: a height EE above every point (w1,w2)(w_1, w_2). In the 3D view below it's drawn like a bowl. The bowl is long and narrow, so it looks more like a valley.

3D pictures are hard to read and impossible to draw for more inputs. So we use a contour plot instead, like a hiking map. Pick a height, cut the bowl at that height, and draw the ring you get on the flat map. Each ring is a contour line: every point on it has the same error.

w₁ →w₂ →012345678246810w₁ (minutes per km)w₂ (minutes per traffic level)(3, 5)

Orange: every (w₁, w₂) with error exactly 65. On the left it is a ring cut round the bowl at that height. On the right it is one contour line. Thin lines on the map are at E = 2, 10, 30, 65, 150, 300, 600; × marks the bottom, (4, 6).

The same error, drawn two ways. The orange ring is every (w₁, w₂) with the chosen error: a cut through the bowl on the left, one contour line on the right.

How to read the map:

  • The × marks the bottom of the valley, (4,6)(4, 6), where the error is 0.
  • Darker shading means a larger error.
  • Where contour lines crowd together, the ground is steep. Where they spread out, it's gentle.
  • The ovals are long and thin. The valley floor runs down and to the right. Raising w1w_1 while lowering w2w_2 keeps the error low.

The slider starts at 65. That orange oval passes through our guess (3,5)(3, 5), the purple dot. Use "Turn the bowl" to see the 3D view from another side.

Wiggle one knob at a time

We know how to find the slope of a curve. A surface isn't a curve, but we can make one. Freeze w2w_2 at 5 and change only w1w_1. That traces a curve across the surface, a slice. Its slope is how fast the error changes when you turn the w1w_1 knob alone.

That slope is the partial derivative of EE with respect to w1w_1. It's written with a curly d:

∂E∂w1\frac{\partial E}{\partial w_1}

Read it as "the change in EE per change in w1w_1, with every other input held still." Do the same with w2w_2 for ∂E/∂w2\partial E / \partial w_2.

Error E as w₁ changes (w₂ stays 5)

12345670200400600800w₁

Error E as w₂ changes (w₁ stays 3)

2468100200400600800w₂

At w = (3, 5) the error is 65

∂E/∂w₁ = -98 · ∂E/∂w₂ = -32

Nudge test (h = 0.01): -97.62 and -31.95

Each plot is a slice through the surface with one weight frozen. The tangent line's slope is a partial derivative.

At (3,5)(3, 5), both slopes are negative. Raising either weight lowers the error. But the w1w_1 slope, −98-98, is about three times steeper than the w2w_2 slope, −32-32.

Worked example

Both partial derivatives at (3, 5), by nudging

  1. Nudge w1w_1 by 0.010.01: E(3.01,5)=64.0238E(3.01, 5) = 64.0238. The change is −0.9762-0.9762, so ∂E/∂w1≈−0.9762/0.01≈−98\partial E / \partial w_1 \approx -0.9762 / 0.01 \approx -98.

You can also get them exactly with the rules. Each squared miss (w⋅xi−ti)2(\mathbf{w} \cdot \mathbf{x}_i - t_i)^2 changes at 2×miss×xi12 \times \text{miss} \times x_{i1} when only w1w_1 moves. Here xi1x_{i1} is the delivery's km. (The chain rule in the next chapter shows where that comes from.) Adding up over the three deliveries:

∂E∂w1=2((−2)(2)+(−6)(5)+(−5)(3))=2(−49)=−98∂E∂w2=2((−2)(0)+(−6)(1)+(−5)(2))=2(−16)=−32\begin{aligned} \frac{\partial E}{\partial w_1} &= 2\big((-2)(2) + (-6)(5) + (-5)(3)\big) = 2(-49) = -98 \\ \frac{\partial E}{\partial w_2} &= 2\big((-2)(0) + (-6)(1) + (-5)(2)\big) = 2(-16) = -32 \end{aligned}

Delivery B, the long trip with the biggest miss, does most of the pushing on w1w_1.

The gradient: one arrow for all the slopes

Put the partial derivatives together in a vector. That vector is the gradient, written ∇E\nabla E ("nabla E" or "grad E"):

∇E=(∂E∂w1,∂E∂w2)=(−98,−32) at (3,5)\nabla E = \left( \frac{\partial E}{\partial w_1}, \frac{\partial E}{\partial w_2} \right) = (-98, -32) \text{ at } (3, 5)

As an arrow on the map, the gradient does three things:

  • It points in the direction where the error rises fastest: straight uphill.
  • It crosses the contour line at a right angle.
  • Its length, 982+322≈103.1\sqrt{98^2 + 32^2} \approx 103.1, is the slope in that steepest direction.

Drag the point around the map. The orange arrow is the gradient there; its length grows with the slope but isn't to scale. The purple line is the contour through the point. The faint arrows show the gradient's direction across the whole map.

Turn the purple handle: the slope is 0 along the contour and largest at right angles to it.

012345678246810w₁ (minutes per km)w₂ (minutes per traffic level)

At w = (3, 5), E = 65

Gradient ∇E = (-98, -32), length 103.09 (arrow not to scale)

Walking 162° away from the gradient: slope -98

Downhill · steepest possible: ±103.09

Drag the point: its gradient (orange) crosses the purple contour at a right angle, pointing uphill. Drag the purple handle to choose a direction to walk.

Why a right angle? Along a contour line the error doesn't change, so the slope along it is zero. The gradient has to point where the error changes fastest, and that's as far from "no change" as it can get: straight across the line.

Predict first

You want to go downhill as fast as possible from (3, 5). Which way do you walk, relative to the orange gradient arrow?

DeeperWhy the gradient is the steepest directionThe slope in any direction is a dot product with the gradient. The dot product does the rest. · 4 min

Pick a direction to walk, as a unit vector u\mathbf{u} (length 1). The slope you feel in that direction is the directional derivative:

slope in direction u=∇E⋅u\text{slope in direction } \mathbf{u} = \nabla E \cdot \mathbf{u}

Why? Up close the surface is flat, like a tilted sheet of paper. A small step u\mathbf{u} changes the error by about ∂E∂w1u1\frac{\partial E}{\partial w_1} u_1 from the w1w_1 part, plus ∂E∂w2u2\frac{\partial E}{\partial w_2} u_2 from the w2w_2 part. That sum is the dot product.

Check it at (3,5)(3, 5) with three directions:

Direction u\mathbf{u}∇E⋅u\nabla E \cdot \mathbf{u}
(1,0)(1, 0): raise w1w_1 only−98-98
(0,1)(0, 1): raise w2w_2 only−32-32
(1,1)/2(1, 1)/\sqrt{2}: raise both equally(−98−32)/2≈−91.9(-98 - 32)/\sqrt{2} \approx -91.9

Now use the angle form of the dot product: ∇E⋅u=∥∇E∥cos⁡θ\nabla E \cdot \mathbf{u} = \lVert \nabla E \rVert \cos\theta, where θ\theta is the angle between the gradient and your direction. The length of ∇E\nabla E is fixed, so only cos⁡θ\cos\theta changes as you turn. It's largest, 1, at θ=0\theta = 0: walking along the gradient. It's −1-1 at θ=180°\theta = 180°: walking against it. It's 0 at 90°90°: along the contour.

So the steepest slope up is ∥∇E∥\lVert \nabla E \rVert, along the gradient, and the steepest slope down is −∥∇E∥-\lVert \nabla E \rVert, against it. The purple handle in the figure above shows exactly this.

Following the arrows downhill

If the gradient points uphill, stepping against it takes you downhill. Do it again from the new point, and again. Each step is

w←w−0.01×∇E(w)\mathbf{w} \leftarrow \mathbf{w} - 0.01 \times \nabla E(\mathbf{w})

Read the arrow ←\leftarrow as "becomes". The 0.010.01 keeps each step small, since the gradient is only a good guide near where you measured it. Press play.

012345678246810w₁ (minutes per km)w₂ (minutes per traffic level)

Step 0: w = (1, 2) · gradient (-316, -106)

Error E = 686

Each step: w ← w − 0.01 × gradient (orange arrow). Drag the purple start dot to try elsewhere.

Step 0 of 40
From the purple start, each step moves against the gradient. The path drops into the valley, then follows the valley floor towards (4, 6).

From (1,2)(1, 2) the error is 686. The gradient there is (−316,−106)(-316, -106), so the first step goes to (1+3.16,2+1.06)=(4.16,3.06)(1 + 3.16, 2 + 1.06) = (4.16, 3.06), where the error is 33.84. After 40 steps you reach about (4.22,5.27)(4.22, 5.27), with an error under 1.

Notice the two speeds. The first step drops straight into the valley, across the steep direction. Then the path crawls along the flat valley floor, where the gradient is small. Long, narrow valleys like this one slow learning down. This method is called gradient descent, and The ML fundamentals course covers it properly, including how to choose the step size (the learning rate).

Common misconceptions

"The gradient points to the minimum." It doesn't. It points straight uphill, at right angles to the contour, and its opposite points straight downhill. In a long, narrow valley, downhill is mostly across the valley, not towards the bottom. That's why the path in the walk figure turns a corner.

"A partial derivative ignores the other inputs." It holds them fixed; it doesn't ignore them. ∂E/∂w1\partial E / \partial w_1 at (3,5)(3, 5) is −98-98, but at (3,6)(3, 6) it's −76-76. The frozen value of w2w_2 changes the slice.

Recap

  • A function of two inputs is a surface. A contour plot maps it with lines of equal height.
  • A partial derivative is the slope when you change one input and freeze the rest.
  • The gradient ∇E\nabla E collects the partial derivatives into a vector. It points straight uphill, crosses the contours at right angles, and its length is the steepest slope.
  • Stepping against the gradient, a little at a time, walks you downhill.

Section 2 shows how to compute these in PyTorch.