All courses
Courses › How LLMs really work · foundation

Partial derivatives and the gradient

Take partial derivatives of a function of many inputs and collect them into the gradient vector.

Builds on: The chain rule, Vectors

▶ Watch this lesson Start the whole course

What the animation says

Many knobs, one meter

A derivative tells you how one output reacts to one input. But real systems have many inputs, sometimes millions of adjustable numbers, all feeding one score. Picture a mixing desk: lots of knobs, one loudness meter. To improve the score, you need to know, for every knob, which way to turn it and how strongly the meter reacts. This lesson gets that rate for every input at once, and packs it into one neat list.

Freeze all but one

The trick: freeze every knob except one. Take this function: a squared times b, plus three b. Two knobs, a and b. Freeze b. It's now just a fixed number, and the output depends on a alone, so this is an ordinary derivative. a squared times b becomes two a times b, by the usual power rule. b just rides along as a constant. That's a partial derivative: the rate of change when only one input moves. With b still frozen, what does the three b term become?

Build the gradient

Three b becomes zero: with b frozen, it's a constant. So the partial with respect to a is two a b. Now swap. Freeze a, turn b. a squared times b becomes a squared, and three b becomes three. So a squared plus three. Plug in a equals two, b equals one. Two times two times one is four. Four plus three is seven. The gradient collects these into one vector: one entry per input, in the same order as the inputs. Drag the seven into its slot.

Check it by nudging

The gradient is four, seven. Let's check it by really nudging. f at two, one is seven. Nudge a to two point zero one: f becomes seven point zero four zero one. The change, about zero point zero four, is four times the nudge. There's the four. In code: this function is f, and e p s is a nudge of one millionth. This line moves a only, this line moves b only, and each divides the change by the nudge. Rounded to three decimals, what does the last line print?

What the gradient tells you

It prints four point oh, seven point oh, matching the gradient. The gradient also predicts nudging every input at once: the change in f is about four times a's nudge, plus seven times b's nudge. Nudge both by one hundredth: four hundredths plus seven hundredths, about point one one. The real change is point one one zero five. Signs say which way to turn each knob. Sizes say which knob matters most: here, b. So a partial derivative is an ordinary derivative with the other inputs frozen. The gradient lists one per input, in order.

One output, many inputs: which knob matters?

A derivative tells you how an output responds to **one** input. Real systems have many inputs, sometimes millions of adjustable numbers feeding one score. To improve that score you need to know, for **each** input, which way to nudge it and how strongly the output reacts. How do we get that rate for every input at once, and package it neatly?

Freeze everything except one input

Picture a mixing desk with many knobs and one loudness meter. Hold every knob still except one, and turn that one slightly. Now the meter depends on a **single** input, so it is an ordinary derivative. The other inputs act as fixed constants. Repeat for each knob in turn. Each answer is a **partial derivative**. Your usual rules (power rule, chain rule) still apply unchanged.

Partial derivatives and the gradient

For $f(u_1,\dots,u_n)$, the partial derivative $\frac{\partial f}{\partial u_i}$ is the rate of change of $f$ when only $u_i$ moves and all other $u_j$ stay fixed. The **gradient** collects them into one vector: $$\nabla f = \left(\tfrac{\partial f}{\partial u_1},\dots,\tfrac{\partial f}{\partial u_n}\right)$$ One entry per input. For small nudges $\Delta u_i$: $$\Delta f \approx \sum_{i=1}^{n} \tfrac{\partial f}{\partial u_i}\,\Delta u_i$$

Worked example: f(a, b) = a²b + 3b

**∂f/∂a**: treat $b$ as a constant. $a^2b \to 2ab$; $3b \to 0$. So $2ab$. **∂f/∂b**: treat $a$ as a constant. $a^2b \to a^2$; $3b \to 3$. So $a^2+3$. At $(a,b)=(2,1)$: $\nabla f=(4,\,7)$. Check: $f(2,1)=7$, $f(2.01,1)=4.0401+3=7.0401$. Change $0.0401 \approx 4\times0.01$. ✓

Check it by nudging one input at a time

Numerically: nudge one input by a tiny amount, keep the other fixed, and divide the change in output by the nudge. How to read the code: `def f(a, b): return ...` defines the function $f$. `a**2` means $a^2$. `eps = 1e-6` sets the nudge to $0.000001$. `round(x, 3)` rounds to 3 decimals, and `print([...])` shows the two results as a list. The raw estimates are about 4.000001 and 7.000001, so after rounding they match the gradient $(4, 7)$ from the worked example.
def f(a, b):
    return a**2 * b + 3*b

eps = 1e-6
a, b = 2.0, 1.0
da = (f(a + eps, b) - f(a, b)) / eps  # move a only
db = (f(a, b + eps) - f(a, b)) / eps  # move b only
print([round(da, 3), round(db, 3)])   # [4.0, 7.0]

What to remember

A partial derivative is an ordinary derivative taken with all other inputs frozen as constants. The gradient lists all of them, one entry per input, and tells you how the output responds to small nudges of every input at once: $\Delta f \approx \sum_i \frac{\partial f}{\partial u_i}\Delta u_i$. The signs and sizes of its entries show which way to nudge each input, which is what later lets many adjustable numbers be tuned together.

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.