Backpropagation
Backpropagation is the algorithm that computes the gradient of a neural network's loss with respect to every weight in a single backward sweep. It applies the chain rule layer by layer, reusing each layer's error signal to compute the one before it, so computing all gradients costs about as much as one extra forward pass.
When a prediction misses its target, training needs to know how sensitive the loss is to each weight. Testing weights one by one by re-running the network for each parameter does not scale. Backpropagation reverses the direction: it starts from the single scalar loss at the output, propagates each layer's local sensitivity backward through the connecting weights, and reuses those shared terms to obtain every weight gradient in one pass.
How Backpropagation Works
For a single weight w, the loss depends on w through a composition of
steps: w shifts a pre-activation z, which shifts an activation a, which
affects the loss L. The chain rule expresses this sensitivity as a product of
local derivatives: ∂L/∂w = ∂L/∂a · ∂a/∂z · ∂z/∂w.
Efficiency comes from grouping shared factors. Define each neuron's error
signal as δ = ∂L/∂z, the sensitivity of the loss to that neuron's
pre-activation. Two identities then drive the entire backward pass. First, the
gradient of any weight feeding a neuron is δ times the input on that
connection, so once a neuron's δ is known, each incoming weight gradient
takes one multiplication. Second, an earlier layer's δ is computed from the
next layer's by multiplying through the transposed weights and scaling by the
activation's local derivative: δ_prev = (Wᵀ·δ_next) ⊙ σ′(z), where ⊙
denotes element-wise multiplication. The transpose routes each error back along
the same connections used in the forward pass. For sigmoid, if s = σ(z) is
the cached activation, the derivative with respect to z is σ′(z) = s·(1 − s),
which is why the forward pass caches activations for reuse.
The backward pass follows a short loop: compute δ at the output, compute the
output weight gradients, propagate δ back one layer, compute that layer's
weight gradients, and repeat toward the inputs. Once all gradients are
computed, every weight takes one gradient-descent update, w ← w − η·∂L/∂w,
applying the rule from the Gradient Descent
lesson using the gradients produced by backpropagation.
Backpropagation Step by Step
The default run executes one training step on a 2 → 2 → 1 sigmoid network with
squared-error loss L = ½(ŷ − y)², small enough to verify every number by hand
while watching the canvas. The inputs and configuration are x = [0.50, 0.90],
target y = 1, and learning rate η = 0.5. Starting parameters are
W₁ = [0.15, 0.20; 0.25, 0.30] (one row per hidden neuron), b₁ = [0.35, 0.35], W₂ = [0.40, 0.45], and b₂ = 0.60. Biases are held fixed here to
focus on the six weights; in full training they update by the same rule since
∂L/∂b = δ.
- Forward · hidden.
a = σ(W₁·x + b₁) = [0.647, 0.678]. - Forward · output.
ŷ = σ(W₂·a + b₂) = 0.762. - Loss.
L = ½(0.762 − 1)² ≈ 0.0283. The prediction is below the target. - δ at the output.
δ_o = (ŷ − y)·ŷ(1−ŷ) = (−0.238)·(0.762·0.238) = −0.043. Because it is negative, raisingŷlowers the loss. - Output weight gradients.
∂L/∂W₂ = δ_o·a = [−0.028, −0.029], one gradient per hidden-to-output connection. - δ back to the hidden layer.
δ_h = (W₂·δ_o) ⊙ a(1−a) = [−0.004, −0.004]. Multiplying byW₂and the local sigmoid derivative carries the output error signal back to the hidden layer. - Input weight gradients.
∂L/∂W₁ = δ_h ⊗ x = [−0.0020, −0.0035, −0.0021, −0.0038](the 2×2 matrix in row-major order). These values are smaller because each passes through two sigmoid derivatives. - Update.
w ← w − η·∂L/∂wwithη = 0.5. All six gradients are negative, so all six weights increase, movingŷtoward the target. - Verify. Re-running the forward pass with the updated weights gives
ŷ = 0.766(up from 0.762) andL ≈ 0.0275(down from 0.0283). Training repeats this forward-backward-update cycle across many iterations.
On the canvas, amber moves left to right across the neurons during the forward
pass, then orange moves right to left as each δ is computed, while the header
switches from forward → to ← backward and back again for the verification
pass. Selecting the second input example (x = [0.1, 0.8]) runs the same
sequence with every activation, error signal, and gradient recomputed.
The Inputs panel also lets you vary the learning rate across η ∈ {0.1, 0.5, 0.9}. Changing η leaves the gradients unchanged at the starting weights,
while scaling the weight update and the resulting single-step loss reduction
(0.0002 at η = 0.1, 0.0008 at 0.5, and 0.0015 at 0.9 on the default input).
Multi-step convergence and instability across learning rates are covered in the
Gradient Descent lesson.
Why the Chain Rule Runs Backward
Evaluating sensitivity forward asks, for each weight individually, how changing
that single parameter affects the loss. That requires a separate full pass per
weight: O(weights) passes and O(weights²) total work. Evaluating backward
instead asks once per neuron how the loss changes with that neuron's
pre-activation, and every incoming weight's gradient follows from one
multiplication with that neuron's δ.
That asymmetry comes from the structure of the function: many parameters
feeding into one scalar loss. Derivatives of one output with respect to many
inputs are cheapest to compute from output to input because the shared factors
(δ) are reused instead of recomputed. In this dense network, the forward pass
costs O(weights), the backward pass costs O(weights), and caching forward
values requires O(activations) extra memory beyond storing the parameters and
their gradients. Computing every gradient for roughly the cost of one
additional forward pass is what makes training large networks practical. The
784 → 25 → 25 → 10 model in
Forward Propagation was trained with
this same backward gradient computation across its 20,475 weights, averaged
over mini-batches (using Adam, an adaptive variant of gradient descent, for the
update step while backpropagation supplies the gradients).
Edge Cases
- Already correct. If
ŷ = y, thenδ_o = 0, every weight gradient is zero, and no weight changes because the squared-error loss for this example is already at its minimum. - Saturated neurons. When a sigmoid activation sits near 0 or 1,
σ′ = a(1−a)approaches zero, attenuating theδpassed backward (the vanishing gradient effect). - Depth compounds it. Each layer multiplies in another factor
σ′ ≤ 0.25, so in deep sigmoid networksδshrinks geometrically (or explodes if weights are large). This 2→2→1 network is too shallow to display deep attenuation; sigmoid is used here for arithmetic clarity, whereas deeper networks commonly use ReLU to avoid repeated saturation.
Common Mistakes
- Dropping the
σ′term. A neuron'sδis the incoming error multiplied by its local derivative. Omittingσ′corrupts every nonzero upstream gradient. - Updating weights mid-backward. The backward pass must use the weights
from the forward pass. Change them early and later layers compute
δagainst a network that no longer exists. - Flipping a sign.
δ_o = (ŷ − y)·σ′and the update subtractsη·∂L/∂w. One sign error converts descent into ascent. - Recomputing instead of caching. The backward pass reuses forward activations; discarding them forces recomputation during the backward sweep. Activation checkpointing trades bounded recomputation for lower memory use, rather than reverting to one full pass per weight.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Embed this lesson in a blog, docs, or course?
Found a mistake or something unclear?