Skip to content
AI360XpertBeta
Core ML

Visual explainer

Backpropagation

Trace cached values, local derivatives and branch sums to compute weight gradients, then separate backpropagation from the optimizer update.

A prediction misses its target before gradients are computed.
A prediction misses its target before gradients are computed.

Backpropagation computes how a loss changes with each parameter. An optimizer then uses those gradients to change the parameters. The picture starts with a prediction of 1 and target of 3: the residual is −2 and squared loss is 4. The square's area represents squared error; it uses its own area scale, rather than the prediction axis scale.

Our synthetic scalar model is y=wx+by=wx+b, with input x=2x=2, weight w=0.5w=0.5 and bias b=0b=0. Unless explicitly changed below, the target stays 3 and L=(y−3)2L=(y-3)^2. A gradient measures local sensitivity, rather than assigning a causal share of the loss to each weight.

Reverse Through Multiplication

A multiply gate sends each gradient through the other input.
A multiply gate sends each gradient through the other input.

For the product y=wxy=wx, the local derivatives are ∂y/∂w=x=2\partial y/\partial w=x=2 and ∂y/∂x=w=0.5\partial y/\partial x=w=0.5. White routes show the product's dependencies; mint arrows carry sensitivities backward. An incoming ∂L/∂y=−4\partial L/\partial y=-4 produces ∂L/∂w=−8\partial L/\partial w=-8 and ∂L/∂x=−2\partial L/\partial x=-2. The gradient on the fixed input helps explain the rule, even though training usually updates parameters.

Evaluate Before Differentiating

The forward pass computes and retains values needed backward.
The forward pass computes and retains values needed backward.

Follow multiplication, subtraction and squaring from left to right: 2(0.5)=12(0.5)=1, 1−3=−21-3=-2, and (−2)2=4(-2)^2=4. The dashed ring selects the cached residual. Backward computation needs that value to differentiate the square, and the cached input to differentiate multiplication. Bias zero is omitted here to keep this single route visible. Retaining intermediate values costs memory; checkpointing can trade recomputation for storage.

Differentiate The Loss

The square-loss slope sends a negative gradient toward the prediction.
The square-loss slope sends a negative gradient toward the prediction.

The curve plots squared loss against prediction, with the target at its minimum. The dashed tangent touches the current point (y,L)=(1,4)(y,L)=(1,4). Its data-space slope is 2(y−3)=−42(y-3)=-4: increasing the prediction slightly reduces loss. Combining that derivative with the product's local slope 2 gives the weight gradient −8. Loss 4 and gradient −4 are different quantities.

Add Contributions From Branches

Gradients from two uses of one value add at the shared input.
Gradients from two uses of one value add at the shared input.

This separate fan-out example uses q=a+a2q=a+a^2 at a=2a=2, so q=6q=6. Read the reverse-only graph from the unit seed at the right. The addition sends sensitivity 1 down each branch; the square branch multiplies its sensitivity by 2a=42a=4. At the shared input, contributions add: dq/da=1+4=5dq/da=1+4=5. The left plus circle represents gradient accumulation, while the right plus circle is the original forward addition. Multiplication applies along a path; addition combines different paths. See the Stanford computational-graph notes.

Let The Optimizer Step

An optimizer uses the computed gradient to move the weight.
An optimizer uses the computed gradient to move the weight.

Return to the original scalar model and hold bias at zero. Gradient descent with learning rate η=0.1\eta=0.1 gives wnew=0.5−0.1(−8)=1.3w_{new}=0.5-0.1(-8)=1.3. The mint displacement is +0.8; its length uses the weight axis. The upper arrow shows only the gradient's direction. Re-evaluation gives y=2.6y=2.6 and L=0.16L=0.16. The cross at weight 1.5 marks the zero-loss solution for this one example, not a guarantee about unseen data. Step size still matters even when derivatives are correct.

Small Factors Compound

Repeated quarter factors shrink backward sensitivity.
Repeated quarter factors shrink backward sensitivity.

The three bars share a linear length scale and show sensitivities 1, .25 and .0625 after zero, one and two backward factors. Ten factors of .25 give .2510=0.0000009536743.25^{10}=0.0000009536743. These are deliberately chosen complete local derivatives. A sigmoid activation's slope bound alone does not bound a full layer derivative, which also includes weights. This small finite value is not automatically numerical zero. The vanishing-gradient visual develops that distinction.

Check A Gradient Numerically

A central difference checks the derivative at the current weight.
A central difference checks the derivative at the current weight.

This is an explicit local zoom of L(w)=(2w−3)2L(w)=(2w-3)^2. Perturbing the current weight by ϵ=.01\epsilon=.01 gives L(.49)=4.0804L(.49)=4.0804 and L(.51)=3.9204L(.51)=3.9204. The central difference is (3.9204−4.0804)/.02=−8(3.9204-4.0804)/.02=-8, agreeing with backpropagation. The secant nearly overlays the curve at this zoom. Central differences are approximate in general; this quadratic happens to give the exact slope in exact arithmetic. Floating-point cancellation and nondifferentiable points complicate checks. Perturbing every parameter is useful for debugging small models but expensive for training large ones.

Reuse The Reverse Sweep

One reverse sweep supplies both weight and bias gradients.
One reverse sweep supplies both weight and bias gradients.

Restoring bias to the graph, addition passes the incoming −4 to both bias and product. Multiplication then gives the weight gradient −8. One forward evaluation and a reverse traversal of this scalar-loss graph supply both derivatives, sharing intermediate work. Runtime still grows with graph size, and floating-point derivatives are not infinitely precise. If we now train both parameters at learning rate .1, their simultaneous updates are w=1.3,b=.4w=1.3,b=.4, giving prediction 3 and zero loss for this sample. That differs from the earlier weight-only update.

Where It Breaks

An inactive ReLU blocks this weight's data-loss gradient.
An inactive ReLU blocks this weight's data-loss gradient.

Change the model to y=ReLU⁡(wx−2)y=\operatorname{ReLU}(wx-2) while keeping x=2,w=.5x=2,w=.5 and target 3. Preactivation is −1, output is zero and loss is 9. The loss sends gradient −6 toward the output, but ReLU's local slope at −1 is zero, so this weight receives zero data-loss gradient. The stop bar makes that blocked route visible. Backpropagation can be correct while this sample provides no useful update through the inactive route. Other samples, paths or parameter changes may reactivate the unit; regularization can also contribute a separate gradient.

The Quick Version

  • Forward evaluation retains values needed for local derivatives.
  • Chain-rule products transport sensitivities along paths.
  • Different uses of a shared value contribute summed gradients.
  • Optimizers apply updates after gradients are computed.
  • Numerical differences help check small differentiable graphs.
  • Correct gradients do not guarantee useful training progress.