All courses
Courses › How LLMs really work · foundation

Derivatives

Read a derivative as the rate of change: how much the output moves per small nudge of the input.

▶ Watch this lesson Start the whole course

What the animation says

Nudge the input

Here's a formula: x squared. Put in three, and out comes nine. Now nudge the input a little. Which way does the output move, and how far? How far it moves per unit of nudge is a single number: the derivative. A model is a huge program full of adjustable numbers. For each one, the derivative says which way to turn it, and how strongly the result reacts. So try it: nudge three up to three point one. Does the output move by point one too?

Rise over run

Three point one squared is nine point six one. The output rose point six one, for a nudge of point one. Divide them: rise over run. Point six one over point one gives six point one per unit of nudge. On a straight line, that rate is the same everywhere. On a curve, it depends on where you stand. So we take a tiny step. Think of a speedometer: it shows your speed right now, not your average over the trip.

Shrink the step

The size of the step, called h, still matters. With h of one, three goes to four and nine goes to sixteen: rate seven. With h of point one: rate six point one. With h of point zero one, the output is nine point zero six zero one: rate six point zero one. The rate is settling. The number it settles on as h shrinks toward zero is the derivative. One more: h of point zero zero one. What rate comes out?

It settles on 6

Six point zero zero one. Here's the same loop in Python. This line computes rise over run. As h shrinks, the rate goes seven, six point one, six point zero one, six point zero zero one. The odd trailing digits are just floating-point rounding. It settles on six: the derivative of x squared at three. In general it's two x. That's the power rule: x to the n gives n times x to the n minus one. The sign says which way the output moves. The size says how strongly.

Use it: predict a change

Now put the derivative to work. Move x from three to three point zero five. The nudge is point zero five. The derivative at three is six. So the output changes by about six times point zero five: point three. Where does the output land? Tap it.

What to remember

To predict a small change, take the output you know, and add the derivative times the nudge. Nine, plus six times point zero five, gives nine point three. The true value is nine point three zero two five, very close. The derivative is how much the output moves per tiny nudge. Its sign says which way; its size says how strongly.

Nudge the input: how far does the output move?

You have a formula, say $f(x) = x^2$. You change $x$ a tiny bit. Does the output go up or down, and by how much **per unit of nudge**? That single number is the **derivative**. It is the basic tool for tuning a huge program full of adjustable numbers: for each number, it tells you which way to turn it, and how strongly the result reacts.

Rise over run, for a tiny run

Picture the graph of $f$. Step a little to the right, by $h$, and see how far the curve rises. **rate = (change in output) ÷ (change in input)** For a straight line this rate is the same everywhere. For a curve it depends on where you stand, so we use a *tiny* step. Think of a speedometer: it shows metres per second *right now*, not your average over the trip.

The definition, symbol by symbol

$$f'(x) \approx \frac{f(x+h) - f(x)}{h}$$ $h$ is the nudge. The derivative $f'(x)$ (also written $\frac{df}{dx}$) is the value this fraction settles on as $h$ shrinks toward 0. - **Sign**: $+$ means the output rises, $-$ means it falls, $0$ means flat. - **Size**: how strongly it reacts. For small $h$: $f(x+h) \approx f(x) + f'(x)\cdot h$.

Derivative of x² at x = 3

$f(3) = 9$. $h=0.1$: $f(3.1)=9.61$, so $(9.61-9)/0.1 = 6.1$ $h=0.01$: $f(3.01)=9.0601$, so $(9.0601-9)/0.01 = 6.01$ The fraction settles on $f'(3) = 6$. In general the derivative of $x^2$ is $2x$ (the **power rule**: $x^n$ gives $n\,x^{n-1}$). Use it: nudge $x$ from 3 to 3.05. The output moves by about $6 \times 0.05 = 0.3$, to about $9.3$.

Watch the fraction settle

The same calculation in Python: shrink the nudge $h$ and the ratio approaches 6, which is $2x$ at $x=3$. The last digits can differ slightly from hand arithmetic (6.0099... instead of 6.01), because computers store decimals like 3.01 only approximately.
def f(x):
    return x ** 2

def rate(f, x, h):
    return (f(x + h) - f(x)) / h

for h in [1, 0.1, 0.01, 0.001]:
    print(h, rate(f, 3, h))
# 1 7.0
# 0.1 6.1000...
# 0.01 6.0099...   (6.01 up to rounding)
# 0.001 6.0009...  (6.001 up to rounding) -> approaches 6

What to remember

The derivative $f'(x)$ is how much the output moves per tiny nudge of the input. Its sign says which way, and its size says how strongly. To predict a small change, use $f(x+h) \approx f(x) + f'(x)\cdot h$.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.