Part I · Maths · Chapter 5

Derivatives and gradients

A derivative is the slope of a curve at one point: how fast the output changes when you nudge the input. Training a model is nothing more than following derivatives downhill, so this is the most important chapter of Part I.

Why it matters

Every model in Part II and Part III learns by asking, for each of its parameters, "if I nudge this number up a little, does the error go up or down, and by how much?" That question is a derivative. The chain rule lets a computer answer it for billions of parameters at once; the answer is called the gradient.

The slope of a curve

A straight line has one slope everywhere (chapter 2). A curve like f(x) = x2 is steeper in some places than others. To find its slope at x = 3, take a second point a small gap h to the right and compute rise over run, then shrink the gap:

gap hf(3 + h)rise = f(3 + h) − 9rise / h
11677
0.19.610.616.1
0.019.06010.06016.01
0.0019.0060010.0060016.001

The answers close in on 6. That limit is the derivative of x2 at 3. The line through the point with that slope just touches the curve; it is called the tangent.

f′(x) = limit as h → 0 of f(x + h) − f(x)hRead f′ as "f prime". Another notation is df/dx: "the change in f per change in x".
Try it

Tangent-line slider

Slide x along the curve. Then shrink the gap h and watch the dashed line through two points turn into the solid tangent. Try the sigmoid: its slope is never more than 0.25, which matters in chapter 14.

Rules that save the limit

Nobody computes limits by hand every time. A handful of rules cover everything in this book:

FunctionDerivativeWorked check
a constant, c0A flat line has no slope.
xn (power rule)nxn−1x2 → 2x, which is 6 at x = 3, as the table found.
c × f(x)c × f′(x)5x2 → 10x.
f(x) + g(x)f′(x) + g′(x)x3 − 3x → 3x2 − 3.
exexThe slope equals the height (chapter 3).
ln x1 / xAt x = 0.5 the slope is 2: small probabilities make ln change fast.
sigmoid σ(x) = 1 / (1 + e−x)σ(x)(1 − σ(x))At 0: 0.5 × 0.5 = 0.25, the largest it ever gets.

Where the derivative is zero the curve is flat: at the bottom of a valley, the top of a hill, or a plateau. For x3 − 3x, 3x2 − 3 = 0 at x = 1 (a valley) and x = −1 (a hill).

Try it

Find the derivative of 4x3 + 2x − 7, and its value at x = 1.

Answer

12x2 + 2. At x = 1 it is 14.

The chain rule

A neural network is a chain of functions (chapter 2). The chain rule says how slopes pass along a chain: multiply the slope of each link.

Worked example. Let h(x) = (2x + 1)2. Split it: the inner link is u = 2x + 1 and the outer link is u2. At x = 1:

  1. Inner value: u = 3. Inner slope: du/dx = 2.
  2. Outer slope at u = 3: 2u = 6.
  3. Multiply: 6 × 2 = 12.

Check with a small nudge: h(1.001) = 3.0022 = 9.012004, and (9.012004 − 9) / 0.001 = 12.004. It agrees. Intuition: the inner link doubles any nudge to x, and the outer link multiplies a nudge to u by 6, so the whole chain multiplies it by 12.

dhdx = dhdu × dudx

Longer chains just add more factors. A network with 50 layers has a chain of 50 links, and the slope of the error with respect to a weight in layer 1 is a product of about 50 factors. If many factors are small (like the sigmoid's 0.25 at best), the product shrinks towards zero: 0.255 ≈ 0.001 and 0.2510 ≈ 0.000001. That is the vanishing-gradient problem of chapter 14.

Try it

Find the slope of e3x at x = 0.

Answer

Inner u = 3x has slope 3. Outer eu has slope eu, which at u = 0 is 1. Product: 3.

Several inputs: partial derivatives

When a function has several inputs, nudge one at a time and hold the others still. That gives a partial derivative, written with a curly ∂.

Worked example: f(x, y) = x2y + 3y at (1, 2).

  • Treat y as a constant: ∂f/∂x = 2xy = 2 × 1 × 2 = 4.
  • Treat x as a constant: ∂f/∂y = x2 + 3 = 1 + 3 = 4.

The gradient

Collect all the partial derivatives into a vector and you have the gradient, written ∇f ("del f"). For the example, ∇f(1, 2) = (4, 4).

∇f = ( ∂f/∂x1, ∂f/∂x2, …, ∂f/∂xn )The gradient points in the direction where f rises fastest. Its negative points steepest downhill.

Picture standing on a hillside in fog. You cannot see the valley, but you can feel which way the ground slopes under your feet. The gradient is that feeling, in as many dimensions as the function has inputs.

In AI: the slope of the error

Here is the whole of Part II in miniature. A model predicts w × x. One training example has x = 2 and the right answer y = 6. The error, or loss, is the squared difference L(w) = (wx − y)2.

  • At w = 1 the prediction is 2, the miss is 2 − 6 = −4, and the loss is 16.
  • Chain rule: dL/dw = 2(wx − y) × x = 2 × (−4) × 2 = −16.
  • The slope is negative, so increasing w lowers the loss. The right answer is w = 3, where the slope is 0.

Real models have many parameters, so they need the whole gradient. Backpropagation (chapter 12) is the chain rule applied from the loss backwards through every layer, reusing each link's slope so the whole gradient costs about as much as one extra pass through the network. Libraries such as PyTorch do this automatically ("autograd").

Try it

A model computes a = σ(wx) and has loss (a − 1)2. With w = 0.5 and x = 2, find dL/dw. You need σ(1) = 0.7311.

Answer

Three links. Loss: 2(a − 1) = 2 × (−0.2689) = −0.5378. Sigmoid: a(1 − a) = 0.7311 × 0.2689 = 0.1966. Inside: d(wx)/dw = x = 2. Product: −0.5378 × 0.1966 × 2 = −0.2115. Negative, so raising w lowers the loss. A numerical nudge gives the same −0.2115.

Most people who build on hosted models never take a derivative in their own code. Every model they call was trained elsewhere with exactly this maths. Knowing how those models learned still helps, because it explains how they fail (chapter 36).

Build it

In chapter 15 you write backpropagation yourself in NumPy and check every gradient against a numerical slope.

Self-check

What does a derivative of zero tell you about a curve at that point?

The curve is flat there: it could be the bottom of a valley, the top of a hill or a flat stretch. Training looks for valleys of the loss.

Using the chain rule, what is the derivative of (3x − 2)2?

Outer 2u times inner 3: 2(3x − 2) × 3 = 6(3x − 2).

What is the gradient of f(x, y) = 3x + 5y?

(3, 5). Each partial derivative is a constant because the function is a flat plane.

Why do long chains of sigmoids make learning slow?

The chain rule multiplies one slope per link, and the sigmoid's slope is at most 0.25. Many such factors multiply to a number near zero, so early layers barely change.

Further reading