The chain rule

Step 10 of 18 in Foundations · Theorem · Medium

The derivative of a composition is the product of the derivatives along the chain. This one rule is all backpropagation is.

The lesson

When one function feeds another, y = f(g(x)), their rates multiply: dy/dx = f′(g(x)) · g′(x). If g triples small changes and f doubles them, the pair multiplies them by six.

Worked example: y = sin(x²)
  1. Inner: u = x², so du/dx = 2x
  2. Outer: y = sin u, so dy/du = cos u
  3. Multiply: dy/dx = cos(x²) · 2x

Products need one more rule, (uv)′ = u′v + uv′. So for sin(x²)·eˣ the derivative is 2x·cos(x²)·eˣ + sin(x²)·eˣ.

A neural network is a long composition: loss(layer₃(layer₂(layer₁(x)))). Backpropagation is the chain rule applied from the loss backwards, reusing each intermediate derivative instead of recomputing it.

Common mistake. Evaluate the outer derivative at the inner value. The derivative of sin(x²) contains cos(x²), not cos(x).

Check yourself

  1. y = (2x + 1)³. What is dy/dx at x = 1?

    Show the answer

    54. Outer: 3u² with u = 3, giving 27. Inner: 2. Multiply: 54.

  2. What is the derivative of e^(x²)?

    • e^(x²)
    • 2x · e^(x²)
    • x² · e^(x² − 1)
    Show the answer

    2x · e^(x²). Outer derivative of eᵘ is eᵘ, evaluated at u = x². Inner derivative is 2x. Multiply them.

  3. z = 3y and y = x². What is dz/dx at x = 2?

    Show the answer

    12. dz/dy = 3 and dy/dx = 2x = 4. Multiply: 12.

Build it

  1. Chain by hand

    For f(x) = sin(x²)·eˣ, derive f′(x) by hand, then verify with your numerical derivative at 5 points.

    Done when: Hand result and numeric result agree to 1e-6.

    Runs in your browser: write the code on the map and the tests mark it.

Resources

Earn this star

On the map, your answers are marked and you log what you built. Finish the step and its star lights.

Open this step on the map