Math for AILoading progress…

Partial derivatives

Calculate slopes, use the chain rule and distinguish derivatives from partial derivatives.

Many inputs, one at a time

Models have thousands of parameters. A loss such as f(x, y) = x² + 3xy depends on two inputs here. We cannot ask for "the" slope, because it depends on which direction you walk. The simplest directions are along the axes. A partial derivative changes one input and freezes the others.

  • ∂f/∂x: treat y as a constant number. Then f is just x² + (3y)·x, whose derivative is 2x + 3y.
  • ∂f/∂y: treat x as a constant. Then f is x² + (3x)·y, whose derivative is 3x.

At (2, 1): ∂f/∂x = 4 + 3 = 7 and ∂f/∂y = 6.

Slice the surface and read the slope

Picture f as a landscape and the colour map as a top view. A line of constant y through the point is a slice along x. The curve at the right is the height of the landscape along that slice, and the slope of its tangent is exactly ∂f/∂x. Choose the other slice and you read ∂f/∂y. The pair of numbers [∂f/∂x, ∂f/∂y] is the gradient.

Red: f large and positive. Blue: f negative. Click the map or use the sliders.

-3-2-10123-20-10010203040
f(x, y) = x² + 3xy   f(1, 1) = 4
∂f/∂x = 2x + 3y = 5   (numerical: 5.0000)
∂f/∂y = 3x = 3   (numerical: 3.0000)
gradient = [5, 3]
Click the map or use the sliders. Switch the slice between x and y and compare the green tangent with the numbers.
f = lambda x, y: x ** 2 + 3 * x * y
h = 1e-5
print((f(2 + h, 1) - f(2 - h, 1)) / (2 * h))
print((f(2, 1 + h) - f(2, 1 - h)) / (2 * h))
# output:
7.0000000000902665
6.000000000039306

Predict first. For f(x, y) = x² + 3xy at (x, y) = (0, 5), what is ∂f/∂x?

Common mistakes

  • Differentiating y as if it changed when you take ∂/∂x. Freeze it: it behaves like the number 5 or −2.
  • Mixing up the symbol d and ∂. Use d for one-variable functions and ∂ when other inputs exist and are held fixed.
  • Thinking a zero partial means a flat point. At (3, −2) the x-slice is flat (∂f/∂x = 0) but the y-slice slopes at 9. Both partials must be 0 for a stationary point.
Chapter context

A derivative measures a local rate of change. It describes what a function does under a very small input change, rather than its average behavior over an arbitrary interval. This local sensitivity is what training algorithms use.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

f′(x)=lim⁡h→0f(x+h)−f(x)h.f'(x)=\lim_{h\to0}\frac{f(x+h)-f(x)}h. ddxf(g(x))=f′(g(x))g′(x),∂∂x(x2+3xy)=2x+3y.\frac{d}{dx}f(g(x))=f'(g(x))g'(x),\quad \frac{\partial}{\partial x}(x^2+3xy)=2x+3y.

For numerical checking, use (f(x+h)−f(x−h))/(2h)(f(x+h)-f(x-h))/(2h) and report a tolerance.

Worked example

Let f(x)=(3x+1)^2. At x=2, the inner value is 7, its derivative is 3, and the outer derivative is 14. Multiplying gives 42. A central difference with h=0.00001 gives approximately the same answer, within numerical error.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
f = lambda x: (3*x + 1)**2
x, h = 2, 1e-5
analytic = 6*(3*x + 1)
numeric = (f(x+h)-f(x-h))/(2*h)
assert analytic == 42
assert isclose(numeric, analytic, rel_tol=1e-7)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

A function value is not its derivative. The chain rule multiplies sensitivities, not intermediate values. At a corner such as abs(x) at zero, a two-sided derivative does not exist.