Lensa ML
Lensa ML

How the Chain Rule Works

Step 1 of 6

Functions Inside Functions

What happens when you compose f(g(x))?

input1.5g(x)2x + 1g(x)4.0f(g)f(g(x))16.00
Drag to change x

Composition means plugging one function into another. Here g(x) = 2x + 1 runs first, then its output feeds into f(g) = g². The value flows left to right: x = 1.5 → g = 4.0 → f = 16.00.

Step 1 of 6: Functions Inside Functions

What happens when you compose f(g(x))?

input1.5g(x)2x + 1g(x)4.0f(g)f(g(x))16.00
Drag to change x

Composition means plugging one function into another. Here g(x) = 2x + 1 runs first, then its output feeds into f(g) = g². The value flows left to right: x = 1.5 → g = 4.0 → f = 16.00.

Step 2 of 6: Local Derivatives

Each function has its own rate of change

x1.5g(x) = 2x+1= 4.0f(g) = g²= 16.00output16.00
Change x

Each function has a local derivative -- its own rate of change. For g(x) = 2x + 1, dg/dx = 2 (always 2). For f(g) = g², df/dg = 2g = 8.0 (depends on what g outputs).

Step 3 of 6: Multiply the Chain

The total derivative is the product of local ones

x1.5g(x) = 2x+1= 4.00f(g) = g²= 16.00output16.00
Change x

The chain rule says: multiply the local derivatives together. At x = 1.5: g'(x) = 2 × f'(g) = 8.0 = 16.0. A tiny nudge to x gets amplified 16.0× at the output.

Step 4 of 6: A Longer Chain

What about f(g(h(x)))?

x1.00h(x)sin(x)= 0.841g(h)2h+1= 2.683f(g)= 7.198
Change x

With three functions composed as f(g(h(x))), the chain rule still works the same way -- just multiply all the local derivatives together. Notice how dh/dx = cos(1.0) = 0.540 can shrink or flip the total derivative depending on x.

Step 5 of 6: Chains in Networks

Each layer is a function — backprop IS the chain rule

InputHidden 1Hidden 2Output

Each layer in a neural network is just a function. The forward pass goes left to right, and backpropagation sends gradients right to left. At each layer, multiply by the local derivative -- that's the chain rule in action.

Step 6 of 6: Why It Matters

Without the chain rule, deep learning wouldn't work

Gradient magnitude as it flows backward← gradient flows backward (each layer multiplies by 0.4)1.00Output×0.40.40Layer 5×0.40.16Layer 4×0.40.064Layer 3×0.40.026Layer 2×0.40.010Layer 1×0.44e-3Input

In a shallow network, the gradient passes through few multiplications and stays strong. In a deep network, the chain rule multiplies many local derivatives together. If those derivatives are small (< 1), the gradient vanishes -- early layers barely learn. Understanding the chain rule led to innovations like ResNets, batch normalization, and careful initialization that keep gradients flowing.