08 — Training

Backpropagation#

AlgorithmMathematical 1 min read
◆ The PatternChain rule applied through a network — how every weight gets its gradient

Backprop answers: "how much did each weight contribute to the error?" It uses the chain rule to propagate gradients backward from the loss to every parameter.

∂L/∂w₁ = ∂L/∂ŷ · ∂ŷ/∂h · ∂h/∂w₁
Chain Rule: multiply local gradients along the path from loss back to each weight
// Forward pass → loss → backward pass → weight update
Click Animate
Vanishing gradients: In deep networks, multiplying many small numbers (sigmoid derivatives ≤ 0.25) makes early-layer gradients near zero. Solutions: ReLU activations, batch norm, residual connections, gradient clipping (topic 14).
Pattern bridge: The chain rule propagates credit backward through layers — the same logic as information cascades in markets where effects ripple back.
Worked example

A one-unit network: h = ReLU(w₁x), y = w₂h, loss ½(y − t)². With x = 2, t = 1, w₁ = 0.5 and w₂ = −1.5, the chain rule gives ∂L/∂w₁ = 7.5 and ∂L/∂w₂ = −2.5. Nudging w₁ by a millionth and measuring the change gives 7.4999999999: the gradient check every hand-written backward pass should pass.

Where it misleads
  • Backprop computes gradients exactly; it says nothing about whether following them leads somewhere good.
  • At ReLU’s kink the derivative is undefined; frameworks pick a value, which rarely matters but breaks naive gradient checks exactly at zero.
  • Long chains multiply many factors, which is where vanishing and exploding gradients come from (see RNN).
In code
x, t = 2.0, 1.0                                  # input and target
w1, w2 = 0.5, -1.5

def loss(w1, w2):
    h = max(w1 * x, 0)                             # ReLU hidden unit
    return 0.5 * (w2 * h - t) ** 2

h = max(w1 * x, 0); y = w2 * h
dL_dy = y - t
grad_w2 = dL_dy * h                                # chain rule, backwards
grad_w1 = dL_dy * w2 * (1 if w1 * x > 0 else 0) * x
eps = 1e-6                                         # check against finite differences
num_w1 = (loss(w1 + eps, w2) - loss(w1 - eps, w2)) / (2 * eps)
Python, in your browser — edit the code and run it again

Assumes import numpy as np. Each snippet carries its own example numbers; the comments say which are made up or simulated.

Sources
  • D. E. Rumelhart, G. E. Hinton & R. J. Williams, “Learning Representations by Back-Propagating Errors”, Nature 323, 1986
  • A. G. Baydin, B. A. Pearlmutter, A. A. Radul & J. M. Siskind, “Automatic Differentiation in Machine Learning: A Survey”, Journal of Machine Learning Research 18(153), 2018

Last reviewed October 2026 · How this site checks its content · Report a mistake

Linked from 4
← Previous
Loss Functions
Open in the full reader, with the topic sidebar