01Why it matters
How fast, right now?
A car's speedometer reads 50 km/h. Speed is distance over time, yet the needle shows a speed at a single instant, when no time passes. What can that number mean?
Machine learning asks the same question. A model's loss depends on millions of weights, and training asks of each: if this weight grew a tiny bit, how fast would the loss change, and which way? For one input the answer is a derivative; for all of them at once, the gradient. Both come from one move: measure a slope across a small gap, then let the gap shrink.
02Key parts
The pieces you will see
- secant slope
- The slope of the line through two points of a curve, and : rise over run.
- derivative f′(a)
- What the secant slopes settle on as shrinks to 0 from both sides: the curve's slope at the single point .
- tangent line
- The line through with slope : the closest straight fit to the curve near .
- partial derivative ∂f/∂x
- The slope as changes while every other input is held fixed.
- gradient ∇f
- All the partial derivatives stacked into a vector. It points the way rises fastest.
03See it work
Shrink the gap
sits on at . A slope needs two points, so take a second point a gap further along, measure the slope between them, and shrink .
For , algebra shows why:
At that is : every secant misses the tangent's slope by exactly . As shrinks to 0 from either side, settles on , so the derivative of is . In general,
and the limit must be the same from both sides. That is the needle's number: average speeds over shorter and shorter intervals, settling on one value.
Done at every point, this makes a function of its own, the Slope f′(x) graph above. Its sign says which way is going: negative left of 0, where falls, positive right of it. It is 0 where levels off, at peaks and valleys. Its size says how sensitive is: at , , so a small step in moves about 5.4 times as far; near 0, hardly moves.
04Predict
Your turn: a sharp corner
Now sits at the corner of , at 0. Predict how the secant slope changes as shrinks, whether a secant from the left agrees, then whether goes uphill or downhill as the sweep moves along.
Make 8 predictions: step forward and answer each question before the next step plays.
0 of 8 made05Try it
Slopes in code, and slopes on a map
A computer can't let reach 0, so it stops at a small and divides. This program measures the forward difference, the secant slope with to the right of , for at with = 0.5, 0.25 and 0.125.
def f(x):
return x * x
def secant_slope(a, h):
return (f(a + h) - f(a)) / h
a = 3
h = 0.5
while h >= 0.125:
print(secant_slope(a, h))
h = h / 2
Points on both sides of do better. The central difference, , averages the forward and backward slopes. On those are and , so the errors cancel exactly; in general what is left is proportional to . For at 0 (slope 1) with , forward gives 1.0050 and central 1.0000167.
So why not take ? Because floats have gaps (see floating point). For at 3 in float64, gives 5.99999996, gives 6.0005, and gives 0: floats near 3 are about apart, so rounds back to 3. Gradient checking tests backpropagation this way: nudge each weight about either way and compare the central difference of the loss with the backward pass's answer.
Nobody works out every derivative from the limit. A few rules, each proved once from it, cover this lesson's functions:
- Power rule: gives , so gives . A constant gives 0.
- Constant multiple and sum: and , so gives .
- Two special functions: is its own derivative, and gives (in radians).
The rules assume the curve is smooth where you use them.
A loss depends on many weights at once. For , the partial derivative is the ordinary derivative in with held fixed like a number, and the other way round. In the narrow valley they are and . Stacked, they make the gradient , which at is .
Walk in the direction of a unit vector , and the slope underfoot is the dot product . That is largest when points along , so the gradient points the way rises fastest, and its length is that steepest slope: about 15 at , where walking along gives just −0.35. At right angles to the slope is 0, which is along the contour line: the gradient crosses contour lines at right angles.
The tangent guess works in many dimensions too: for a small step . Step by and the guess falls by , so a small enough step against a nonzero gradient always goes downhill. Gradient descent is that one idea, repeated.
06Check yourself
Check yourself
Question 1 of 6
A curve has no gaps or jumps. At x = 3 its secant slopes settle on 2 from the right and 0.5 from the left. What is f′(3)?
Your answer comes back in Review in a few days, so it sticks.
Question 2 of 6
f(4) = 10 and f′(4) = −2. What is the best estimate of f(4.1)?
Question 3 of 6
With h = 0.2, a forward difference is off by 0.08 and a central difference by 0.004. Roughly how far off is each with h = 0.1?
Question 4 of 6
In float64, a forward difference for the slope of x² at 4 with h = 10⁻¹⁴ gives 7.816, not 8. What would most likely bring it closer?
Question 5 of 6
f(x, y) = x² + 3y. At (2, 1), how steep is the steepest climb, and how steep is the ground walking in the +y direction?
Question 6 of 6
On a map of f, the contour lines near a point P run east to west, f is higher to the north, and the highest point on the map lies to the north-east. Which way does ∇f point at P?
07Explain it back
Say it in your own words
What is the derivative f′(a), how can a computer estimate it, and what does the gradient tell you when f has several inputs?
08Recap
What to remember
- is the limit of the secant slope as , from both sides. At a corner, like or ReLU at 0, the sides disagree and there is none.
- The sign of says rising or falling, it is 0 at smooth peaks and valleys, and its size says how sensitive is. Near , .
- Rules: gives , constant factors stay, sums split, stays , and gives .
- The forward difference's error is proportional to , the central difference's to , and too small an loses digits to rounding.
- The gradient stacks the partial derivatives. It points the way rises fastest, its length is that slope, it crosses contour lines at right angles, and the slope along a unit vector is .
- Next: gradient descent walks against the gradient, and backpropagation computes it for every weight. Then try Backpropagate through a neuron by hand.