Maths toolkit6 / 17

Lesson 6 · Calculus

How things change: derivatives

The problem

Back at the pizza shop, you predict each delivery's time with weights w=(w1,w2)\mathbf{w} = (w_1, w_2): minutes per km and minutes per traffic level. The prediction is w⋅x\mathbf{w} \cdot \mathbf{x}.

Here are three real deliveries:

DeliveryDistance (km)Traffic levelReal time (min)
A208
B5126
C3224

Say you already know the traffic weight is w2=6w_2 = 6, but you don't know w1w_1. You guess w1=3w_1 = 3. How wrong is that, and should you raise w1w_1 or lower it?

To score a guess, take each delivery's miss (prediction minus real time), square it, and add up the squares. Squaring makes every miss count as positive, and big misses count a lot. Call the total the error EE.

Worked example

The error for w₁ = 3 (with w₂ = 6)

  1. Predictions: A is 3×2+6×0=63 \times 2 + 6 \times 0 = 6. B is 3×5+6×1=213 \times 5 + 6 \times 1 = 21. C is 3×3+6×2=213 \times 3 + 6 \times 2 = 21.

The error is a function of w1w_1. Each miss is a multiple of (w1−4)(w_1 - 4): the misses are 2(w1−4)2(w_1 - 4), 5(w1−4)5(w_1 - 4) and 3(w1−4)3(w_1 - 4). Squaring and adding gives

E(w1)=(4+25+9)(w1−4)2=38 (w1−4)2E(w_1) = (4 + 25 + 9)(w_1 - 4)^2 = 38\,(w_1 - 4)^2

Its graph is a valley with the bottom at w1=4w_1 = 4, the true weight. This chapter is about one question: standing at w1=3w_1 = 3, which way is downhill, and how steep is it? The answer is the derivative. The next two chapters build on it to tune both weights at once.

Up close, curves are straight

A straight line has one slope: rise over run. If a line goes up 6 when you move 2 to the right, its slope is 6/2=36 / 2 = 3. A negative slope means the line goes down as you move right.

A curve has no single slope. It is steep in some places and flat in others. But zoom in on one point and something useful happens. Press play.

123450100200300w₁ (minutes per km)error Ew₁ = 3

Zoom ×1: showing w₁ from 1 to 5

Biggest gap from the dashed tangent line: 44% of the height

Zoomed out, the curve bends a lot: it even reaches the bottom of the valley at w₁ = 4.

Zoom 0 of 6
Each step halves the window around w₁ = 3. The gap between the curve and the dashed tangent line shrinks with it.

Zoomed out, the curve bends a lot. After a few zooms it looks like a straight line, and every further zoom makes it straighter. So near any one point, the curve has a slope after all: the slope of the line you see when you zoom in far enough. That line is the tangent line, drawn dashed in the figure.

This holds for most curves in this book. ReLU, which you'll meet later, has a corner at 0, and there it never straightens out.

Measuring the slope with a nudge

How do you measure the slope of the line you'd see? Pick a second point a small step hh away. Then take rise over run between the two points:

slope≈E(3+h)−E(3)h\text{slope} \approx \frac{E(3 + h) - E(3)}{h}

The straight line through the two points is called a secant line. The smaller hh is, the closer the two points are, and the closer the secant gets to the tangent. Press play to shrink hh.

1.522.533.544.555.5050100150w₁ (minutes per km)error Ew₁ + hw₁ = 3

Nudge h = 2: E goes from 38 to 38

Secant slope = (38 − 38) ÷ 2 = 0

Tangent slope (dashed) = −76 · off by 76

Nudge 0 of 5
The secant (orange) joins w₁ = 3 to w₁ = 3 + h. As h shrinks, it swings onto the tangent (dashed).
Nudge hhE(3+h)E(3 + h)Secant slope
2380
10−38
0.130.78−72.2
0.0137.2438−75.62

With h=2h = 2 the secant is flat, which is badly wrong. It jumps right over the valley to the other side, where the error is 38 again. As hh shrinks, the estimates settle towards −76-76. The number they settle on, −76-76, is called the limit.

That limit is the derivative. It is the slope of the tangent line at that point:

f′(x)=dfdx=lim⁡h→0f(x+h)−f(x)hf'(x) = \frac{df}{dx} = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}

Read it as: nudge xx by hh and divide the change in ff by hh. Then see what number that settles on as hh shrinks to zero. The symbol lim⁡h→0\lim_{h \to 0} reads "the limit as hh goes to 0". Both f′(x)f'(x) and dfdx\frac{df}{dx} mean the same thing. The second one reads as "a tiny change in ff per tiny change in xx."

For our error, E′(3)=−76E'(3) = -76. That says: near w1=3w_1 = 3, the error drops about 76 for each unit you raise w1w_1. So a small nudge of 0.010.01 should lower the error by about 0.760.76. Check: E(3.01)=37.2438E(3.01) = 37.2438, which is 0.75620.7562 lower than 38.

DeeperLimits without epsilonsWhat 'as h shrinks to zero' really means, without the formal machinery. · 3 min

You can't set h=0h = 0 in the nudge formula, because then you'd divide 0 by 0. The limit avoids that. It asks what the formula gets close to as hh gets close to 0, without ever using h=0h = 0 itself.

For our curve you can see it exactly. Expand E(3+h)=38(h−1)2=38−76h+38h2E(3 + h) = 38(h - 1)^2 = 38 - 76h + 38h^2. Then

E(3+h)−E(3)h=−76h+38h2h=−76+38h\frac{E(3 + h) - E(3)}{h} = \frac{-76h + 38h^2}{h} = -76 + 38h

The hh on the bottom cancels. What's left is −76-76 plus an error of 38h38h. That error shrinks in step with hh: at h=0.1h = 0.1 it is 3.83.8, and at h=0.01h = 0.01 it is 0.380.38. So the estimate settles on −76-76, and nothing breaks.

The formal version (with ε\varepsilon and δ\delta) makes "gets close" precise. You won't need it in this book.

Predict first

You stand at the very bottom of the error valley, at w₁ = 4. What is the slope there?

Which way to nudge

The sign of the derivative tells you which way is downhill.

  • Negative slope: the curve goes down as you move right. Raise the input to lower the output.
  • Positive slope: the curve goes up as you move right. Lower the input.
  • Zero slope: you're on flat ground, perhaps at the bottom.

At w1=3w_1 = 3 the slope is −76-76, so you should raise w1w_1. That's right: the true value is 4. The size of the slope says how steep the ground is. Far from the bottom, at w1=2w_1 = 2, a nudge of 0.01 gives a slope of about −151.6-151.6: twice as steep.

This one idea, "move against the slope," drives how machine learning models learn. It's called gradient descent, and the ML fundamentals course covers it properly.

The derivative rules

You don't want to run the nudge test every time. For the functions this book uses, a few derivative rules give exact answers:

Function f(x)f(x)Derivative f′(x)f'(x)Example
a constant cc00f(x)=5f(x) = 5 is flat
xnx^n (power rule)n xn−1n\,x^{n-1}x2→2xx^2 \to 2x,   x3→3x2\;x^3 \to 3x^2
c⋅g(x)c \cdot g(x) (constant multiple)c⋅g′(x)c \cdot g'(x)5x2→10x5x^2 \to 10x
g(x)+h(x)g(x) + h(x) (sum)g′(x)+h′(x)g'(x) + h'(x)5x2+3x→10x+35x^2 + 3x \to 10x + 3
exe^xexe^xslope equals height
log⁡x\log x1/x1/xslope at x=2x = 2 is 0.50.5

Here log⁡\log means the natural log, base ee, as in the log chapter.

Pick a curve below and drag the point. The orange tangent line touches the curve at the point. The readout gives its slope from the rule, and a nudge test with h=0.001h = 0.001 to compare.

−3−2−10123−202468xy

At x = 1.5, the curve’s height is 2.25

Tangent slope from the rule 2x: 3

Nudge test with h = 0.001: 3

Drag the point along each curve. The tangent's slope is the rule's answer; the nudge test agrees to two or three decimals.

Things to notice:

  • On x2x^2 the slope is 2x2x. It's negative on the left, zero at the bottom, and positive on the right.
  • On exe^x the slope always equals the height, which is the defining property of ee.
  • On log⁡x\log x the slope is 1/x1/x. It's steep near 0 and flattens out as xx grows.

Now use the rules on our error.

Worked example

The derivative of E(w₁) = 38(w₁ − 4)²

  1. Multiply out the square: 38(w12−8w1+16)=38w12−304w1+60838(w_1^2 - 8w_1 + 16) = 38w_1^2 - 304w_1 + 608.

Multiplying out the square worked here, but it gets painful for longer formulas. The chain rule, two chapters on, gets the same answer without it.

Common misconceptions

"The derivative is the value of the function." No. At w1=3w_1 = 3 the error is 38, but its slope is −76-76. One says how high you are; the other says how steep the ground is.

"A big slope means you're close to the answer." It's the other way round. In our valley the slope is steepest far from the bottom and zero at the bottom.

"The nudge test gives the exact slope." It gives an estimate. On our error slice the estimate is off by 38h38h, as the limits aside shows. The rules give the exact answer.

Recap

  • Zoom in on a smooth curve and it looks like a straight line, the tangent.
  • The derivative f′(x)f'(x) is the tangent's slope: rise over run across a nudge hh, as hh shrinks to 0.
  • Its sign says which way to nudge the input to go downhill. At the bottom of a valley it is 0.
  • A short table of rules (power, constant multiple, sum, exe^x, log⁡x\log x) gives exact derivatives.

Section 2 shows how to compute these in PyTorch.