The mathematical essence of backpropagation
The mathematical essence of backpropagation
This chapter will manually calculate the entire backpropagation process for a two-layer neural network.
Backpropagation = systematic application of the chain rule on a computational graph.
Concept analysis
Computational graph: forward and backward
A simple two-layer network: input x → hidden layer (ReLU) → output → MSE loss.
x
z₁=w₁x+b₁
a₁=ReLU(z₁)
z₂=w₂a₁+b₂
ŷ
L=(ŷ-y)²
Forward propagation computes the output from left to right. Backpropagation computes the gradient of each parameter with respect to the loss from right to left.
Calculate a complete example manually
Let x=2, y=4 (true label), and parameters w₁=0.5, b₁=0, w₂=1, b₂=0.
Forward propagation
| Steps | Computation | Value |
|---|---|---|
| z₁ = w₁x + b₁ | 0.5×2 + 0 | 1.0 |
| a₁ = ReLU(z₁) | max(0, 1.0) | 1.0 |
| ŷ = w₂a₁ + b₂ | 1×1.0 + 0 | 1.0 |
| L = (ŷ - y)² | (1-4)² | 9.0 |
Backpropagation (Chain Rule)
Gradient of Loss with respect to ŷ:( \frac{\partial L}{\partial \hat{y}} = 2(\hat{y}-y) = 2(1-4) = -6 )
Gradient of ŷ with respect to w₂ → L with respect to w₂:( \frac{\partial \hat{y}}{\partial w_2} = a_1 = 1.0 ),( \frac{\partial L}{\partial w_2} = (-6) \times 1.0 = -6 )
Gradient of L with respect to a₁ (continue forward propagation):( \frac{\partial L}{\partial a_1} = \frac{\partial L}{\partial \hat{y}} \cdot w_2 = -6 \times 1 = -6 )
After ReLU:( \frac{\partial a_1}{\partial z_1} = 1 ) (since z₁=1>0), ( \frac{\partial L}{\partial z_1} = -6 )
L with respect to w₁:( \frac{\partial L}{\partial w_1} = \frac{\partial L}{\partial z_1} \cdot x = -6 \times 2 = -12 )
Aggregate gradients and update
| Parameters | Gradients | Update (lr=0.1) |
|---|---|---|
| w₁ | -12 | 0.5 + 1.2 = 1.7 |
| b₁ | -6 | 0 + 0.6 = 0.6 |
| w₂ | -6 | 1 + 0.6 = 1.6 |
| b₂ | -6 | 0 + 0.6 = 0.6 |
All gradients are negative—indicating the current prediction is too small (1 < 4), so all parameters need to be increased.
The gradient of w₁ (-12) is larger than w₂ (-6): because w₁ is further from the loss, the chain rule path is longer, and it is multiplied by x=2.
Python verification
Instance
import
torch
x
=
torch.
tensor
(
[
2.0
]
)
y
=
torch.
tensor
(
[
4.0
]
)
w1
=
torch.
tensor
(
[
0.5
]
,
requires_grad
=
True
)
b1
=
torch.
tensor
(
[
0.0
]
,
requires_grad
=
True
)
w2
=
torch.
tensor
(
[
1.0
]
,
requires_grad
=
True
)
b2
=
torch.
tensor
(
[
0.0
]
,
requires_grad
=
True
)
Forward
a1
=
torch.
relu
(
w1 * x + b1
)
y_pred
=
w2 * a1 + b2
loss
=
(
y_pred - y
)
**
2
loss.
backward
(
)
(
"=== RUNOOB PyTorch Verification ==="
)
(
f
"∂L/∂w1 = {w1.grad.item():.1f} (manual calculation: -12)"
)
(
f
"∂L/∂b1 = {b1.grad.item():.1f} (Manual calculation: -6)"
)
(
f
"∂L/∂w2 = {w2.grad.item():.1f} (Manual calculation: -6)"
)
(
f
"∂L/∂b2 = {b2.grad.item():.1f} (manual calculation: -6)"
)
One step of gradient descent
lr
=
0.1
with
torch.
no_grad
(
)
:
w1 -
=
lr * w1.
grad
;
b1 -
=
lr * b1.
grad
w2 -
=
lr * w2.
grad
;
b2 -
=
lr * b2.
grad
new_loss
=
(
(
w2 * torch.
relu
(
w1*x+b1
)
- b2 - y
)
**
2
)
.
item
(
)
(
f
"\nOriginal loss: {loss.item():.1f} → New loss: {new_loss:.2f}"
)
(
f
"Loss has decreased!"
)
Original prompt (Chinese, left untranslated)
=== RUNOOB PyTorch 验证 ===
∂L/∂w1 = -12.0 (手算: -12)
∂L/∂b1 = -6.0 (手算: -6)
∂L/∂w2 = -6.0 (手算: -6)
∂L/∂b2 = -6.0 (手算: -6)
原损失: 9.0 → 新损失: 1.03
损失减小了!
Backpropagation = Three lines converging
** Linear Algebra
Forward propagation = Matrix operation
z = Wx + b
Calculus
Backpropagation = Chain Rule
∂L/∂w = ∂L/∂z · ∂z/∂w
Optimization
Parameter Update = Gradient Descent
θ = θ - η∇J
Probability & Information Theory
Loss design = MLE + Cross-entropy
L = -log P(y|x)
Backpropagation essentially does three things: forward pass uses matrix operations to calculate the output, backward pass uses the chain rule to calculate the gradient, and after obtaining the gradient, it updates the parameters using gradient descent.
Probability statistics and information theory provide the theoretical basis for choosing the loss function.