1.2.14Calculus & Optimization Basics

Chain rule for multivariate functions (backprop foundation)

2,737 words12 min readdifficulty · medium

What Is the Multivariate Chain Rule?

WHY does this work? Because when xx changes by a tiny amount Δx\Delta x:

  1. It causes uu to change by uxΔx\approx \frac{\partial u}{\partial x} \Delta x
  2. That change in uu causes zz to change by zuuxΔx\approx \frac{\partial z}{\partial u} \cdot \frac{\partial u}{\partial x} \Delta x
  3. Simultaneously, xx also affects vv, which affects zz through the second path
  4. The total change in zz is the sum of both pathway contributions

Derivation from First Principles

Let's build this from the ground truth of limits and differentials.

Step 1: Start with the single-variable chain rule For y=f(u)y = f(u) and u=g(x)u = g(x), we know: dydx=dydududx\frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dx}

WHY? Because ΔydyduΔu\Delta y \approx \frac{dy}{du} \Delta u and ΔududxΔx\Delta u \approx \frac{du}{dx} \Delta x, so: ΔydydududxΔx\Delta y \approx \frac{dy}{du} \cdot \frac{du}{dx} \cdot \Delta x

Dividing by Δx\Delta x and taking the limit gives chain rule.

Step 2: Generalize to multiple intermediate variables Now let z=f(u,v)z = f(u, v) where both uu and vv depend on xx.

From the definition of partial derivatives: dz=fudu+fvdvdz = \frac{\partial f}{\partial u} du + \frac{\partial f}{\partial v} dv

This is the total differential of ff. It says: "A tiny change in zz comes from contributions through uu and through vv."

Step 3: Express dudu and dvdv in terms of dxdx du=uxdx+uydydu = \frac{\partial u}{\partial x} dx + \frac{\partial u}{\partial y} dy dv=vxdx+vydydv = \frac{\partial v}{\partial x} dx + \frac{\partial v}{\partial y} dy

Step 4: Substitute and collect terms dz=fu(uxdx+uydy)+fv(vxdx+vydy)dz = \frac{\partial f}{\partial u}\left(\frac{\partial u}{\partial x} dx + \frac{\partial u}{\partial y} dy\right) + \frac{\partial f}{\partial v}\left(\frac{\partial v}{\partial x} dx + \frac{\partial v}{\partial y} dy\right)

Collecting coefficients of dxdx: dz=(fuux+fvvx)dx+(fuuy+fvvy)dydz = \left(\frac{\partial f}{\partial u} \frac{\partial u}{\partial x} + \frac{\partial f}{\partial v} \frac{\partial v}{\partial x}\right) dx + \left(\frac{\partial f}{\partial u} \frac{\partial u}{\partial y} + \frac{\partial f}{\partial v} \frac{\partial v}{\partial y}\right) dy

By definition, the coefficient of dxdx is zx\frac{\partial z}{\partial x}:

WHAT does this mean? Each term in the sum is one "pathway" from input xjx_j through intermediate uiu_i to output zz. We multiply along each path and sum all paths.

Figure — Chain rule for multivariate functions (backprop foundation)

Worked Examples

Connection to Backpropagation

In neural networks, we have a computational graph where:

  • Nodes = intermediate values (activations, weighted sums)
  • Edges = operations (matrix multiply, activation functions)
  • Goal = compute Lw\frac{\partial L}{\partial w} for every parameter ww

The backprop algorithm IS the chain rule:

  1. Forward pass: Compute zz from xx through all intermediates, storing values
  2. Backward pass: Compute Lx\frac{\partial L}{\partial x} by propagating gradients backward through the graph

At each node, if zz depends on u1,,unu_1, \ldots, u_n: Lui=Lzzui\frac{\partial L}{\partial u_i} = \frac{\partial L}{\partial z} \cdot \frac{\partial z}{\partial u_i}

This is called the local gradient (zui\frac{\partial z}{\partial u_i}) times the upstream gradient (Lz\frac{\partial L}{\partial z}).

WHY is this efficient? Instead of computing each Lwi\frac{\partial L}{\partial w_i} independently (expensive), we compute one backward pass that gives us ALL parameter gradients. This is reverse-mode automatic differentiation.

Recall Explain to a 12-Year-Old

Imagine you're making a smoothie. You put in strawberries and banas, blend them, add milk, blend again, then add honey. The final taste depends on every ingredient.

Now your friend says "this is too sweet!" You need to figure out: how much should I reduce the honey? But wait—the sweetness also depends on how ripe the banas were. And the banas affect the thickness, which affects how much honey you taste.

The chain rule is like being a "taste detective." You work backward:

  1. "The sweetness is 70% from honey, 30% from bananas"
  2. "The banana ripeness affects sweetness through how much sugar they have"
  3. "So to reduce sweetness by 10%, I need to reduce honey by 7% and use less-ripe bananas" In math, when one thing affects another, which affects another, we multiply the "effect sizes" along the chain. If there are multiple chains (like honey→sweetness and bananas→sweetness), we add them all up. That's the chain rule!

Why This Matters for AI/ML

  1. Backpropagation is just repeated chain rule application. Every neural network training relies on this.

  2. Computational graphs make it visual. Modern frameworks (PyTorch, TensorFlow) build a graph and automatically apply the chain rule.

  3. Efficiency: Without the chain rule, computing gradients would require O(n)O(n) forward passes for nn parameters. With it, one backward pass gives all gradients.

  4. Vanishing/exploding gradients happen when chain rule products become very small/large through many layers: Lw1=Lznznzn1z2z1z1w1\frac{\partial L}{\partial w_1} = \frac{\partial L}{\partial z_n} \cdot \frac{\partial z_n}{\partial z_{n-1}} \cdots \frac{\partial z_2}{\partial z_1} \cdot \frac{\partial z_1}{\partial w_1} If each zizi1<1\frac{\partial z_i}{\partial z_{i-1}} < 1, the product shrinks exponentially (vanishing). If >1> 1, it explodes.

Connections

  • Gradient Descent - Uses these gradients to update parameters
  • Backpropagation Algorithm - The practical implementation of chain rule in neural nets
  • Computational Graphs - Visual representation enabling automatic differentiation
  • Automatic Differentiation - Software that computes chain rule automatically
  • Jacobian and Hessian Matrices - Matrix forms of multivariate derivatives
  • Vanishing and Exploding Gradients - Problems arising from long chains
  • Activation Functions - Each has a derivative used in the chain
  • Loss Functions - Starting point (Ly\frac{\partial L}{\partial y}) for backprop

#flashcards/ai-ml

What is the multivariate chain rule formula for zx\frac{\partial z}{\partial x} when z=f(u,v)z = f(u,v) with u=g(x,y)u=g(x,y) and v=h(x,y)v=h(x,y)? :: zx=zuux+zvvx\frac{\partial z}{\partial x} = \frac{\partial z}{\partial u}\frac{\partial u}{\partial x} + \frac{\partial z}{\partial v}\frac{\partial v}{\partial x}. Sum over all paths from xx to zz, multiplying derivatives along each path.

Why do we SUM the terms in the multivariate chain rule? :: Because the input variable (e.g., xx) can affect the output through MULTIPLE intermediate variables (multiple paths). Each path contributes independently to the total change, so we add them.

In backpropagation, what is the "local gradient" at a node?
The derivative of that node's operation with respect to its immediate inputs: zui\frac{\partial z}{\partial u_i}. Combined with the upstream gradient Lz\frac{\partial L}{\partial z}, it gives Lui\frac{\partial L}{\partial u_i}.

What's the difference between zx\frac{\partial z}{\partial x} and dzdx\frac{dz}{dx}? :: zx\frac{\partial z}{\partial x} is the partial derivative holding other variables constant. dzdx\frac{dz}{dx} is the total derivative accounting for ALL ways xx affects zz, including indirect paths through other variables.

Why is backpropagation called "reverse mode" automatic differentiation?
Because it traverses the computational graph in REVERSE order (output to input), computing gradients by applying the chain rule backward. This gives all parameter gradients in one pass.
If z=u2+v2z = u^2 + v^2, u=2xu = 2x, v=3xv = 3x, what is zx\frac{\partial z}{\partial x}?
zx=zuux+zvvx=(2u)(2)+(2v)(3)=4u+6v=4(2x)+6(3x)=8x+18x=26x\frac{\partial z}{\partial x} = \frac{\partial z}{\partial u}\frac{\partial u}{\partial x} + \frac{\partial z}{\partial v}\frac{\partial v}{\partial x} = (2u)(2) + (2v)(3) = 4u + 6v = 4(2x) + 6(3x) = 8x + 18x = 26x.
What causes vanishing gradients in deep networks?
Long chains of multiplication in the chain rule. If each layer's gradient zi+1zi<1\frac{\partial z_{i+1}}{\partial z_i} < 1, the product Lw1\frac{\partial L}{\partial w_1} shrinks exponentially through many layers.
In the chain rule, which derivative comes first: the one closest to input or output?
Closest to OUTPUT. We work backward: Lossinput=Lossintermediateintermediateinput\frac{\partial \text{Loss}}{\partial \text{input}} = \frac{\partial \text{Loss}}{\partial \text{intermediate}} \cdot \frac{\partial \text{intermediate}}{\partial \text{input}}. "Outside-in" order.

Concept Map

affects

affects

contributes

contributes

generalizes to

derives

dz/dx = sum of paths

each path is

foundation of

traces error backward

error signal

Input x

Intermediate u

Intermediate v

Output z

Single-var chain rule

Multivariate chain rule

Total differential dz

Sum over paths

Product of derivatives

Backpropagation

Neural network neurons

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho bhai, multivariate chain rule samajhna hai toek factory ki assembly line imagine karo. Raw material ata hai,Station 1 se guzarta hai, fir Station 2, Station 3, aur finally finished product bahar aata hai. Agar final product mein defect hai, to tumhe backwards jake pata

Go deeper — visual, from zero

Test yourself — Calculus & Optimization Basics

Connections