Partial Derivatives
A partial derivative measures how a function of several variables changes when you nudge one input and hold all the others fixed. It is the ordinary derivative you already know, applied one variable at a time. It is also the basic quantity of machine learning: the partial derivative of the loss with respect to each weight says how to adjust that weight.
Definition
For , the partial derivative with respect to at is
and nudges instead. Geometrically, slice the surface with the plane ; is the slope of the resulting curve. Common notations:
| Notation | Meaning |
|---|---|
| , , | partial derivative with respect to |
| , | second partial, twice |
| , | first , then (mixed partial) |
Computing partials
Treat every other variable as a constant and use the usual rules (power, product, chain). For :
SymPy's diff(f, x) does exactly this, and a finite difference checks it numerically:
import sympy as sp
x, y = sp.symbols('x y')
f = x**3 * y**2 + 4*x*y - sp.sin(y)
fx, fy = sp.diff(f, x), sp.diff(f, y)
print("f_x =", fx)
print("f_y =", fy)
a, b = 1.5, 0.8
exact = float(fx.subs({x: a, y: b}))
h = 1e-6
F = sp.lambdify((x, y), f)
numeric = (F(a + h, b) - F(a - h, b)) / (2 * h)
print(f"f_x(1.5, 0.8): exact {exact:.6f}, central difference {numeric:.6f}")f_x = 3*x**2*y**2 + 4*y
f_y = 2*x**3*y + 4*x - cos(y)
f_x(1.5, 0.8): exact 7.520000, central difference 7.520000
SymPy prints x**2 for ; that is Python syntax, not a different answer.
Second partials and Clairaut's theorem
Differentiate again to get four second partials of : , and the two mixed ones and . Clairaut's theorem says that if the second partials are continuous (true for every formula built from polynomials, exp, sin and so on, away from trouble spots), the order does not matter: .
import sympy as sp
x, y = sp.symbols('x y')
f = sp.exp(x * y) + x**2 * sp.cos(y)
print("f_xx =", sp.diff(f, x, 2))
print("f_yy =", sp.diff(f, y, 2))
fxy = sp.diff(f, x, y) # x first, then y
fyx = sp.diff(f, y, x) # y first, then x
print("f_xy =", fxy)
print("equal?", sp.simplify(fxy - fyx) == 0)f_xx = y**2*exp(x*y) + 2*cos(y)
f_yy = x**2*(exp(x*y) - cos(y))
f_xy = x*y*exp(x*y) - 2*x*sin(y) + exp(x*y)
equal? True
The second partials collected into a matrix form the Hessian, which describes curvature and appears in Jacobian and Hessian.
Linear approximation and the tangent plane
Near a point , a smooth surface looks like its tangent plane:
This is the two-variable version of the tangent line, and it is how errors propagate: if a rectangle's sides and are measured with small errors and , the area has error .
In software: gradients of a loss
For the line model with mean squared error , the partial derivatives are
Each says how the loss reacts to a small change of one parameter. Every training step of linear regression, and of a neural network, computes these numbers (by backpropagation or automatic differentiation) and moves each parameter a little against its own partial derivative. A finite-difference check like the one below is the standard way to test hand-written gradient code.
import numpy as np
x = np.array([0.0, 1.0, 2.0, 3.0])
y = np.array([1.0, 3.2, 4.8, 7.1])
def L(w, b):
return np.mean((w * x + b - y) ** 2)
w, b = 1.0, 0.5
r = w * x + b - y
dw = 2 * np.mean(r * x)
db = 2 * np.mean(r)
h = 1e-6
print(f"dL/dw: formula {dw:.6f}, numeric {(L(w + h, b) - L(w - h, b)) / (2 * h):.6f}")
print(f"dL/db: formula {db:.6f}, numeric {(L(w, b + h) - L(w, b - h)) / (2 * h):.6f}")dL/dw: formula -8.550000, numeric -8.550000
dL/db: formula -4.050000, numeric -4.050000
Both partials are negative, so increasing or would lower the loss: the line is currently too low and too flat.
Exercises
Exercise 1: First partials
Find and for .
Answer
, .
Solution
Holding fixed, differentiates to , is a constant, and gives 7. Holding fixed, gives , gives , and is a constant.
Exercise 2: Chain rule inside a partial
Find for and evaluate it at .
Answer
, which is at .
Solution
Exercise 3: Mixed partial
For , compute and .
Answer
Both equal .
Solution
Exercise 4: Error estimate
A cylinder has radius and height . Use the linear approximation to estimate the change in its volume if grows by 0.1 cm and shrinks by 0.2 cm, then compare with the exact change in code.
Answer
; the exact change is about .
Solution
import math
V = lambda r, h: math.pi * r**2 * h
print(round(7 * math.pi, 2), round(V(5.1, 11.8) - V(5, 12), 2))21.99 21.73
Test yourself
When computing , the other variables are treated as…
- zero
- constants
- functions of
- infinitely small
What is of ?
Clairaut's theorem says that (for smooth functions)…
- all second partials are zero