AI Wiki中文/EN

Backpropagation

反向传播

Apply the chain rule backward through a computation graph to efficiently differentiate a loss with respect to every parameter.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

Intuition: decompose an error into local influences

A prediction might multiply an input by a weight, apply a nonlinear function, and compare the result with a target. To improve the weight, ask how a small increase would change the loss. Perturbing every parameter separately is expensive. Backpropagation reuses intermediate values from the forward computation and transmits the influence of the final loss backward through the operations.

Backpropagation computes derivatives; it does not specify the parameter-update rule. Optimizers such as SGD or Adam consume the gradients and decide the updates. D2L explains forward computation, backward computation, and computation graphs. The classic 1986 paper helped popularize this training method, but does not establish that all the underlying mathematics originated that year. D2L, classic paper

A graph you can differentiate by hand

Let z=wxz=wx, y^=tanh⁡z\hat y=\tanh z, and L=(y^−y)2/2L=(\hat y-y)^2/2. The chain rule gives:

∂L∂w=(y^−y)(1−y^2)x.\frac{\partial L}{\partial w}=(\hat y-y)(1-\hat y^2)x.

The three factors are the local derivatives of loss with respect to prediction, prediction with respect to the intermediate value, and the intermediate value with respect to the weight. If a variable influences the loss through multiple paths, add their contributions. Missing that accumulation is a common error in a hand-written autodiff engine.

For z=Wh+bz=Wh+b, given an upstream gradient g=∂L/∂zg=\partial L/\partial z, local backpropagation gives ∂L/∂W=gh⊤\partial L/\partial W=gh^\top, ∂L/∂b=g\partial L/\partial b=g, and ∂L/∂h=W⊤g\partial L/\partial h=W^\top g. With batches, accumulate according to whether the loss sums or averages samples. Tensor shapes provide an immediate consistency check.

Reverse-mode automatic differentiation is especially suitable for a scalar loss with many parameters. In common differentiable operation graphs, its computation is usually a constant multiple of the forward computation, but intermediate activations must be stored or recomputed. Per-parameter central differences instead require roughly twice as many forward evaluations as there are parameters, making them more useful for small checks. Baydin et al., reverse-mode AD, Section 3.2

With column vectors h∈Rnh\in\mathbb R^n, z,g∈Rmz,g\in\mathbb R^m and W∈Rm×nW\in\mathbb R^{m\times n}, the outer product gh⊤gh^\top has exactly the weight matrix’s shape. From dL=g⊤(dW h+W dh+db)dL=g^\top(dW\,h+W\,dh+db), collecting coefficients yields the three local gradients above. For a batch-mean loss, each example’s contribution carries the factor 1/B1/B.

A complete reverse traversal starts with loss sensitivity one, visits operations in reverse topological order, and adds each local contribution into its input’s accumulated sensitivity. Shared operands must receive every contribution. For u=wxu=wx and L=u2+3uL=u^2+3u, the weight gradient is (2u+3)x(2u+3)x; differentiating only the square path omits 3x3x. D2L, Section 5.3.3

Compare analytical and numerical gradients

import math

def loss(w, x=2.0, target=1.0):
    prediction = math.tanh(w*x)
    return 0.5*(prediction-target)**2

w, x, target = 0.5, 2.0, 1.0
prediction = math.tanh(w*x)
analytical = (prediction-target)*(1-prediction**2)*x
epsilon = 1e-5
numerical = (loss(w+epsilon)-loss(w-epsilon))/(2*epsilon)
assert abs(analytical-numerical) < 1e-8
print(analytical, numerical)

The finite-difference step balances truncation and floating-point error; smaller is not always better. At a nondifferentiable point such as a ReLU kink, numerical differences may disagree with a framework’s chosen subgradient. Fix randomness, disable unstable stochastic paths, and choose small test inputs away from such points.

Correct gradients do not guarantee successful training

Correct derivatives can still be tiny because of saturation. Repeated multiplications can amplify or suppress gradients. Excessive learning rates can cause divergence. Training also depends on initialization, data scale, objectives, and optimizer settings.

Engineering failures include accidentally detaching a graph, omitting parameters from an optimizer, failing to clear accumulated gradients, and misusing training statistics at evaluation time. Autodiff differentiates the graph that actually ran; it does not know whether that graph matches your intention.

Experiments and exercises

  1. What is the weight gradient when x=0x=0? Answer: zero, because the weight no longer affects the prediction.
  2. Differentiate u2+3uu^2+3u. Answer: 2u+32u+3; add both computational paths.
  3. Increase ww until tanh saturates. Answer: 1−y^21-\hat y^2 approaches zero and compresses the local gradient.
  4. Does a negative gradient mean the weight should increase? Answer: under ordinary gradient descent with a positive learning rate, yes; the actual rule still depends on the optimizer.

Research connections

Autodiff frameworks organize reverse computation through vector-Jacobian products. Activation checkpointing addresses memory use; higher-order derivatives describe how gradients change. Evaluate numerical correctness, memory efficiency, and optimization effectiveness separately to diagnose why training slows or fails.

Make a prediction, then check

Which operation is backpropagation responsible for?

Choose one answer

Learning objectives

  • Apply the chain rule through a graph and accumulate shared-node gradients.
  • Check analytical gradients of a small model with finite differences.

Prerequisites & connections

Explore related knowledge →

References

  1. Forward Propagation, Backward Propagation, and Computational Graphs
    D2L authors
  2. Learning representations by back-propagating errors
    David E. Rumelhart; Geoffrey E. Hinton; Ronald J. Williams · 1986
  3. Automatic Differentiation in Machine Learning: a Survey
    Atilim Gunes Baydin; Barak A. Pearlmutter; Alexey Andreyevich Radul; Jeffrey Mark Siskind · 2018

Version2 · Editorial status: Recorded technical checks completed

View technical check record (AI)

Codex · 2026-09-05 · Codex technical check: cited-source comparison, re-derivation and recalculation, bilingual argument comparison, and executed Python assertions; no independent human peer review.

  • Finite-differenced every W, b, and h entry of a two-output affine layer, plus tanh and shared-path derivatives; checked the batch-mean 1/B factor.
  • Separated differentiation, optimization, and training success; corrected the Chinese activation-checkpointing terminology.
  • Compared the main claims, assumptions, examples, exercises, and citations across languages; structural checks additionally compare display equations, code, and source IDs. This is an AI technical check, not independent human peer review.

Source locations checked:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.