AI from scratch · Calculus 1 · Foundations

Derivatives: the rules of change

Training a model means asking one question millions of times: if I nudge this number, how much does the error change? That number is a derivative. This lesson builds it from a picture, gives you the handful of rules that differentiate any network, and shows the two places engineers get burned: activations whose slopes vanish, and derivatives computed carelessly by the computer.

1. Zoom in until the curve is a line

Pick a point \(a\) on a curve and a second point a distance \(h\) further along. The red line through both has slope "rise over run". Shrink \(h\): the red line turns into the green tangent, the line the curve looks like if you zoom in far enough. Its slope is the derivative.

\[ f'(a) = \lim_{h \to 0} \frac{f(a+h) - f(a)}{h} \]

Notice the gap between the two slopes shrinks in step with \(h\): halve \(h\), halve the gap. For \(x^2\) you can see why: \(\frac{(a+h)^2 - a^2}{h} = \frac{2ah + h^2}{h} = 2a + h\). Let \(h\) go to zero and what's left is \(2a\).

For \(f(x) = x^2\) at \(a = 3\) with \(h = 0.1\), what is the red line's slope \(\frac{f(a+h) - f(a)}{h}\)?

\((3.1^2 - 9)/0.1 = (9.61 - 9)/0.1 = 6.1\), which is \(2a + h\). The true slope at 3 is 6.

A derivative is also a sensitivity: near \(a\), with \(\Delta\) (the Greek capital delta, "a small change") for the nudge, \(f(a + \Delta) \approx f(a) + f'(a)\,\Delta\). If the loss has derivative 6 with respect to a weight, nudging that weight up by 0.001 raises the loss by about 0.006. Gradient descent (chapter 2) is just this sentence used backwards.

Zoom in far enough and a smooth curve looks like a straight line; the derivative is that line's slope, and it says how much the output moves per unit you move the input.

Where it breaks: kinks, jumps, and "near"

2. Seven rules differentiate everything

Nobody takes limits by hand. Every function a network computes is built from a few pieces, and each piece has a rule:

functionderivativein words
\(c\)\(0\)a constant doesn't change
\(x^n\)\(n x^{n-1}\)power rule (works for \(n = -1\), \(\tfrac12\), any \(n\))
\(c\,f + g\)\(c\,f' + g'\)derivatives add and scale
\(f \cdot g\)\(f' g + f g'\)product rule: each one changes while the other holds
\(f(g(x))\)\(f'(g(x)) \cdot g'(x)\)chain rule: multiply the slopes along the way
\(e^x\)\(e^x\)its own slope (section 3)
\(\ln x\)\(1/x\)the log flattens as it grows

Plus \(\sin' = \cos\) and \(\cos' = -\sin\) for waves (chapter 1's sine and cosine: the height and the shadow of a turning arrow). One more way of writing a derivative: \(\frac{d}{dx}\) in front of a formula means "the derivative of this, as \(x\) changes", the same thing as the prime, handy when the function has no name. Two worked examples: \(\frac{d}{dx}\, x^2 \sin x = 2x \sin x + x^2 \cos x\) (product rule), and the bell curve \(\frac{d}{dx}\, e^{-x^2} = e^{-x^2} \cdot (-2x)\) (chain rule: outside slope times inside slope). The chain rule is the one that matters most: a network is a long chain of functions, and backprop (chapter 4) is the chain rule run from the loss backwards.

\(f(x) = 3x^4 - 2x + 7\). What is \(f'(1)\)?

\(f'(x) = 12x^3 - 2\), so \(f'(1) = 10\). The 7 disappears: shifting a function up doesn't change its slope.

What is the slope of \(e^{3x}\) at \(x = 0\)?

Chain rule: \(e^{3x} \cdot 3\), and \(e^0 = 1\), so 3. The inside's slope multiplies everything.

The chain rule multiplies the slopes along the way, and a network is one long chain.

Why the chain rule multiplies

Nudge \(x\) by a small \(\Delta\). The inside, \(g\), moves by about \(g'(x)\,\Delta\) (the sensitivity sentence). That move is the outside's input nudge, so \(f\) moves by about \(f'(g(x))\) times it: \(f'(g(x))\, g'(x)\, \Delta\). Divide by \(\Delta\) and you have the rule. Three gears in a row, turning 2× and then 3×, turn 6×; ten layers that each pass on 0.25 of a nudge pass on \(0.25^{10}\) of it (section 4).

Predict before you compute: what is the slope of the bell curve \(e^{-x^2}\) at \(x = 0\)?

0: it's the top of the bell, flat for an instant. The formula agrees: \(e^{0} \cdot (-2 \cdot 0) = 0\). Tops and bottoms of smooth curves always have slope 0, which is exactly how training knows it has reached a minimum.
Why the product rule looks like that

Picture \(f \cdot g\) as the area of a rectangle with sides \(f\) and \(g\). Nudge \(x\): the sides grow by \(df\) and \(dg\), and the area gains a strip \(g\,df\), a strip \(f\,dg\), and a corner \(df\,dg\). The corner is a small number times a small number, so it vanishes faster than the strips when you divide by the nudge. What remains is \(f'g + fg'\).

3. ex and ln: the two functions ML is made of

\(e^x\) is the function that grows at a rate equal to its size: its slope everywhere is its own value. That makes it the natural way to turn any score into something positive with a clean derivative, which is exactly what softmax does (chapter 5). Its inverse, \(\ln x\), has slope \(1/x\): steep near zero, flat for large \(x\).

Logs are everywhere in ML for two reasons. First, \(\ln(ab) = \ln a + \ln b\): the probability of a whole sentence is a product of a thousand token probabilities, and the log turns it into a sum, which has a far easier derivative. Second, the product doesn't fit in a computer:

The loss for a prediction is \(-\ln p\), where \(p\) is the probability the model gave the right answer. Its derivative is \(-1/p\): when the model is confidently wrong (tiny \(p\)), the slope is huge and the correction is strong.

The model gave the right answer probability \(p = 0.01\). How many times steeper is \(-\ln p\) there than at \(p = 1\)?

The slope's size is \(1/p\): 100 at \(p = 0.01\), 1 at \(p = 1\). Confident mistakes get pushed a hundred times harder.

4. Activations and their slopes

Between layers, a network bends its numbers with an activation function. Backprop multiplies by the activation's derivative at every layer, so its shape decides whether learning signals survive the trip. Blue is the function, yellow its derivative.

The sigmoid \(\sigma(x) = \frac{1}{1 + e^{-x}}\) has a neat derivative, \(\sigma'(x) = \sigma(x)\,(1 - \sigma(x))\), and it is never more than 0.25. Stack ten sigmoid layers and the chain rule multiplies ten of those slopes together: the signal reaching the first layer is at most \(0.25^{10}\), about one millionth. That's the vanishing gradient that stalled deep networks for years. ReLU (slope exactly 1 for positive inputs) fixed most of it; its weakness is slope 0 for negative inputs, where a neuron can "die" and never recover. GELU, used in transformers, is a smooth ReLU.

Backprop multiplies one activation slope per layer, so an activation whose slope is usually far below 1 starves the early layers.

What is the largest value the sigmoid's derivative ever takes?

At \(x = 0\), \(\sigma = 0.5\), so \(\sigma' = 0.5 \times 0.5 = 0.25\). Everywhere else it's smaller.

Use the chain rule: what is the slope of \(\ln \sigma(x)\) at \(x = 0\)?

\(\frac{d}{dx} \ln \sigma(x) = \frac{\sigma'(x)}{\sigma(x)} = 1 - \sigma(x)\), which is 0.5 at 0. That simple "1 minus the probability" is why logistic regression and cross-entropy have such clean gradients (chapter 5's "what you said minus what was true").

5. Derivatives by computer, and checking them

A computer can estimate a slope straight from the definition with a small \(h\): the forward difference \(\frac{f(x+h) - f(x)}{h}\), or the more accurate central difference \(\frac{f(x+h) - f(x-h)}{2h}\). Smaller \(h\) should be better. Here is the actual error for \(f = \sin\) at \(x = 1\), on a log scale (each grid line is ten times more):

forward difference central difference

Going right to left, both errors fall as \(h\) shrinks (forward like \(h\), central like \(h^2\)), then turn around and climb. Below about \(h = 10^{-8}\), \(f(x+h)\) and \(f(x)\) agree in almost all of their 16 digits, and subtracting them leaves mostly rounding noise, which is then divided by a tiny \(h\). Floating point puts a floor under how precisely you can measure a slope this way.

Roughly which \(h\) gives the forward difference its smallest error?

The approximation error grows like \(h\), the rounding error like \(10^{-16}/h\); they balance near \(h = \sqrt{10^{-16}} = 10^{-8}\). For the central difference the sweet spot is near \(10^{-5}\), with an error around \(10^{-11}\).

Why not train with numerical derivatives? They cost one or two extra evaluations of the whole network per parameter: a billion-parameter model would need two billion forward passes for one step. Backprop gets every derivative for about the cost of two or three forward passes. So engineers use numerical derivatives for one job: gradient checking, comparing them against the hand-derived or backprop gradient on a few inputs to catch bugs.

6. What to carry forward

All chapters: AI from scratch

Last updated