# Why Training Needs Backpropagation

Suppose you have a machine with 41 knobs and a score that tells you how badly it's doing. You want to know which way to turn each knob to improve the score. How would you find out?

The obvious approach is to turn one knob a tiny bit, check the score, turn it back, and move to the next knob. It's simple, and it works.

It's also not how neural networks are trained. This article explains why, and in doing so, it explains what backpropagation is actually for.

My running example is the network from my own repo: a small network with 41 trainable parameters, which are its knobs.

## What training needs

Training repeats a loop: make predictions, score them, and adjust the parameters to score better. Two terms:

- **Loss:** a single number measuring how wrong the network is. Zero means perfect.
- **Gradient:** for each parameter, one number saying how much the loss changes if you increase that parameter slightly. A gradient of −0.4 means that nudging the parameter up by a small amount lowers the loss by about 0.4 times that amount, so you should move it up.

To train, we need the **whole gradient** at every step: one number per parameter, so 41 numbers for my network.

## Method A: nudge and measure

The knob-turning idea has a name: numerical differentiation. For a parameter θⱼ, nudge it up by a tiny amount h, nudge it down by h, and compare the losses:

gradient ≈ (L(θⱼ + h) − L(θⱼ − h)) / 2h

Each of those two measurements needs a **forward pass**: running the network on the data to compute the loss. So here is the cost:

- 2 forward passes per parameter
- 41 parameters, so **82 forward passes** for one gradient
- and we need a fresh gradient at every training step

To make that concrete, count multiplications in my network. It's a 3 → 4 → 4 → 1 network with 12 + 16 + 4 = 32 weights, so one forward pass on one example costs 32 multiplications. (I'm ignoring the additions and the tanh, which scale the same way.) Eighty-two passes come to 82 × 32 = **2,624 multiplications** for one gradient.

That's survivable for 41 parameters. Now scale it.

GPT-2 small has 124 million parameters. Nudging would need about **248 million forward passes** for a single training step, and training takes hundreds of thousands of steps. The cost grows in direct proportion to the parameter count, and that rules the method out.

There's a second problem: choosing h. If it's too large, the answer is crude. If it's too small, the computer's rounding errors take over. You have to tune it, and you still only get an approximation.

## Method B: backpropagation

Backpropagation gets the entire gradient from **one forward pass and one backward pass**. The backward pass costs about twice as much as the forward pass, so the full gradient costs about three forward passes, no matter how many parameters there are.

My network shows this exactly. The forward pass has 32 multiplications. In the backward pass, each multiplication needs two:

- one to get the gradient for the weight (the output's gradient times the input)
- one to pass the gradient back to the input (the output's gradient times the weight)

That's 64 more, so 96 in total. Nudging needed 2,624, so for my tiny network backpropagation is about **27 times cheaper**.

The ratio grows with the parameter count. Nudging costs 2P forward passes and backpropagation costs about 3, so the ratio is roughly 2P/3. For P = 41 that's the 27 above. For GPT-2 small it's about **83 million times** cheaper.

|  | Nudging | Backpropagation |
| --- | --- | --- |
| Work for the full gradient | 2P forward passes | about 3 forward passes (1 forward + 1 backward) |
| My network (P = 41) | 82 forward passes | about 3 |
| GPT-2 small (P ≈ 124 million) | about 248 million | about 3 |
| Accuracy | approximate, depends on h | exact up to rounding |

That's the reason backpropagation exists. How can it get so much for so little?

## The key idea: share the work

Let's explore it with a tiny example: one neuron, one training example.

- input x = 2, weight w = 0.5, bias b = 0, target y = 1

**Forward pass**

- z = x·w + b = 1.0
- o = tanh(z) = 0.7616
- L = (o − y)² = 0.0568

**Backward pass**, working from the loss back to the parameters with the chain rule (if A affects B and B affects the loss, multiply the effects):

1. dL/do = 2(o − y) = **−0.4768**
2. dL/dz = dL/do × (1 − o²) = −0.4768 × 0.4200 = **−0.2002**
3. dL/dw = dL/dz × x = −0.2002 × 2 = **−0.4005**
4. dL/db = dL/dz × 1 = **−0.2002**

![](https://cdn.hashnode.com/uploads/covers/63e91421861f30240f6e03ff/0f966a35-4863-4f23-9cd6-ce49888783af.png align="center")

As a check, nudging w by 0.00001 in each direction gives −0.4005, the same number.

Now look at step 2. The value dL/dz = −0.2002 was computed **once**, and then it served both w and b. If I had nudged, each parameter would have needed its own full re-run to learn what this one number already told me.

To make it simple: every node in the calculation answers one question, "how much does the loss care about me?" That answer is computed once, from the answers of the nodes after it, and every parameter feeding into that node reuses it.

That is the whole trick. In a real layer with four neurons, each neuron's single number is shared by every weight feeding it. The backward pass visits each connection once, so its cost matches the size of the network and not the size of the network multiplied by its parameter count.

## Why not list the paths?

You might wonder why we don't compute each gradient straight from the chain rule. The chain rule says a parameter's effect on the loss is the sum, over every path from that parameter to the loss, of the product of the local effects along the path.

In my network, a single input reaches the output through 4 × 4 × 1 = 16 paths, which is fine. But take a network 1,000 neurons wide and 10 layers deep, and a single input has on the order of 1,000⁹ = 10²⁷ paths to the output. Nobody can list those.

Backpropagation never lists them. At each node it adds up the contributions that have already arrived and passes the total back, so paths that share a stretch are combined early. It's the same idea as dynamic programming: store a partial answer instead of recomputing it.

## What this looks like in code

My engine does this in a handful of lines. Each operation stores a small function that knows its own local derivative. This is the multiplication from my notebook:

```python
def _backward():
    self.grad += other.data * out.grad
    other.grad += self.data * out.grad
```

And this is the `backward` method that runs the whole graph:

```python
build_topo(self)
topo.reverse()
self.grad = 1.0
for node in topo:
    node._backward()
```

Two details matter:

- `+=` **instead of** `=`**:** if a value is used in several places, its total gradient is the *sum* of the gradients coming back from each. That's the chain rule's sum over paths, applied one node at a time.
- `topo.reverse()`**:** this walks the graph from the loss back to the inputs, so a node passes its gradient on only after every node that depends on it has contributed its share. Otherwise it would pass on a partial total.

## Why backwards and not forwards?

There's a second way to compute exact derivatives, called **forward mode**, which starts at the inputs and pushes derivatives forward. It's exact, and it avoids the problem of choosing h. But one pass gives you the effect of *one* input on *all* outputs.

Reverse mode (backpropagation) is the mirror image: one pass gives you the effect of *all* inputs on *one* output.

So forward mode costs one pass per input, and reverse mode costs one pass per output. Now look at training:

- inputs: the parameters, 41 here and billions in a large model
- outputs: **one**, the loss

With a huge number of inputs and a single output, reverse mode wins by a wide margin. That's why backpropagation is reverse-mode automatic differentiation applied to a network's loss. In the opposite situation, a few inputs and many outputs, forward mode would be the better choice.

## What it costs: memory

Backpropagation isn't free. The backward pass needs numbers from the forward pass: computing 1 − o² needs the stored o, and a multiplication's backward step needs both of its inputs. So the whole computation has to be kept alive until the backward pass is done. Backpropagation saves all the intermediate activations so it doesn't have to recompute them, which trades memory for compute.

In my engine, every `Value` holds its data and a function remembering how it was made. For a large model those stored intermediate values (called activations) take up a lot of memory, and the amount grows with the batch size.

One standard answer is **gradient checkpointing**: store only some of the intermediate values and recompute the rest during the backward pass. It spends extra computation to save memory. Backpropagation's speed has a price, and engineers manage it with tradeoffs like this one.

## Where nudging still belongs

Nudging isn't useless. It's the right tool for a different job: **checking** your backpropagation. A network as small as mine can have every gradient compared against nudged values. If they match, your backward pass is very likely right. That's the one job left for the slow method, and it's how people debug custom gradient code.

## Takeaways

1. A gradient needs one number per parameter at every step, so the cost of getting it dominates training.
2. Nudging costs about 2P forward passes, which is hopeless at scale, and it's only approximate.
3. Backpropagation costs about three forward passes whatever P is, because each node's "how much does the loss care about me?" is computed once and shared.
4. It works backwards because training has many inputs and a single output, the loss.
5. The price is memory, since the forward pass has to be remembered.

PyTorch's `loss.backward()` does exactly this with tensors instead of single numbers. My engine handles one number at a time, so next I'm moving to tensors, where one operation covers a whole array.

The code for the engine and the network is on GitHub: [neural-learn](https://github.com/SeunFashina002/neural-learn), inspired by Andrej Kaparthy Micrograd.
