Lesson 5 of 7 · Calculus

Chain rule

The chain rule differentiates composite functions by multiplying local derivatives, and it is the mathematics on which backpropagation rests in every neural network.

Mathematics for machine learning Difficulty: Intermediate Study time: 55 min
Lesson contents
  1. Introduction
  2. Why study this topic?
  3. Theoretical explanation
  4. Mathematical definitions
  5. The equations
  6. Worked example
  7. Application in AI
  8. Interactive lab
  9. Practice exercises
  10. Summary
  11. Short quiz
  12. Related lessons

1 Introduction

A neural network is a chain of composite functions: multiplication by the weights, then an activation function, then another layer, then a loss function. Chain rule tells us how to differentiate this composition.

Learning objectives

  • Differentiating composite functions
  • Drawing a computational graph and applying the rule on it
  • Computing the gradient of a single neuron by hand
  • Checking the gradient numerically

2 Why study this topic?

The backpropagation algorithm is nothing but an organized application of the chain rule from the output towards the input, reusing intermediate results. Understanding it explains vanishing and exploding gradients, since both come from multiplying a large number of local derivatives.

3 Theoretical explanation

The one-variable formula

If \(y = f(u)\) and \(u = g(x)\), then:

\[\frac{dy}{dx} = \frac{dy}{du}\cdot\frac{du}{dx} = f'(g(x))\,g'(x)\]

The multivariable formula

If \(L\) depends on \(x\) through intermediate variables \(u_1,\dots,u_k\), we sum over the paths:

\[\frac{\partial L}{\partial x} = \sum_{i=1}^{k}\frac{\partial L}{\partial u_i}\frac{\partial u_i}{\partial x}\]

The computational graph

We write the computation as nodes: \(z = wx + b\), then \(a = \sigma(z)\), then \(L = (a - y)^2\). In the forward pass we compute the values, and in the backward pass we multiply the local derivatives going back from \(L\):

\[\frac{\partial L}{\partial w} = \underbrace{2(a-y)}_{\partial L/\partial a}\cdot\underbrace{a(1-a)}_{\partial a/\partial z}\cdot\underbrace{x}_{\partial z/\partial w}\]

Numerical gradient check

To make sure a gradient is correct we compare it with the central difference \(\frac{L(w+\varepsilon) - L(w-\varepsilon)}{2\varepsilon}\); this is gradient checking.

4 Mathematical definitions

Chain rule
The derivative of a composite function equals the derivative of the outer function evaluated at the inner one, times the derivative of the inner one.
\[(f\circ g)'(x) = f'(g(x))\,g'(x)\]
Backpropagation
Applying the chain rule on the computational graph from the loss to the weights to compute all the partial derivatives efficiently.
\[\frac{\partial L}{\partial w} = \frac{\partial L}{\partial a}\frac{\partial a}{\partial z}\frac{\partial z}{\partial w}\]
Chain rule
A rule for differentiating composite functions by multiplying the derivative of the outer function by that of the inner one; it is the basis of backpropagation.
\[\frac{dy}{dx}=\frac{dy}{du}\frac{du}{dx}\]
Backpropagation
An algorithm that computes the gradient of the loss with respect to all network weights by applying the chain rule from the output towards the input while reusing intermediate values.
\[\frac{\partial L}{\partial w}=\frac{\partial L}{\partial a}\frac{\partial a}{\partial z}\frac{\partial z}{\partial w}\]

5 The equations

\[\frac{dy}{dx} = \frac{dy}{du}\frac{du}{dx}\]
Chain rule
\[\frac{\partial L}{\partial x} = \sum_i \frac{\partial L}{\partial u_i}\frac{\partial u_i}{\partial x}\]
The multi-path formula
\[\frac{\partial L}{\partial w} = 2(a-y)\,a(1-a)\,x\]
The gradient of a Sigmoid neuron with the squared loss

6 Step-by-step worked example

Differentiating a composite function

Problem: Differentiate \(y = (3x^2 + 1)^4\).

  1. The inner function is \(u = 3x^2 + 1\), with derivative \(6x\).
  2. The outer one is \(u^4\), with derivative \(4u^3\).
  3. Multiply: \(4(3x^2+1)^3\cdot 6x\).

Result: \(y' = 24x(3x^2+1)^3\)

The gradient of a single neuron

Problem: A neuron: \(z = wx + b\), \(a = \sigma(z)\), \(L = (a-y)^2\), with \(x = 1.5, w = 0.8, b = -0.2, y = 1\). Compute \(\partial L/\partial w\).

  1. Forward: \(z = 1\), \(a = \sigma(z) \approx 0.7311\), \(L \approx 0.0723\).
  2. \(\partial L/\partial a = 2(a - 1) \approx -0.5379\).
  3. \(\partial a/\partial z = a(1-a) \approx 0.1966\).
  4. \(\partial z/\partial w = x = 1.5\).
  5. Multiply: \(\partial L/\partial w \approx -0.1586\). The numerical central difference gives the same value to six decimal places.

Result: \(\partial L/\partial w \approx -0.1586\)

7 Practical application in AI

  • Backpropagation: Deep learning frameworks compute gradients automatically (automatic differentiation) by applying the chain rule on the computational graph.
  • Vanishing and exploding gradients: The gradient in the early layers is a product of many derivatives; if they are smaller than 1 it vanishes, and if they are larger it explodes.
  • Residual connections: The connection \(y = x + F(x)\) in ResNet adds a path whose derivative is 1, which helps the gradient flow.

8 Interactive lab linked to the lesson

Plotting functions, computing the derivative numerically and drawing the tangent, partial derivatives and the gradient of a two-variable function, the chain rule step by step, and numerical integration with the trapezoidal and Simpson's rules.

Open the Calculus Lab

9 Practice exercises

Review questions for reflection

  • Why is backpropagation computed from the output towards the input and not the other way?
  • How do residual connections help train very deep networks?

Common mistakes

  • Forgetting to multiply by the derivative of the inner function.
  • Evaluating the derivative of the outer function at x instead of g(x).
  • Not summing over paths when a variable affects the loss in more than one way.

Practice 5 questions from the question bank for this lesson

10 Lesson summary

  • The derivative of a composition is the product of the local derivatives.
  • With several paths, we sum their contributions.
  • Backpropagation is the chain rule applied efficiently on the computational graph.

11 Short quiz

5 questions · 10 minutes · pass mark 60% — passing it completes the lesson.