Skip to content

Chain Rule of Calculus Explained With Backpropagation

The chain rule of calculus says a composed function changes at the product of each step's rate, with each rate taken at that step's own input. Goodfellow, Bengio, and Courville introduce it in Section 6.5.2 of Deep Learning as the tool "used to compute the derivatives of functions formed by composing other functions whose derivatives are known," and note that it should not be confused with the chain rule of probability. Their scalar statement: let y = g(x) and z = f(g(x)) = f(y). Then dz/dx = (dz/dy)(dy/dx), equation 6.44. They connect it to training right away: "Back-propagation is an algorithm that computes the chain rule, with a specific order of operations that is highly efficient."

The same section extends the rule to vectors. If x has m entries, y has n, and z is a single number, then each partial derivative of z with respect to x_i sums over the entries of y, and in matrix form "the gradient of a variable x can be obtained by multiplying a Jacobian matrix ∂y/∂x by a gradient ∇y z." Back-propagation, they write, "consists of performing such a Jacobian-gradient product for each operation in the graph." Their equation 6.46 writes it as ∇ₓz = (∂y/∂x)ᵀ ∇ᵧz, where ∂y/∂x is the n × m Jacobian of g, so the transpose is what makes the shapes fit. So the vector version is the same rule with the factors multiplied in a particular order, and the order matters because matrix products do not commute in general.

Try it by hand. Let g(x) = x² + 1 and f(y) = y², so z = f(g(x)) = (x² + 1)². Then g'(x) = 2x and f'(y) = 2y. At x = 1, the inner function gives y = g(1) = 2. The chain rule says dz/dx = f'(g(1)) · g'(1) = f'(2) · g'(1) = 4 · 2 = 8. Check it a second way: expanding gives z = x⁴ + 2x² + 1, whose derivative 4x³ + 4x equals 8 at x = 1. A numerical check agrees. The difference quotient (z(1 + h) − z(1)) / h equals 8 + 8h + 4h² + h³ exactly, so it is 8.8410 for h = 0.1, 8.0804 for h = 0.01, and 8.0080 for h = 0.001, closing in on 8. Now the two usual slips. If you evaluate f' at x = 1 instead of at g(1) = 2, you get f'(1) · g'(1) = 2 · 2 = 4, half the right answer. If you add the two local rates, you get 4 + 2 = 6. Neither is the slope. The same example at x = 2 gives 10 · 4 = 40 by the rule, 4 · 4 = 16 from the wrong argument, and 10 + 4 = 14 from adding. All numbers were computed in exact fractions, and the functions are invented.

The trap: multiplying the right derivatives at the wrong points, or adding them. Each factor is a local rate, so it has to be evaluated where that step actually sits in the computation: the inner function's derivative at x, the outer function's derivative at the inner function's output. A caution of my own, not from the section I read: in a network the points are the stored values from the forward pass, which is one reason back-propagation keeps them. Also, the rule assumes both functions are differentiable at the points that matter. At a kink, such as a rectified linear unit at exactly zero, frameworks pick a convention.

Prof Rod's rule: multiply the local rates along the chain, and evaluate each one at the value that actually reached that step.

Goodfellow, Bengio, and Courville, Deep Learning, MIT Press, 2016, Section 6.5.2 Chain Rule of Calculus, pp. 201–203