§ Gradients · Interactive Graduate Primer
Tensor by Tensor · expanded

Backpropagation in depth

Learn how changing several inputs changes one output. Begin with partial derivatives, then explore gradient descent, backpropagation, and optimizers. The later sections include proofs and deeper mathematical detail.
A first reading path

Start with sections 2–4, then section 7. Return to the directional-derivative proof after you understand a gradient-descent update. An optimizer is a rule for updating the model’s adjustable numbers.

For L(a, b) = a² + 2b², the gradient at (1, 2) is [2, 8]. The first entry measures change along a while b stays fixed. The second measures change along b while a stays fixed. With step size 0.1, subtract [0.2, 0.8] to reach (0.8, 1.2). The loss falls from 9 to 3.52.

Check: why subtract the gradient?

The gradient points toward the steepest local increase under the usual Euclidean length measure. Subtracting it moves toward a local decrease when the step is small enough. A large step can still overshoot and increase the loss.

10. Backpropagation

Introduction

A simple calculation path changes an input by factor 2 and then changes loss by factor negative 3. Reverse-mode differentiation multiplies those local effects to obtain total derivative negative 6.

Learning goal

Trace reverse-mode gradients through a computation graph and connect the process to neural-network backpropagation equations.

Before you start

Chain rule, gradients, matrix-vector products, and basic neural-network layer notation.

Lesson plan

  1. Compare forward mode and reverse mode by the derivatives each one propagates.
  2. Read the four backpropagation equations and identify every local factor.
  3. Walk backward through the interactive graph while accumulating shared gradient contributions.

Trace one path: if x changes u by a factor of 2 and u changes the loss by a factor of -3, then x changes the loss by 2×(-3)=-6. Backpropagation reuses such local factors from output to input.

For a composition $L=\ell\circ f_K\circ\cdots\circ f_1(\theta)$, the chain rule gives $\nabla_\theta L = J_1^\top\cdots J_K^\top \nabla_{f_K}\ell$. Naively computing all $n$ components costs $O(n)$ forward passes. Backprop evaluates the entire gradient in one reverse pass.

10.1 Forward vs. reverse mode

ModeDirectionCostGood when
Forwardleft→right$n$ forward passes$n\ll m$
Reverseright→left1 forward + 1 reverse$m\ll n$ (always in ML)

10.2 The four backprop equations

For $z^{(\ell)}=W^{(\ell)}a^{(\ell-1)}+b^{(\ell)}$, $a^{(\ell)}=\sigma(z^{(\ell)})$, with $\delta^{(\ell)}=\nabla_{z^{(\ell)}}L$:

$$\begin{aligned} \delta^{(L)} &= \nabla_{a^{(L)}}L \odot \sigma'(z^{(L)}) \\ \delta^{(\ell)} &= (W^{(\ell+1)})^\top \delta^{(\ell+1)} \odot \sigma'(z^{(\ell)}) \\ \nabla_{W^{(\ell)}}L &= \delta^{(\ell)}(a^{(\ell-1)})^\top \\ \nabla_{b^{(\ell)}}L &= \delta^{(\ell)}. \end{aligned}$$

10.3 Computation-graph walkthrough

Trace forward and backward passes through a tiny three-operation graph. Edit $x$ and watch both values and gradients propagate. The "local grad" column is the derivative of each node with respect to its input; backprop multiplies them right-to-left.

Interactive · trace the chain rule through a small graph
NodeOpValueLocal gradUpstream $\bar v$Gradient at node
Graph: $x \xrightarrow{\times 2} a \xrightarrow{\sin} b \xrightarrow{(\cdot)^2} L$