Composite functions are everywhere
When you bake a cake, the internal temperature of the cake depends on the time spent in the oven. And the doneness of the cake (fluffy, overdone, raw) depends on the internal temperature. Ultimately, the doneness depends on time, but through the temperature. It's a composite function: doneness = .
In mathematics, a composite function is simply a function applied to the result of another function. If and are two functions, the composition is written:
You first apply to , then to the result. It's a chain of transformations.
Why it's crucial in AI
A neural network is a composition of functions. Each layer applies a linear transformation (multiplication by a weight matrix + bias), followed by a nonlinear activation function. A 3-layer network is:
Where each represents a layer: matrix multiplication + activation. The input passes through layer after layer, each transformation applying to the result of the previous one. The deeper the network, the longer the composition.
The chain rule
How do you differentiate a composite function? The chain rule answers this question. If , then:
In words: the derivative of the composition is the derivative of the outer function (evaluated at the intermediate point) multiplied by the derivative of the inner function.
The gear analogy
Imagine two connected gears. The first turns at 3 revolutions per minute. The second, connected to the first with a 2:1 ratio, turns at revolutions per minute. To find the final speed, you multiply the ratios at each stage. The chain rule works exactly the same way: you multiply the "rates of change" at each step of the composition.
Leibniz notation
Leibniz notation makes the chain rule even more intuitive. If and , then:
It almost looks like the 's "cancel out" like in a fraction. That's not exactly what happens mathematically, but this intuition always works for the chain rule and helps you never make a mistake.
Concrete examples
Example 1: differentiate
Let and . Then:
Example 2: differentiate
Let and . Then:
This function is the shape of the Gaussian distribution, ubiquitous in ML.
Example 3: differentiate (cross-entropy)
Let and . Then:
This elegant result explains why cross-entropy works so well with the sigmoid: the gradient has a simple form and doesn't saturate.
Multi-link chain
When the composition is longer, the rule generalizes naturally. For :
You multiply the derivatives of each link in the chain, from the outside inward. For a network with layers, you get a product of terms, one for each layer traversed.
It's a product of matrices (or scalars in the simple case). And it's exactly what backpropagation computes.
Backpropagation, step by step
Let's take a simple 2-layer network to make things concrete. The input passes through:
- Layer 1: , then
- Layer 2: , then
- Loss:
To update , we need . By the chain rule:
Each term is simple to compute individually:
- : derivative of the loss with respect to the prediction
- : derivative of the sigmoid
- : the weight of layer 2
- : derivative of ReLU ( if , otherwise)
- : the input
Backpropagation consists of computing these terms from right to left (from output to input) and propagating them layer by layer. Hence the name: backward propagation of the gradient.
The computational graph
To organize all these computations, you use a computational graph. It's a directed acyclic graph (DAG) where:
- Each node represents an operation (addition, multiplication, activation)
- Each edge represents the data flow
The forward pass traverses the graph from input to output. The backward pass traverses it in reverse, multiplying local derivatives along each path. This is exactly what frameworks like PyTorch and TensorFlow do: they automatically build this graph during the forward pass, then traverse it backward to compute all gradients.

Autograd: automatic differentiation
In PyTorch, when you write loss.backward(), the framework:
- Traverses the computational graph backward
- Applies the chain rule at each node
- Accumulates gradients in the
.gradattribute of each parameter
You never need to differentiate by hand. But understanding the chain rule lets you diagnose problems when training isn't going well.
The vanishing gradient problem
The chain rule implies a product of derivatives. For a deep network (50, 100 layers), this product contains dozens of terms. If each derivative is less than 1 (which happens with the sigmoid, whose maximum derivative is ), the product tends exponentially toward zero:
The gradient arriving at the first layers is nearly zero: they learn almost nothing. This is the vanishing gradient problem, the bane of deep networks before modern innovations.

The solutions that enabled deep learning all rest on managing this product of derivatives:
- ReLU: its derivative is for positive values, which avoids multiplication by fractions
- Batch Normalization: stabilizes intermediate activations so that gradients stay in a reasonable range
- Residual connections (ResNet): add a direct path from input to output, creating a "shortcut" for the gradient that bypasses the destructive product
- LSTM and GRU: in sequences, gates control the gradient flow to prevent it from vanishing over long dependencies
Key takeaways
- The chain rule allows differentiating composite functions by multiplying the derivatives of each step
- A neural network is a function composition, so backpropagation is a direct application of the chain rule
- The computational graph organizes the forward and backward pass
- Automatic differentiation (autograd) applies the chain rule automatically in modern frameworks
- The vanishing gradient is a direct consequence of the product of derivatives in the chain rule: derivatives less than 1, multiplied many times, tend toward zero
- ReLU, batch norm, and residual connections are direct solutions to this mathematical problem
Where this is used. This rule is the engine of backpropagation, the algorithm that computes the correction to apply to every weight in a network.