Problem
Most neural network explanations either stay abstract or jump straight to huge models. This article does the opposite. We take two tiny networks and compute every single number by hand, so you can see exactly what "learning" means.
- Example 1: one neuron learns the rule
y = 2x. - Example 2: a small network does one full forward and backward pass.
Theory
A neural network is a function with adjustable numbers (weights). Training is a loop of four steps:
- Forward: put an input in, get a prediction out.
- Loss: measure how wrong the prediction is.
- Backward: work out how much each weight contributed to the error (the gradient).
- Update: nudge each weight in the direction that reduces the error.
The update rule is one line:
lr (learning rate) controls the step size. The gradient tells us which direction reduces the loss, and why that direction is correct: the gradient is the slope of the loss, so moving against the slope goes downhill.
Architecture
Example 1: one weight, no bias, no activation.
x ──(× w)──> ŷ loss = (ŷ − y)²Example 2: 2 inputs → 2 hidden neurons → 1 output, with sigmoid everywhere.
x1 ─┬─> h1 ─┐
│ ├─> ŷ
x2 ─┴─> h2 ─┘Sigmoid squashes any number into 0–1: , and its slope is .
Implementation
Example 1: learning y = 2x
Data: x = 1, y = 2. Start with w = 0, lr = 0.1.
The gradient is .
| Step | w | ŷ = w·x | error (ŷ − y) | gradient | new w |
|---|---|---|---|---|---|
| 1 | 0.000 | 0.000 | −2.000 | −4.000 | 0.400 |
| 2 | 0.400 | 0.400 | −1.600 | −3.200 | 0.720 |
| 3 | 0.720 | 0.720 | −1.280 | −2.560 | 0.976 |
| 4 | 0.976 | 0.976 | −1.024 | −2.048 | 1.181 |
Each step cuts the error by 20% (it gets multiplied by 1 − 2·lr·x² = 0.8). That's why w creeps toward 2 and never overshoots. A bigger lr would cut faster, but at lr = 1.0 the factor becomes −1 and the weight would bounce back and forth forever.
Example 2: one full pass through a 2-2-1 network
Input x = (1, 0), target y = 1. Starting weights (biases are 0 to keep it readable):
- Hidden:
w11 = 0.5, w12 = −0.3(for h1),w21 = 0.8, w22 = 0.2(for h2) - Output:
v1 = 0.6, v2 = −0.4
Forward
z1 = 0.5·1 + (−0.3)·0 = 0.5→h1 = σ(0.5) = 0.6225z2 = 0.8·1 + 0.2·0 = 0.8→h2 = σ(0.8) = 0.6900z = 0.6·0.6225 + (−0.4)·0.6900 = 0.0975→ŷ = σ(0.0975) = 0.5244- Loss = ½(ŷ − y)² = ½(−0.4756)² = 0.1131
Backward (chain rule, going from the output back to the inputs)
- Output delta:
(ŷ − y) · ŷ(1−ŷ) = −0.4756 · 0.2494 = −0.1186 - Output weight gradients:
v1: −0.1186 · 0.6225 = −0.0738,v2: −0.1186 · 0.6900 = −0.0818 - Hidden deltas (send the output delta back through
v, then through the sigmoid slope):h1: −0.1186 · 0.6 · 0.2350 = −0.0167h2: −0.1186 · (−0.4) · 0.2139 = +0.0101
- Hidden weight gradients = hidden delta × input. Since
x2 = 0, the weightsw12andw22get gradient 0. An input of zero can't have caused any of the error.
Update (lr = 0.5)
v1: 0.6 → 0.637,v2: −0.4 → −0.359w11: 0.5 → 0.508,w21: 0.8 → 0.795
Forward again with the new weights: ŷ = 0.5375, loss = 0.1069.
The loss dropped from 0.1131 to 0.1069 after a single update, and the prediction moved toward the target of 1.
Experiments
We ran both examples as code and compared them with the hand calculations. Example 1 was trained for 20 steps. Example 2 was trained for one step by hand, then for 2000 steps in code.
import numpy as np
# Example 1
w, lr = 0.0, 0.1
for step in range(20):
pred = w * 1
grad = 2 * (pred - 2) * 1
w -= lr * grad
print(round(w, 4))
# Example 2
sig = lambda z: 1 / (1 + np.exp(-z))
x, y = np.array([1., 0.]), 1.
W = np.array([[0.5, -0.3], [0.8, 0.2]])
v = np.array([0.6, -0.4])
for step in range(2000):
h = sig(W @ x)
yhat = sig(v @ h)
d_out = (yhat - y) * yhat * (1 - yhat)
d_hid = d_out * v * h * (1 - h)
v -= 0.5 * d_out * h
W -= 0.5 * np.outer(d_hid, x)
print(round(yhat, 4))Results
- Example 1:
wgoes 0 → 0.4 → 0.72 → 0.976 → 1.181 …, and reaches about 1.99 after 20 steps. - Example 2: the hand calculation matches the code. After one update, loss goes 0.1131 → 0.1069.
- Example 2, 2000 steps:
ŷgets very close to 1.
Lessons Learned
- Learning is only "measure the error, nudge the weights." There's no magic step beyond that.
- Gradients tell you who to blame. A weight attached to a zero input gets zero gradient.
- Backprop is the chain rule applied layer by layer, reusing each layer's delta for the layer before it.
- Learning rate matters. Too small is slow, too large overshoots. Try changing
lrin the code and watch what happens. - Next step: add more training examples, which lets the same network learn XOR.
Results & Reproducibility
- Python 3, NumPy only. No GPU needed; runs in well under a second.
- Check: Example 1 should print about 1.99. Example 2's first-step loss should be 0.1131, then 0.1069.
- All numbers in the article were computed by hand and cross-checked with the code.
Tags: neural networks, backpropagation, gradient descent, fundamentals, from scratch


