← Tensor by Tensor
§ Math Fundamentals for AI
Tensor by Tensor · Complete Guide

Chain rule

Fourteen lessons that connect high-school mathematics to AI. Start with the plain-language explanation and a small example. Then read the full formulas, derivations, and Python code at your own pace.

How to study a lesson

Read the opening example. Try the question before revealing its answer. Then work through the detailed notes below it. You do not need to understand every proof on the first reading.

Symbols: a subscript such as xi identifies one entry. Σ means add a collection of terms. ∈ means “belongs to”. The symbol ≈ means “approximately equal”, not exactly equal. A parameter is an adjustable number in a model.

01.07 · The Chain Rule

Introduction

A tiny network first doubles input 3 and then squares the result. The final output changes through both stages, so the local changes, 2 and 12, must be multiplied to obtain 24.

Learning goal

Differentiate nested single-variable and multivariable functions step by step by multiplying the correct local derivatives.

Before you start

Function composition, basic derivative rules, multiplication of expressions, and introductory partial derivatives.

Lesson plan

  1. Trace an input through inner and outer functions before differentiating.
  2. Multiply local derivatives across two stages, then extend to longer chains.
  3. Apply the multivariable rule to a tiny network and verify each dependency.

Trace one chain. Let u=2x and y=u². At x=3, dy/du=12 and du/dx=2. Their product gives dy/dx=24. Each factor measures one local link.

The most important rule in ML

The chain rule lets you differentiate compositions of functions. Since neural networks are compositions of many layers, the chain rule is the mathematical engine of backpropagation.

Single-variable chain rule

If $y = f(g(x))$, then:

$$\frac{dy}{dx} = \frac{df}{dg}\cdot\frac{dg}{dx} = f'(g(x))\cdot g'(x)$$
Example

Let $y = \sin(x^2)$. Here $f(u) = \sin u$ and $g(x) = x^2$:

$$\frac{dy}{dx} = \cos(x^2) \cdot 2x = 2x\cos(x^2)$$

Multi-step compositions

For a chain $y = f_3(f_2(f_1(x)))$:

$$\frac{dy}{dx} = \frac{df_3}{df_2}\cdot\frac{df_2}{df_1}\cdot\frac{df_1}{dx}$$

Each factor is the derivative of one layer with respect to its input. This product structure is what makes backpropagation efficient.

Multivariable chain rule

When intermediate values are vectors, the derivatives become Jacobian matrices, and the chain rule becomes matrix multiplication:

$$\frac{\partial L}{\partial \mathbf{x}} = J_1^\top J_2^\top \cdots J_K^\top \frac{\partial L}{\partial \mathbf{y}}$$

where $J_i$ is the Jacobian of the $i$-th layer.

Chain rule for neural networks

A neural network computes $\mathbf{y} = f_K(f_{K-1}(\cdots f_1(\mathbf{x})))$. The gradient of the loss with respect to the input is:

$$\nabla_\mathbf{x} L = J_1^\top J_2^\top \cdots J_K^\top \nabla_\mathbf{y} L$$

This product of transposed Jacobians is computed right-to-left in backpropagation.

Leibniz notation makes the chain rule intuitive

The notation $\frac{dy}{dx} = \frac{dy}{du}\cdot\frac{du}{dx}$ looks like fractions cancelling. While this is technically an abuse of notation, it's a powerful mnemonic. For a chain $x \to a \to b \to y$:

$$\frac{dy}{dx} = \frac{dy}{db}\cdot\frac{db}{da}\cdot\frac{da}{dx}$$

Worked example: a tiny network

Consider a two-layer network with one neuron each:

$$a = W_1 x + b_1, \quad h = \sigma(a), \quad y = W_2 h + b_2, \quad L = (y - t)^2$$

To find $\frac{\partial L}{\partial W_1}$, apply the chain rule:

$$\frac{\partial L}{\partial W_1} = \frac{\partial L}{\partial y}\cdot\frac{\partial y}{\partial h}\cdot\frac{\partial h}{\partial a}\cdot\frac{\partial a}{\partial W_1}$$

Computing each factor:

  • $\frac{\partial L}{\partial y} = 2(y - t)$
  • $\frac{\partial y}{\partial h} = W_2$
  • $\frac{\partial h}{\partial a} = \sigma'(a) = h(1-h)$
  • $\frac{\partial a}{\partial W_1} = x$

Multiplying: $\frac{\partial L}{\partial W_1} = 2(y-t) \cdot W_2 \cdot h(1-h) \cdot x$

Vanishing gradients

If each Jacobian has spectral radius $< 1$, the product shrinks exponentially with depth. This is the vanishing gradient problem that killed deep sigmoid networks. Fixes: ReLU activations, residual connections, careful initialisation.