Lesson 7 · Calculus
Many knobs: partial derivatives and the gradient
The problem
In the last chapter we tuned one weight, minutes per km , and held the traffic weight at . 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:
Here is delivery 's features, (km, traffic level), and is its real time. The deliveries are A in 8 minutes, B in 26 and C in 24. The error is zero at the true weights and grows as you move away.
Take the guess . The predictions are 6, 20 and 19. The misses are , and , so
Which way should you turn each knob, and by how much? We'll use this point, , all through the chapter.
A surface and its map
With two inputs, the error is a surface: a height above every point . 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.
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).
How to read the map:
- The × marks the bottom of the valley, , 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 while lowering keeps the error low.
The slider starts at 65. That orange oval passes through our guess , 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 at 5 and change only . That traces a curve across the surface, a slice. Its slope is how fast the error changes when you turn the knob alone.
That slope is the partial derivative of with respect to . It's written with a curly d:
Read it as "the change in per change in , with every other input held still." Do the same with for .
Error E as w₁ changes (w₂ stays 5)
Error E as w₂ changes (w₁ stays 3)
At w = (3, 5) the error is 65
∂E/∂w₁ = -98 · ∂E/∂w₂ = -32
Nudge test (h = 0.01): -97.62 and -31.95
At , both slopes are negative. Raising either weight lowers the error. But the slope, , is about three times steeper than the slope, .
Worked example
Both partial derivatives at (3, 5), by nudging
Nudge by : . The change is , so .
You can also get them exactly with the rules. Each squared miss changes at when only moves. Here is the delivery's km. (The chain rule in the next chapter shows where that comes from.) Adding up over the three deliveries:
Delivery B, the long trip with the biggest miss, does most of the pushing on .
The gradient: one arrow for all the slopes
Put the partial derivatives together in a vector. That vector is the gradient, written ("nabla E" or "grad E"):
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, , 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.
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
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 (length 1). The slope you feel in that direction is the directional derivative:
Why? Up close the surface is flat, like a tilted sheet of paper. A small step changes the error by about from the part, plus from the part. That sum is the dot product.
Check it at with three directions:
| Direction | |
|---|---|
| : raise only | |
| : raise only | |
| : raise both equally |
Now use the angle form of the dot product: , where is the angle between the gradient and your direction. The length of is fixed, so only changes as you turn. It's largest, 1, at : walking along the gradient. It's at : walking against it. It's 0 at : along the contour.
So the steepest slope up is , along the gradient, and the steepest slope down is , 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
Read the arrow as "becomes". The keeps each step small, since the gradient is only a good guide near where you measured it. Press play.
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.
From the error is 686. The gradient there is , so the first step goes to , where the error is 33.84. After 40 steps you reach about , 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. at is , but at it's . The frozen value of 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 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.