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
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.
Freeze everything except one input
Partial derivatives and the gradient
Worked example: f(a, b) = a²b + 3b
Check it by nudging one input at a time
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
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.
Bytewise
All courses