How does a network with a hidden layer learn? Work out how much each weight is to blame for the error, starting at the output and passing it back, one link at a time.
Use the chain rule to get each weight's gradient, from the output back
Update both weights and watch the loss fall
backprop.py
import math
# Backpropagation: share out the blame for the error, from the output back to every weight.
x, target = 1.0, 1.0
w1, w2 = 0.5, 0.5 # input -> hidden, hidden -> output
lr = 1.0
for step in range(2):
h = round(1 / (1 + math.exp(-w1 * x)), 3) # hidden neuron (sigmoid)
out = round(w2 * h, 3) # output neuron
loss = round((out - target) ** 2, 3)
d_out = round(2 * (out - target), 3) # how the loss changes with out
d_w2 = round(d_out * h, 3) # chain rule, through out = w2 * h
d_h = round(d_out * w2, 3) # blame passed back to the hidden neuron
d_w1 = round(d_h * h * (1 - h) * x, 3) # through the sigmoid, to w1
w1, w2 = round(w1 - lr * d_w1, 3), round(w2 - lr * d_w2, 3)
print("step", step, "loss", loss)
Step by step
This brings in Python's built-in math module, for functions like math.exp and math.sqrt.
One training example: input 1, and the network should output 1.
Two weights: w1 from the input to a hidden neuron, w2 from the hidden neuron to the output.
Learning rate.
step is created with the value 0.
Forward pass, part 1: the hidden neuron applies a sigmoid to w1 × x: h = 0.622.
Part 2: the output is w2 × h = 0.311. Far from the target 1.
Loss: (0.311 − 1)² = 0.475.
Backward pass. Start at the end: how does the loss change if out changes? 2 × (out − target) = −1.378. Negative: a bigger out would lower the loss.
w2's share: out = w2 × h, so a change in w2 changes out by h. Gradient = −1.378 × 0.622 = −0.857.
Pass the blame back to h: out changes by w2 for each change in h. −1.378 × 0.5 = −0.689.
Through the sigmoid, whose slope is h × (1 − h), and on to w1: −0.162.
Update both weights: w1 = 0.662, w2 = 1.357.
It prints step 0 loss 0.475.
step changes from 0 to 1.
h changes from 0.622 to 0.66.
Forward again: out = 0.896, much closer to 1.
Loss 0.011, down from 0.475 in a single step.
d_out changes from -1.378 to -0.208.
d_w2 changes from -0.857 to -0.137.
d_h changes from -0.689 to -0.282.
d_w1 changes from -0.162 to -0.063.
w1 changes from 0.662 to 0.725. w2 changes from 1.357 to 1.494.
It prints step 1 loss 0.011.
The loss fell from 0.475 to 0.011. Real networks run exactly this, forward then backward, over millions of weights and examples. Libraries like PyTorch work out every gradient for you, using this same chain rule.
Common mistakes
Updating a weight before the blame for the earlier weights has been worked out with the old value
Forgetting the activation's own slope, h × (1 − h) for the sigmoid
Best practices
Work backwards one link at a time: each gradient reuses the one after it
Key takeaways
Forward pass: compute the output and the loss.
Backward pass: work out each weight's gradient, from the output back towards the input.
Each step back multiplies by one local slope: the chain rule.
Then every weight takes a gradient descent step.
Check yourself
Why is it called BACKpropagation?
Answer: Gradients are worked out from the output back towards the input. Each earlier gradient reuses the one after it, so you start at the loss and move back.
For out = w2 × h, how does out change when w2 changes by a small amount?
Answer: By h times that amount. The slope of w2 × h with respect to w2 is h.