Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 4: Neural Network Foundations

Backpropagation: intuition

P4.725 min●●○ IntermediateReviewed Dec 2, 2026

Why this matters for machine learning

Training a network means changing every weight a little in the direction that lowers the loss. Backpropagation is how the network finds out, for every weight at once, how much that weight matters and in which direction.

Before this: The chain rule, Gradient descent, Multi-layer perceptrons, Loss functions

After this lesson you can

  • Explain the credit-assignment problem and why nudging each weight and re-running the network is too slow
  • Use "rates multiply along a path" on a short chain, and read a gradient as the blame of a weight
  • Describe the forward and backward passes on a computation graph, with the local rules for add, multiply and a nonlinearity and the rule that blames add
  • Say what backpropagation produces, what it costs, and what is done with the result

A network has just made a wrong prediction. The loss is one number. Inside the network are thousands, or millions, of weights, and training has to change them so that the next loss is smaller. But which weights are to blame for this mistake, and by how much?

This is the credit-assignment problem (or blame-assignment problem). A weight near the input and a weight near the output both affect the result, through different routes, and nobody told the network which one did the damage. Backpropagation answers the question for every weight at once, by sending the blame backward from the loss through the network in a single sweep.

This lesson builds the picture with small numbers. P4.8 then follows every number through a complete example, and P4.9 turns the rules into code.

A network small enough to see

Two inputs, two hidden units and one output, written as formulas (a hidden unit adds up its weighted inputs, then squashes the sum with the sigmoid σ\sigma; P4.3 and P4.4 introduce these units in full):

z1=w11x1+w12x2,h1=σ(z1)z2=w21x1+w22x2,h2=σ(z2)y^=v1h1+v2h2,L=(y^−y)2\begin{aligned} z_1 &= w_{11}x_1 + w_{12}x_2, & h_1 &= \sigma(z_1) \\ z_2 &= w_{21}x_1 + w_{22}x_2, & h_2 &= \sigma(z_2) \\ \hat{y} &= v_1 h_1 + v_2 h_2, & L &= (\hat{y} - y)^2 \end{aligned}

Here wjkw_{jk} is the weight from input kk into hidden unit jj, and vjv_j is the weight from hidden unit jj to the output. We leave out biases to keep the picture small (a bias is a weight whose input is always 1). That makes 6 weights. Take the input (x1,x2)=(1,2)(x_1, x_2) = (1, 2), the target y=1y = 1, and the weights w11=0.5w_{11} = 0.5, w12=−0.3w_{12} = -0.3, w21=0.8w_{21} = 0.8, w22=0.2w_{22} = 0.2, v1=1v_1 = 1, v2=−1.5v_2 = -1.5.

Going forward: z1=−0.1z_1 = -0.1, h1=0.475h_1 = 0.475, z2=1.2z_2 = 1.2, h2=0.769h_2 = 0.769, so y^=−0.678\hat{y} = -0.678 and the loss is L=2.81L = 2.81. The prediction is far from the target 11. Which of the 6 weights should change, in which direction, and how much?

What “blame” means: the gradient

The answer for one weight is its gradient ∂L/∂w\partial L/\partial w (P1.C4, P1.C5): how much the loss changes per unit change of that weight, with all other weights held fixed. It carries two pieces of information:

  • the sign gives the direction: a negative gradient means that raising the weight lowers the loss;
  • the size says how sensitive the loss is to that weight right now.

For this network ∂L/∂v2=−2.58\partial L/\partial v_2 = -2.58 (you will see where it comes from below). So raising v2v_2 by 0.010.01 should change the loss by about −2.58×0.01=−0.026-2.58 \times 0.01 = -0.026, a small drop. In this lesson we call the gradient of a value its blame: a large size means that nudging it matters a lot, and the sign says which way to nudge.

The slow way: nudge and re-run

Here is a method that needs no new ideas. To find the blame of v2v_2, change v2v_2 by a tiny amount ε\varepsilon, run the whole network forward again, and see how much the loss moved. Divide by ε\varepsilon. This finite-difference method (P1.C2, P1.C4) works, and it gives the right answer. (Nudging in both directions, which is more accurate, costs two passes per weight.)

The cost is the problem. Every weight needs its own nudge and its own forward pass:

  • 6 weights: 6 extra forward passes;
  • a network with a million weights: a million extra forward passes;
  • and that is for one training step. Training takes many steps.

Real networks cannot be trained this way. We need all the blames from a cost that does not grow with the number of weights.

The key idea: rates multiply along a path

Take a short chain. A value xx goes into one step, a=3xa = 3x, and then another, L=a2L = a^2. At x=2x = 2: a=6a = 6 and L=36L = 36.

Nudge xx up by a tiny amount. The first step makes aa move 3 times as much as xx. The second step, at a=6a=6, makes LL move 2a=122a = 12 times as much as aa. So LL moves 3×12=363 \times 12 = 36 times as much as xx: the rates multiply. This is the chain rule (P1.C3). A check by nudging: at x=2.01x = 2.01 we get a=6.03a = 6.03 and L=36.3609L = 36.3609, a rise of 0.36090.3609, which is about 36×0.0136 \times 0.01.

An everyday picture is a chain of currency exchanges. If one dollar buys 3 euros and one euro buys 12 yen, one dollar buys 3×12=363 \times 12 = 36 yen. P1.C3 used gears for the same idea. Where does the picture break? Exchange rates stay fixed, but the rate of a step in a network depends on where you stand, for example the 2a2a above used a=6a = 6, and it is only right for tiny nudges. Also, real exchange rates are positive, while a rate here can be negative (the output goes the other way), or zero (then nothing gets through, and the whole product is zero).

Now the useful part. Start at the end and walk back. The loss has blame ∂L/∂L=1\partial L/\partial L = 1. One step back, aa has blame 1212. One more step back, xx has blame 12×3=3612 \times 3 = 36. Each step back multiplies by one more rate, and every value on the way gets its blame as a by-product. In a network, the weights sit at different depths along many paths. Walking back from the loss once shares the work among all of them. Starting from each weight and walking forward would repeat the shared part of every path again and again, which is the nudging cost in disguise.

Forward, then backward, on a graph

To do this for any computation, write it as a computation graph (P1.C10): one box for each small calculation, with arrows for the values passed on. Then:

  1. Forward pass. Compute the values from the inputs to the loss, and store them, because the backward pass will need them.
  2. Backward pass. Start at the loss with blame 11. Go through the boxes in reverse order. Each box takes the blame arriving from above, multiplies it by its local rate, and hands the result to its inputs.

A local rate says how a box’s output changes when one input changes, ignoring everything else. Three kinds of box cover most of what you will meet. (P4.8 gives the full table and the exact derivative rules.)

  • Add copies the blame to each input, unchanged.
  • Multiply swaps: each input gets the blame times the other input.
  • A nonlinearity, such as the sigmoid or ReLU, scales the blame by its slope at the value it was given. The sigmoid’s slope is never above 0.250.25. ReLU’s slope is 11 or 00.

One more rule handles branching: if a value is used in more than one place, the blames from each use add up.

In our network a hidden unit hides an add and some multiplies inside one box. For a weight, the rules reduce to this: its blame is the blame at the sum it feeds, times the value it multiplied.

Below is the graph of our network. Play it, or press Step one at a time. Values go forward in blue. Blame goes backward in orange, and the thicker an edge, the larger the blame it carries. The table lists each weight with its gradient.

w₁₁w₁₂w₂₁w₂₂v₁v₂x₁ (input)x₁ = 1x₂ (input)x₂ = 2hidden 1?hidden 2?output?loss?

The setup

A network with two inputs, two hidden units (each squashes its sum with a sigmoid) and one output. It has 6 weights. For the input (1, 2) and target 1 we want to know which weights to blame for the loss. Press Step, or Play.

Change more numbers

The 6 weights and their blame

Blue: the weight. Orange: its gradient ∂L/∂weight, with a bar for its size. Tap a row to choose the weight for the nudge test. A negative gradient means: raise this weight to lower the loss.

Nudge test for w₁₁

Nudge size ε:

Run the backward pass, then press the button. It changes one weight by ε, re-runs the network and compares the change in the loss with the gradient × ε.

Finding all 6 gradients by nudging costs 6 extra forward passes. The backward pass found all 6 in one sweep. With a million weights it would be a million forward passes against one backward sweep.

Blame flowing backward. The forward pass (blue) computes every value from the inputs to the loss. The backward pass (orange) starts at the loss with blame 1 and hands blame back through the square, the output sum, the sigmoids and the first-layer weights. Edge width and the bars show the size of each gradient; the numbers show size and sign. After the backward pass, pick a weight and run the nudge test: it changes that weight by a small ε, re-runs the whole forward pass, and compares the real change in the loss with gradient × ε. With Lite Mode on, Play shows the finished result without animation. On a phone the graph runs top to bottom.

Things to try:

  1. Press Play and watch the blue values go forward, then the orange blame come back. Which weight gets the loudest blame, and which the quietest?
  2. Run the nudge test on v2v_2 with ε=0.01\varepsilon = 0.01, then on w11w_{11}. The real change in the loss should be close to gradient × ε\times\ \varepsilon. Then try ε=0.1\varepsilon = 0.1 and ε=0.001\varepsilon = 0.001.
  3. Move the Output weight v2v_2 slider to 00. Which gradients become 00, and why?

Reading the numbers

The widget’s default numbers give the following, from the loss backward:

  • Square. ∂L/∂y^=2(y^−y)=2×(−1.678)=−3.36\partial L/\partial \hat{y} = 2(\hat{y} - y) = 2 \times (-1.678) = -3.36. The prediction is too low, so raising y^\hat{y} would lower the loss.
  • Output sum (multiply swaps). The blame on v1v_1 is −3.36×h1=−1.59-3.36 \times h_1 = -1.59, and on v2v_2 it is −3.36×h2=−2.58-3.36 \times h_2 = -2.58. Each hidden unit receives the blame times its output weight: −3.36×1=−3.36-3.36 \times 1 = -3.36 for h1h_1 and −3.36×(−1.5)=+5.03-3.36 \times (-1.5) = +5.03 for h2h_2. The signs differ, because v2v_2 is negative: raising h2h_2 would push y^\hat{y} down.
  • Sigmoids (scale by the slope). The slopes are 0.2490.249 and 0.1780.178, so the blame on z1z_1 is −3.36×0.249=−0.84-3.36 \times 0.249 = -0.84 and on z2z_2 it is 5.03×0.178=+0.905.03 \times 0.178 = +0.90.
  • First-layer weights (multiply swaps). The blame on w11w_{11} is −0.84×x1=−0.84-0.84 \times x_1 = -0.84 and on w12w_{12} it is −0.84×x2=−1.67-0.84 \times x_2 = -1.67. For unit 2: w21w_{21} gets +0.90+0.90 and w22w_{22} gets +1.79+1.79.
  • A value used twice. The input x1x_1 feeds both hidden units, so its blame is the sum of two blames: 0.5×(−0.84)+0.8×0.90=−0.42+0.72=0.300.5 \times (-0.84) + 0.8 \times 0.90 = -0.42 + 0.72 = 0.30. (Inputs are data, so we do not use this number. The same rule matters for a hidden value that feeds many units.)

Two things to notice. The blame got smaller as it passed the sigmoids, because their slope is at most 0.250.25: a preview of a problem in deep networks (P4.16). And the smallest weight in the network by size, w22=0.2w_{22} = 0.2, has a blame of larger size (+1.79+1.79) than v1=1v_1 = 1 (−1.59-1.59). The size of a weight says little about the size of its blame. The blame depends on the input the weight multiplies and on the route to the loss.

What the blame is used for

Backpropagation measures. The next step uses the measurements. Gradient descent (P1.F3) moves each weight against its gradient, w←w−η ∂L/∂ww \leftarrow w - \eta\,\partial L/\partial w. With a learning rate η=0.1\eta = 0.1, one step on all 6 weights lowers the loss in this example from 2.812.81 to about 1.381.38. Training repeats three steps: forward pass, backward pass, update. Optimizers such as momentum and Adam (P4.13) change the update step, but they take the same gradients.

Why one backward sweep is cheap

  • Shared work. The blame at y^\hat{y} is computed once and used for all the weights below it. The blame at z1z_1 serves w11w_{11} and w12w_{12}. In a deep network, the blame at each layer is computed once and reused by everything in the layers beneath. Nothing is recomputed per weight.
  • The cost. A standard result in automatic differentiation is that the gradient of one output costs a small constant multiple of one forward pass, and the multiple does not grow with the number of weights. For networks dominated by large matrix products, the backward sweep alone is often described as roughly twice the forward pass. These are rules of thumb, not exact figures. So a million weights cost about a forward pass and a backward sweep, not a million extra forward passes.
  • Its name. Computing derivatives by sweeping from the output back to the inputs is reverse-mode automatic differentiation. Sweeping the other way, from the inputs forward, is forward mode, and it needs one sweep per input. A network has one output (the loss) and many inputs (the weights), so reverse mode wins. P1.C10 compares the two modes.
  • The price. The backward pass needs the values from the forward pass, so training keeps them in memory. This is one reason training needs more memory than running a trained network (P4.8).

Where the idea came from

The reverse sweep has a history older than neural networks, and who deserves the credit is debated. Linnainmaa described reverse-mode differentiation of programs in 1970. Werbos described how to use it to train networks in a 1974 thesis. A 1986 paper by Rumelhart, Hinton and Williams in Nature made backpropagation widely known and showed that hidden units can learn useful internal representations. Treat these as milestones, not as a complete record.

Intuition

Picture a company. A customer complains about the final product. The complaint goes back to the last department, which looks at how much its own work mattered, and passes a copy of the complaint to each team that fed it, scaled by how strongly that team’s output affected the result. Those teams pass it back to theirs, and so on. In the end, every person has a number that says how much their work mattered for this complaint, and in which direction they should change it.

Where does the picture break? These numbers are not shares of a pie: they do not add up to the complaint, they can be negative, and an add step gives the full blame to each input. They also describe only tiny changes from the way things are now. And the blame at a step depends on the values stored from the forward pass, so the process is not a free-floating “error signal”: it is arithmetic on stored numbers.

Math

The blame of w11w_{11} is the chain-rule product of the rates along the path from w11w_{11} to the loss:

∂L∂w11=∂L∂y^⋅∂y^∂h1⋅∂h1∂z1⋅∂z1∂w11=2(y^−y)⋅v1⋅h1(1−h1)⋅x1\begin{aligned} \frac{\partial L}{\partial w_{11}} &= \frac{\partial L}{\partial \hat{y}} \\ &\quad\cdot\frac{\partial \hat{y}}{\partial h_1}\cdot\frac{\partial h_1}{\partial z_1} \\ &\quad\cdot\frac{\partial z_1}{\partial w_{11}} \\ &= 2(\hat{y}-y)\cdot v_1 \\ &\quad\cdot h_1(1-h_1)\cdot x_1 \end{aligned}

With the numbers: (−3.36)×1×0.249×1=−0.84(-3.36)\times 1\times 0.249\times 1 = -0.84. The backward pass computes the product from the left, and every partial product is reused. For example, 2(y^−y) v1 h1(1−h1)2(\hat y - y)\,v_1\,h_1(1-h_1) serves both w11w_{11} (times x1x_1) and w12w_{12} (times x2x_2).

When a value feeds several places, the contributions add (the multivariable chain rule, P1.C4):

∂L∂x1=∂L∂z1 w11+∂L∂z2 w21\frac{\partial L}{\partial x_1} = \frac{\partial L}{\partial z_1}\,w_{11} + \frac{\partial L}{\partial z_2}\,w_{21}

Code

PyTorch records the operations of the forward pass and backward() sweeps through them in reverse. It gives the same gradients as the widget:

import torch

x, y = torch.tensor([1.0, 2.0]), torch.tensor(1.0)
W = torch.tensor([[0.5, -0.3], [0.8, 0.2]], requires_grad=True)
v = torch.tensor([1.0, -1.5], requires_grad=True)

h = torch.sigmoid(W @ x)    # forward pass
L = (v @ h - y) ** 2
L.backward()                # one backward sweep

print(round(L.item(), 4))   # 2.8149
print(W.grad)  # [[-0.8368, -1.6736], [0.8954, 1.7908]]
print(v.grad)  # [-1.5939, -2.5788]

Try it: backprop against nudging

This cell builds the same network in NumPy. It computes all 6 blames in one backward sweep, then finds each one the slow way by nudging every weight and re-running the forward pass, and counts the extra forward passes. Last, it takes one gradient-descent step.

Try it: one backward sweep against 6 nudges

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

The two methods agree to four decimals. The nudging needed 6 extra forward passes, and with a million weights it would need a million. Try changing y to another target, or one of the weights: the two methods keep agreeing.

Go deeper The blame can shrink on its way back

Every sigmoid multiplies the blame by a slope of at most 0.250.25. In the widget the slopes were 0.2490.249 and 0.1780.178. If a signal passes through ten sigmoid layers, the slopes alone can shrink it by a factor of 0.2510≈10−60.25^{10} \approx 10^{-6}, so the weights near the input get almost no blame and learn very slowly. The weights multiply the blame too, and they can make it larger instead, which then grows without limit. This is the problem of vanishing and exploding gradients (P4.16). Part of the cure is a different choice of activation (P4.4) and of starting weights (P4.12), and another part is residual connections (P4.20).

Go deeper Forward mode: the same idea in the other direction

Instead of starting at the loss and moving back, you can start at one input and carry its effect forward: “if I nudge this input, how much does each later value move?” This is forward mode. One forward-mode sweep gives the effect of one input on all the outputs. To get the effect of all nn weights on one loss, you need nn sweeps, no better in spirit than nudging each weight. Reverse mode gives the effect of all inputs on one output in a single sweep. A training loss is one output and the weights are the inputs, which is why backpropagation is reverse mode. Forward mode is the better choice when there are few inputs and many outputs. P1.C10 gives the pass counts.

Check your understanding

  1. A network has 2,000,0002{,}000{,}000 weights. To measure the effect of each weight on the loss by nudging it once and re-running the forward pass, how many extra forward passes are needed?

  2. A chain computes a=5xa = 5x and then L=a2L = a^2. At x=1x = 1, what is ∂L/∂x\partial L/\partial x?

  3. A value xx is used in two places. The blames arriving at xx from the two uses are 33 and −1-1. What is the blame of xx?

  4. A model has one loss and 10,00010{,}000 weights. Why is a backward sweep from the loss better than sweeping forward from each weight?

  5. What does the backward pass of backpropagation produce?

Exercises

  1. Let a=4xa = 4x, c=a2c = a^2 and L=3cL = 3c. At x=1x = 1, find ∂L/∂x\partial L/\partial x by multiplying the rates from the end. Then check by writing LL as a function of xx.
    Show answer∂L/∂c=3\partial L/\partial c = 3, ∂c/∂a=2a=8\partial c/\partial a = 2a = 8 at a=4a = 4, and ∂a/∂x=4\partial a/\partial x = 4. So 3×8×4=963 \times 8 \times 4 = 96. Check: L=3(4x)2=48x2L = 3(4x)^2 = 48x^2, whose slope is 96x=9696x = 96 at x=1x = 1.
  2. In the widget, set v2v_2 to 00 and run the backward pass. Which first-layer gradients become 00, and does v2v_2 itself still get blame?
    Show answerw21w_{21} and w22w_{22} become 00, because the blame reaching hidden unit 2 is the blame at y^\hat{y} times v2=0v_2 = 0, so nothing is handed back to them. But v2v_2 itself still gets ∂L/∂y^×h2\partial L/\partial \hat{y}\times h_2, which is about −0.81-0.81: changing v2v_2 would change the output.
  3. Run the nudge test on v2v_2 with ε=0.1\varepsilon = 0.1, then 0.0010.001. Why does the agreement with gradient × ε\times\ \varepsilon get better as ε\varepsilon shrinks?
    Show answerAt ε=0.1\varepsilon = 0.1 the real change is −0.2520-0.2520 against −0.2579-0.2579 predicted. At 0.0010.001 both are −0.0026-0.0026. The gradient is the slope of the loss at the current weights, and the loss curve bends away from its tangent line, so a smaller nudge stays closer to the tangent.
  4. A network has 3 million weights. How many extra forward passes does nudging every weight once need, and how many backward sweeps does backpropagation need?
    Show answerNudging needs 3 million extra forward passes. Backpropagation needs one backward sweep (after the forward pass), which typically costs a small multiple of a forward pass.
  5. Let f=a⋅b+af = a\cdot b + a with a=2a = 2 and b=5b = 5. Find ∂f/∂a\partial f/\partial a and ∂f/∂b\partial f/\partial b.
    Show answerThe value aa is used twice. The multiply node sends it blame b=5b = 5, and the add node sends it blame 11. They add to 66. For bb, the multiply node gives a=2a = 2. Check: f=ab+af = ab + a, so ∂f/∂a=b+1=6\partial f/\partial a = b + 1 = 6 and ∂f/∂b=a=2\partial f/\partial b = a = 2.

Next: in P4.8 you will follow every number through a different tiny model, a single neuron, and see the same rules in a complete forward pass, backward pass and weight update. P4.9 turns them into a small autograd engine.

Key takeaways

  • A network has one loss number and many weights. The credit-assignment problem is to find how much each weight is responsible for the loss. The answer for one weight is its gradient ∂L/∂w\partial L/\partial w: how much LL changes per unit nudge of ww. Its sign gives the direction, its size the sensitivity.
  • The slow way is to nudge each weight and re-run the network: one extra forward pass per weight. A million weights means a million extra passes for a single training step.
  • Along a chain of steps, the rates multiply. For a=3xa=3x and L=a2L=a^2 at x=2x=2: dL/dx=3×12=36\mathrm{d}L/\mathrm{d}x=3\times12=36.
  • Backpropagation runs the network forward and stores the values, then sweeps backward from the loss. Each node multiplies the blame arriving from above by its own local rate. Add copies the blame, multiply swaps (each input gets the blame times the other input), a nonlinearity scales it by its slope. If a value is used twice, its blames add.
  • One backward sweep gives every gradient, and typically costs a small multiple of one forward pass, whatever the number of weights. This is reverse-mode automatic differentiation: one output (the loss), many inputs (the weights).
  • Backpropagation only computes gradients. The learning is the update step that uses them (gradient descent, P1.F3; better optimizers, P4.13).
  • A weight's blame depends on its input and on everything between it and the loss, not on how large the weight is.
Found a mistake? Tell us