Partial derivatives
Change one input at a time when there are many.
Create a free account Try part of it here
Why it matters
Most results you care about depend on more than one input. Weekly sales depend on search ads, social ads and emails. A model's error score, called the loss, depends on every one of its weights, and there can be billions of them. A partial derivative asks about one input at a time: if you nudge only this one, how much does the output move? With one answer per input, you know which lever matters most right now. Training asks exactly that question about every weight, on every step.
What you'll be able to do
- Read functions of several inputs, like conversions from two ad channels
- Compute a partial derivative by holding the other inputs fixed
- Estimate partial derivatives numerically, one input at a time
- Use partial derivatives to decide where the next dollar should go
What it covers
- Many inputs, one output
- Which slope?
- One input at a time
- When inputs team up
- Estimating partials with nudges
- Worked example: where should the next $1,000 go?
Many inputs, one output
Think about baking cookies. How they taste depends on the sugar, the butter and the time in the oven. Change any one of them and the cookies change. Most real results work like this: several inputs go in, one output comes out.
Here's a marketing version. Say your weekly conversions (sales or sign-ups) depend on two ad budgets. Call your search ad spend . Call your social ad spend . Both are counted in thousands of dollars, so means $4,000.
The function gives the conversions for any pair of budgets. Here's a made-up one with a little teamwork built in: people who see your social ads often go and search for you.
In plain words: the sign is the square root. The square root of 9 is 3, because 3 × 3 = 9. Square roots grow fast at first and then more slowly, so the first $1,000 buys more than the tenth. Search earns 40 times the root of its budget. Social earns 25 times the root of its budget. The last part is a bonus for running both at once.
Here's a worked example. Spend $4,000 on search and $1,000 on social, so and . The roots are , and . So the conversions are 40 × 2 + 25 × 1 + 3 × 2 = 80 + 25 + 6 = 111.
import math s, m = 4, 1 print(40 * math.sqrt(s) + 25 * math.sqrt(m) + 3 * math.sqrt(s * m))
Show the answer
111.0
80 + 25 + 6 = 111. math.sqrt always returns a decimal number, so Python prints 111.0. Leaving out the bonus gives 105, and forgetting the first square root gives 191.
Which slope?
Picture as a landscape. Lay the two budgets out on the ground: search runs one way, social runs the other. Above each spot, the ground rises to the number of conversions. You get a hilly surface.
Now stand somewhere on a hill. How steep is it? That depends on which way you walk. Heading east might be steep, and heading north might be gentle. So a surface has no single slope. You have to say which direction you mean.
Move y and watch the slope in x change. That's what it means for two inputs to team up.
One input at a time
The simplest directions run straight along one budget. You change search spend and leave social alone, or the other way around. Think of a sound mixing board: you move one slider and keep your hands off the rest.
The slope in one of those directions is a partial derivative. It's the derivative you already know, with one extra rule: vary one input and hold all the others fixed. It's written with a curly d, , usually read "partial".
In plain words: read as "partial f by partial s". It's roughly how many extra conversions one more $1,000 of search would bring, while social spend stays where it is.
Working one out by hand
To find a partial derivative, pretend the other inputs are ordinary fixed numbers. Then use the slope rules you already have. You need three of them:
- has slope ;
- a number times , like , has slope 5;
- a number on its own, like 7, has slope 0, because it never changes.
The derivative rules lesson covers these. If you skipped it, the nudge method from the derivatives lesson confirms each one.
Take a simpler function named . Its first input is . Its second input is .
In plain words: adds up three parts. The first is times itself. The second is 3 times times . The third is times itself. At and , that's 1 + 6 + 4 = 11.
Show the answer
Treat it as a fixed number
You ask how the output changes when only x moves. So y sits still, like any other number.
Hold fixed at 2. Then becomes . That's an ordinary function of one input. Its slope is , and the 6 there is 3 times . Keep as a letter instead of 2, and you get the general answer. Holding fixed instead gives the other partial:
In plain words: for , treat as a fixed number. The term gives . The term is " times ", so it gives . The term doesn't move when only moves, so it gives 0. For , swap the roles.
Worked with small numbers, at and :
- ;
- .
At this point, nudging moves a little more than nudging does. You'll confirm both numbers with Python further down.
Go deeper Checking the 3xy term with a nudge
Hold and start at . The term is . Nudge up by a small amount . The term becomes .
So the output grew by . Divide by the nudge and the slope is 6, which is . The term stays at 4 the whole time, so its slope is 0.
Show the answer
2xy
Hold x fixed. Then 5x is a constant, so its slope is 0. And xy² is "x times y²", which gives x · 2y = 2xy.
When inputs team up
Look again at . It contains . So how much matters depends on where is. That's called an interaction.
You know interactions from everyday life. An extra bag of chips is worth more when there's also salsa on the table. In marketing terms, the return on search spend depends on how much you spend on social.
Compare a function with no teamwork, such as . Here , whatever is. Each input does its own job, and neither changes the other's effect.
Slices of the surface
Go back to the conversions landscape. Fix social spend at one value, and becomes an ordinary curve in . It's like slicing a loaf of bread: each slice shows the shape at one social budget. The partial derivative is that curve's slope.
Different values of give different slices, with different slopes. The code below draws three slices: social at $1,000, $5,000 and $15,000. Then it measures the search slope at for two of them. It uses the central difference from the derivatives lesson: nudge up and down by a tiny of 0.00001, written 1e-5 in code.
import numpy as np import matplotlib.pyplot as plt def f(s, m): return 40 * np.sqrt(s) + 25 * np.sqrt(m) + 3 * np.sqrt(s * m) s = np.linspace(0.1, 20, 200) for m in [1, 5, 15]: plt.plot(s, f(s, m), label=f"social = ${m}k") plt.xlabel("search spend ($ thousands)") plt.ylabel("conversions") plt.legend() h = 1e-5 for m in [1, 15]: slope = (f(10 + h, m) - f(10 - h, m)) / (2 * h) print(f"∂f/∂s at s=10, social={m}k: {slope:.2f} conversions per extra $1k of search")
With $1,000 of social, the search slope is 6.80: roughly 6.8 extra conversions per extra $1,000 of search. With $15,000 of social, it's 8.16. More social spend makes each search dollar a bit more productive, so the curves are steeper. That's the bonus term at work.
Go deeper The exact slope by rule
A square root is a power of one half, so the power rule gives a slope of . The bonus term equals . With held fixed, is the number in front. The term doesn't change when only moves, so its slope is 0. Put it together:
In plain words: the first part comes from search alone. The second part contains , so more social spend makes it bigger.
At and , that's . At , it's . Both match the code.
Show the answer
4
Hold y fixed, and 7y is a constant with slope 0. The 4x term gives 4. The answer is 4 for any y, because these inputs don't interact.
Estimating partials with nudges
The nudge recipe from the derivatives lesson works with many inputs too. Number the inputs. Then means input number . The dots in the formula stand for every other input, left unchanged.
In plain words: move input number up a little, by . Then move it down by . Leave every other input where it is. Take the change in output and divide by the distance between the two points, which is .
Worked by hand with at , , using a big nudge of :
- For : and . So .
- For : and . So .
Both match the rule. For , this recipe happens to be exact for any . Most functions need a tiny to get close.
The code does the same with a tiny . The partial function makes two copies of the point, up and down. It nudges input i in each copy and returns the slope. The copies matter: if you nudged the original point, you'd change it for any code that uses it next. Python counts from 0, so i = 0 is and i = 1 is .
import numpy as np def g(p): x, y = p return x ** 2 + 3 * x * y + y ** 2 def partial(f, point, i, h=1e-5): up = np.array(point, dtype=float) down = np.array(point, dtype=float) up[i] += h down[i] -= h return (f(up) - f(down)) / (2 * h) p = [1.0, 2.0] print("∂g/∂x:", round(partial(g, p, 0), 6), " rule: 2x + 3y =", 2 * 1 + 3 * 2) print("∂g/∂y:", round(partial(g, p, 1), 6), " rule: 3x + 2y =", 3 * 1 + 2 * 2)
The nudges give 8.0 and 7.0, the same as the rule.
The cost of nudging
Each partial needs two runs of the function. A model with weights needs runs of the loss to get every partial. With 1,000 weights, that's 2,000 runs. That's fine for checking code on a small model. For a billion weights it's impossible.
Training uses a method called backpropagation instead, which you'll meet later. It gets all partials in one backward pass through the model.
Show the answer
1,000
Each weight needs one run nudged up and one nudged down: 2 × 500 = 1,000.
Worked example: where should the next $1,000 go?
You spend $9,000 on search and $4,000 on social, with the conversions function f from this lesson. Measure both partials. Then give $1,000 more to social and measure again. Each printed line shows the social budget, search's partial, and social's partial.
import math def f(s, m): return 40 * math.sqrt(s) + 25 * math.sqrt(m) + 3 * math.sqrt(s * m) h = 1e-5 s = 9 for m in [4, 5]: by_search = (f(s + h, m) - f(s - h, m)) / (2 * h) # social held at m by_social = (f(s, m + h) - f(s, m - h)) / (2 * h) # search held at 9 print(m, round(by_search, 2), round(by_social, 2))
At $4,000 of social, search's partial is 7.67 and social's is 8.5. So the next $1,000 should go to social: about 8.5 extra conversions against 7.67.
After social rises to $5,000, the picture flips. Social's partial drops to 7.6 and search's rises to 7.78. Two things happened. Social's returns shrank as its budget grew, because the square root flattens out. And search got a small boost from the extra social, which is the interaction. That's why you measure again after every shift.
Show the answer
Returns shrink as a channel grows, and channels interact, so the best choice can change
Each partial describes one budget point. Move the budget and both partials change: social's fell from 8.5 to 7.6 and search's rose from 7.67 to 7.78.
Practice what you just read
- 3 code examples you can edit and run
- a practice exercise that checks your code
- a 5-question quiz
- 4 review cards for spaced repetition
Try a quiz question
What does ∂f/∂x measure for a function f(x, y)?