Calculus and Matrix Calculus for ML

Partial Derivatives

Partial derivatives as coordinate-by-coordinate sensitivity for functions with several inputs.

Lesson
34

Goal

Learn how to ask a precise local question about a function with several inputs:

If I change only this input, how does the output respond?

That question is answered by a partial derivative.

Motivation

A machine-learning loss usually depends on many parameters:

L(θ1,θ2,,θn)L(\theta_1,\theta_2,\ldots,\theta_n)

Training needs to know how sensitive the loss is to each parameter. Partial derivatives isolate those coordinate-by-coordinate effects.

They are the entries from which gradients, Jacobians, and backpropagation are built.

Intuition: One Knob At A Time

Imagine a sound mixer with several knobs. The output volume depends on all of them, but you can study one knob while leaving the others untouched.

For a function f(x,y)f(x,y):

The other variable does not disappear. During that derivative, it behaves like a constant.

Notation / Symbol Table

SymbolPlain-English nameMeaning
f(x,y)f(x,y)scalar fieldA function taking two coordinates and returning one number.
fx\frac{\partial f}{\partial x}partial derivative with respect to xxLocal output sensitivity when only xx varies.
xf\partial_x fcompact partial notationAnother way to write fx\frac{\partial f}{\partial x}.
(a,b)(a,b)evaluation pointThe input where a derivative is measured.
Δx,Δy\Delta x,\Delta yfinite input changesSmall but non-infinitesimal coordinate changes.
dfdftotal differentialFirst-order change assembled from all coordinate sensitivities.

The rounded symbol \partial is used because the function has multiple inputs and we are differentiating with respect to only one of them.

Formal Definition

For f:R2Rf:\mathbb{R}^2\to\mathbb{R}, the partial derivative with respect to xx at (a,b)(a,b) is:

fx(a,b)=limh0f(a+h,b)f(a,b)h\frac{\partial f}{\partial x}(a,b) = \lim_{h\to0}\frac{f(a+h,b)-f(a,b)}{h}

Only the first coordinate changes. The second remains exactly bb.

Similarly:

fy(a,b)=limh0f(a,b+h)f(a,b)h\frac{\partial f}{\partial y}(a,b) = \lim_{h\to0}\frac{f(a,b+h)-f(a,b)}{h}

This is ordinary one-variable differentiation applied to a coordinate slice through the surface.

Worked Example: A Quadratic Surface

Let:

f(x,y)=x2+3xy+y2f(x,y)=x^2+3xy+y^2

To differentiate with respect to xx, treat yy as a constant:

fx=2x+3y\frac{\partial f}{\partial x}=2x+3y

Why?

Now hold xx fixed and differentiate with respect to yy:

fy=3x+2y\frac{\partial f}{\partial y}=3x+2y

At (1,2)(1,2):

fx(1,2)=2+6=8\frac{\partial f}{\partial x}(1,2)=2+6=8 fy(1,2)=3+4=7\frac{\partial f}{\partial y}(1,2)=3+4=7

Near that point, the output is slightly more sensitive to motion in the xx coordinate than in the yy coordinate.

Geometry: Slicing A Surface

The graph of z=f(x,y)z=f(x,y) is a surface.

To see f/x\partial f/\partial x at (a,b)(a,b):

  1. freeze y=by=b;
  2. intersect the surface with that vertical plane;
  3. measure the slope of the resulting curve in the xx direction.

For f/y\partial f/\partial y, freeze x=ax=a and take the perpendicular slice.

Each partial derivative is a slope along one coordinate direction. It is not yet the whole gradient; it is one component of it.

Partial Derivatives Assemble Into The Gradient

For a scalar field f:RnRf:\mathbb{R}^n\to\mathbb{R}, the gradient collects all first partial derivatives:

f(x)=[fx1fx2fxn]\nabla f(x)= \begin{bmatrix} \frac{\partial f}{\partial x_1}\\ \frac{\partial f}{\partial x_2}\\ \vdots\\ \frac{\partial f}{\partial x_n} \end{bmatrix}

This shape distinction matters:

The next lesson studies that assembled vector directly.

Partial Change Versus Total Change

A partial derivative isolates one coordinate. A real input change may move several coordinates at once.

For a small displacement (Δx,Δy)(\Delta x,\Delta y), the first-order output change is approximately:

ΔffxΔx+fyΔy\Delta f \approx \frac{\partial f}{\partial x}\Delta x + \frac{\partial f}{\partial y}\Delta y

In differential notation:

df=fxdx+fydydf = \frac{\partial f}{\partial x}dx + \frac{\partial f}{\partial y}dy

The partial derivatives provide local sensitivity coefficients; the coordinate changes say how much of each sensitivity is activated.

Connection To Constrained Optimization

Lesson 33 used the stationarity equation:

xL(x,λ)=0\nabla_x\mathcal{L}(x,\lambda)=0

For L(x,y,λ)\mathcal{L}(x,y,\lambda), that compact equation expands into separate partial-derivative equations:

Lx=0,Ly=0\frac{\partial\mathcal{L}}{\partial x}=0, \qquad \frac{\partial\mathcal{L}}{\partial y}=0

The constraint is recovered from:

Lλ=0\frac{\partial\mathcal{L}}{\partial\lambda}=0

Partial derivatives are therefore the mechanics underneath the Lagrange-multiplier system you already solved.

Connection To Machine Learning

Suppose a loss depends on a weight ww and bias bb:

L(w,b)L(w,b)

Gradient descent updates them using different partial derivatives:

wt+1=wtηLww_{t+1}=w_t-\eta\frac{\partial L}{\partial w} bt+1=btηLbb_{t+1}=b_t-\eta\frac{\partial L}{\partial b}

Each derivative measures the local effect of changing one parameter while conceptually holding the others fixed. The optimizer then changes all parameters together using the assembled gradient.

Connection To Programming

A scalar field with two inputs can be represented as:

type ScalarField2 = (x: number, y: number) => number;

A centered finite-difference approximation to the xx partial is:

function partialX(
  f: ScalarField2,
  x: number,
  y: number,
  h = 1e-5,
): number {
  return (f(x + h, y) - f(x - h, y)) / (2 * h);
}

Notice that both function evaluations use the same yy. Only xx changes.

The corresponding yy partial keeps xx fixed:

function partialY(
  f: ScalarField2,
  x: number,
  y: number,
  h = 1e-5,
): number {
  return (f(x, y + h) - f(x, y - h)) / (2 * h);
}

These numerical approximations are useful for checking analytic derivatives, although training systems usually obtain derivatives through automatic differentiation.

Common Mistakes

Differentiating every variable

When computing f/x\partial f/\partial x, all other independent variables are held constant.

Dropping a constant factor that contains another variable

For 3xy3xy, differentiating with respect to xx gives 3y3y, not 33 and not 00.

Confusing the function with its gradient

A scalar objective returns one number. Its partial derivatives are scalar components; the gradient collects them into a vector.

Treating a partial derivative as the whole change

f/x\partial f/\partial x describes sensitivity along one coordinate direction. If several inputs move, their first-order contributions must be combined.

Ignoring the evaluation point

f/x\partial f/\partial x is generally a function. A numerical slope appears only after evaluating it at a particular input.

Exercises

  1. In one sentence, distinguish an ordinary derivative df/dxdf/dx for f:RRf:\mathbb{R}\to\mathbb{R} from a partial derivative f/x\partial f/\partial x for f:R2Rf:\mathbb{R}^2\to\mathbb{R}.

  2. For f(x,y)=4x2+2xy+3y2f(x,y)=4x^2+2xy+3y^2, compute f/x\partial f/\partial x and f/y\partial f/\partial y.

  3. Evaluate both partial derivatives from Problem 2 at (x,y)=(1,1)(x,y)=(1,-1) and interpret their signs.

  4. For f(x,y)=x2sinyf(x,y)=x^2\sin y, compute both first partial derivatives. State explicitly which quantity is held fixed in each calculation.

  5. Explain geometrically what f/x(a,b)\partial f/\partial x(a,b) measures on the surface z=f(x,y)z=f(x,y).

  6. A loss is L(w,b)=(2w+b5)2L(w,b)=(2w+b-5)^2. Compute L/w\partial L/\partial w and L/b\partial L/\partial b using the one-variable chain rule inside each partial derivative.

  7. At a point, f/x=3\partial f/\partial x=3 and f/y=2\partial f/\partial y=-2. Estimate Δf\Delta f for Δx=0.1\Delta x=0.1 and Δy=0.05\Delta y=0.05 using the first-order approximation.

  8. For L(x,y,λ)=x2+y2+λ(x+y2)\mathcal{L}(x,y,\lambda)=x^2+y^2+\lambda(x+y-2), compute all three partial derivatives and explain what setting each one to zero enforces.

  9. Explain why one partial derivative alone cannot determine the steepest direction of a scalar field with multiple inputs.

  10. Write TypeScript-style pseudocode for a generic centered finite-difference function that approximates the partial derivative of a scalar field f:RnRf:\mathbb{R}^n\to\mathbb{R} with respect to coordinate ii. Make the scalar return type of ff explicit.

Summary

Next Lesson

Lesson 35 assembles coordinate sensitivities into gradients and directional derivatives, then interprets the gradient as a local linear approximation.