Deep Learning · Beginner Lesson
Backpropagation: How a Neural Network Learns From One Mistake
A neural network may contain millions of adjustable weights. After it makes a wrong prediction, backpropagation calculates how much each weight contributed to that mistake. An optimizer then uses those calculations to make a small correction. This lesson follows both journeys—forward to the prediction and backward to the weights—using one complete numerical example.
The Learning Problem
Suppose a network predicts 2, but the correct answer is 4. Knowing the final error is not enough. The network must answer three more questions:
Which weights?
Which connections helped produce the wrong answer?
Which direction?
Should each weight become larger or smaller?
How sensitive?
Would a tiny change have a large or small effect on the loss?
Backpropagation answers those questions efficiently. It starts at the final loss and works backward through the calculations. At every step it uses a derivative—a number that describes how sensitive one value is to a small change in another.
First Journey: The Forward Pass
The forward pass is simply the calculation that makes a prediction. Input values enter the network, each layer performs its weighted calculations and activations, and the output layer produces the final answer. The loss function then compares that answer with the target.
input → weighted sums → activations → prediction → loss
Second Journey: The Backward Pass
The backward pass begins only after the loss is known. It asks how the loss would change if each earlier value changed slightly. This sensitivity is called a gradient. The information moves from the loss toward the output layer, hidden layers and earlier weights.
loss → output gradients → hidden gradients → weight gradients
The arrows do not mean that the training examples themselves run backward. Only derivative information moves backward. The actual weight change happens later, when the optimizer applies the learning rate.
Important Terms Before the Calculation
Weight (w)
An adjustable number that controls how strongly an input affects the next calculation.
Bias (b)
An adjustable offset added after the weighted input.
Loss (L)
The numerical penalty for the current prediction.
Derivative
The sensitivity of one value to a tiny change in another value.
Gradient
The set of derivatives for all trainable weights and biases.
Learning rate (η)
A small multiplier that controls the size of an optimizer update.
One Complete Numerical Example
We will train the smallest useful model: one input, one weight and one bias. It predicts with ŷ = wx + b. To make differentiation simple, we use the squared loss L = ½(ŷ − y)². The ½ is a teaching convenience: differentiating the square introduces a factor of 2, and ½×2 becomes 1.
| Symbol | Value | Where it comes from | Can training change it? |
|---|---|---|---|
| Input x | 2 | A manually chosen training-example input | No |
| Target y | 4 | The known answer paired with this example | No |
| Weight w | 0.5 | A manually chosen starting parameter for the demonstration | Yes |
| Bias b | 1 | A manually chosen starting parameter for the demonstration | Yes |
| Learning rate η | 0.1 | A developer-chosen update-size hyperparameter | No; the optimizer uses it |
2D Computation Graph: Forward Activations vs. Backward Chain-Rule Gradients
Forward values compute the prediction and loss; backward partial derivatives compute each parameter’s exact responsibility.
Calculate the prediction
Multiply the input by the current weight, then add the bias. The model predicts 2.
ŷ = wx + b = (0.5 × 2) + 1 = 2
Calculate the loss
The target is 4, so the prediction is too small. The current loss is 2.
L = ½(ŷ − y)² = ½(2 − 4)² = ½ × 4 = 2
Find how loss changes with the prediction
The symbol ∂ means “change in.” This derivative is negative, telling us that increasing the prediction would reduce the loss.
∂L/∂ŷ = ŷ − y = 2 − 4 = −2
Follow the chain back to the weight
Because ŷ = wx + b, the prediction changes by x when w changes, so ∂ŷ/∂w = x = 2. Multiplying the connected derivatives is the chain rule.
∂L/∂w = (∂L/∂ŷ)(∂ŷ/∂w) = (−2)(x) = (−2)(2) = −4
Find the bias gradient
Adding one unit to the bias adds one unit to the prediction, so ∂ŷ/∂b = 1.
∂L/∂b = (∂L/∂ŷ)(∂ŷ/∂b) = (−2)(1) = −2
Let the optimizer update the parameters
Gradient descent subtracts the learning rate multiplied by each gradient. Subtracting a negative number makes both parameters larger.
w_new = 0.5 − 0.1(−4) = 0.9 b_new = 1 − 0.1(−2) = 1.2
Check the next prediction
The loss fell from 2 to 0.5. One update did not finish training, but it moved the model in the correct direction.
ŷ_new = (0.9 × 2) + 1.2 = 3 L_new = ½(3 − 4)² = 0.5
How the Chain Rule Connects the Steps
The loss does not use w directly. It depends on the prediction ŷ, and ŷ depends on w. The chain rule connects those dependencies:
∂L/∂w = ∂L/∂ŷ × ∂ŷ/∂w
Why multiply? Imagine a tiny increase of 0.01 in w. Because ∂ŷ/∂w=2, the prediction changes by approximately 2×0.01=0.02. Because ∂L/∂ŷ=−2, that prediction change alters loss by approximately −2×0.02=−0.04. Combining the two effects gives −2×2×0.01=−0.04, so the sensitivity per one unit of w is −4.
A real network repeats this idea through many connected operations. Backpropagation reuses intermediate results, making it far more efficient than perturbing and recalculating every parameter separately.
Do Not Confuse Backpropagation, Autodiff, Gradient Descent and the Optimizer
These ideas cooperate, but each has a different responsibility.
| Term | What it means | Its job during training |
|---|---|---|
| Computation graph | A record of operations and dependencies | Remembers how the loss was produced |
| Automatic differentiation | A framework mechanism that applies derivative rules to recorded operations | Evaluates derivatives without hand-coding each one |
| Backpropagation | An efficient reverse pass that sends sensitivity through the network | Computes a gradient for each trainable parameter |
| Gradient descent | The basic principle of moving parameters opposite the gradient | Explains the direction used to reduce a local objective |
| Optimizer | The actual update algorithm, such as SGD or Adam | Combines gradients, learning rates and possible state to change parameters |
What the Framework Does for You
PyTorch and TensorFlow record the forward operations and compute the gradients automatically. The mathematics is still the same as the hand calculation.
import torch
x = torch.tensor(2.0)
y = torch.tensor(4.0)
w = torch.tensor(0.5, requires_grad=True)
b = torch.tensor(1.0, requires_grad=True)
y_hat = w * x + b # forward pass
loss = 0.5 * (y_hat - y)**2
loss.backward() # backward pass
print(w.grad.item()) # -4.0
print(b.grad.item()) # -2.0
with torch.no_grad():
w -= 0.1 * w.grad
b -= 0.1 * b.grad
new_y_hat = w * x + b
new_loss = 0.5 * (new_y_hat - y)**2
print(w.item(), b.item()) # about 0.9, 1.2
print(new_y_hat.item()) # about 3.0
print(new_loss.item()) # about 0.5
# PyTorch adds future backward results to .grad, so clear them before
# another independent training step.
w.grad.zero_()
b.grad.zero_()Read the PyTorch Example Line by Line
requires_grad=True
Marks w and b as trainable values whose gradients should be stored.
y_hat = w * x + b
Runs the same forward prediction calculated by hand.
loss = 0.5 * ...
Creates one scalar objective from prediction and target.
loss.backward()
Traverses the recorded operations backward and accumulates gradients in w.grad and b.grad.
with torch.no_grad()
Changes parameters without recording the update itself as part of the next gradient graph.
w -= 0.1 * w.grad
Performs the explicit gradient-descent update using the chosen learning rate 0.1.
new_loss
Runs a second forward calculation and verifies that loss fell from 2 to about 0.5.
grad.zero_()
Clears accumulated gradients before another independent step; optimizers commonly provide zero_grad() for all parameters.
Common Mistakes and How to Diagnose Them
Forgetting to clear gradients
Many frameworks accumulate gradients. Clear them before the next backward pass unless accumulation is intentional.
Updating during validation
Validation measures the model; it must not call backward or change parameters.
Detached computation
Converting a needed tensor to a plain number or detaching it can break the path from loss to weight.
Exploding or vanishing gradients
Repeated large derivatives can explode; repeated small derivatives can vanish. Inspect gradient norms.
Wrong loss-output pairing
A mathematically valid gradient can still train the wrong objective. Verify targets, logits and activation expectations.
Confusing backprop with the update
Backprop calculates gradients. SGD, Adam or another optimizer performs the update.
When Backpropagation Is—and Is Not—the Right Tool
| Situation | Guidance | Why |
|---|---|---|
| Differentiable neural network | Use backpropagation | It efficiently supplies gradients for millions of parameters |
| Sequence model | Use backpropagation through time or an equivalent graph | The repeated operations are unrolled through sequence positions |
| Non-differentiable decision or simulator | Consider a smooth surrogate, policy gradient, search or gradient-free optimization | Ordinary derivatives may not exist or may not reach the parameters |
| Need only inference | Do not run backward | Prediction needs the forward pass only, saving memory and time |
The complete idea in five lines
- A forward pass creates a prediction and a loss.
- The computation graph records which operations produced that loss.
- Backpropagation follows the graph backward and applies the chain rule.
- The result is one gradient for every trainable parameter.
- An optimizer uses those gradients and a learning rate to update the parameters.