Fig. 1 in words
One net, four ways to train it · 200 points, two moons · same data as code 05
1 Gradient check
- x → h → p → L, four circles in a row
- forward: keep outputs (arrows left to right)
- backward: multiply slopes (arrows right to left)
- backprop 0.0033105346
- finite diff. 0.0033105346
- all 13 weights, one by one: max rel. error 3.7 × 10⁻¹⁰
- one slope left out of backward(): 0.1443 · caught
2 Loss by step · η
- loss on a log scale, 0.1 to 100, against step (full batch, 200 points), 0 to 2 000
- dashed line: start 0.549
- η 30 · 563 at step 3 (a red dot at the top)
- η 0.01 · ends at 0.293 (gold curve)
- η 1 · ends at 0.048 (green curve)
- η 30 · then a 4-step cycle (red band)
3 Zero init
- 16 identical circles, each marked with a minus sign
- 16 hidden units identical after 2 000 steps
- 1 distinct · acc 0.885
4 Error signal by layer
- log scale, 10⁻⁹ to 10⁻³, against hidden layer (1 = nearest the input), 1 to 12
- ReLU, He · 1 ÷ 12 = 0.61 (green, flat near the top)
- sigmoid · 1 ÷ 12 = 1.0 × 10⁻⁷ (red, a straight rise from layer 1 to layer 12)
01 Backward Pass Chain rule
- Forward: keep each layer’s output. Backward: multiply slopes from the loss to each weight
- It only computes the gradient; gradient descent learns (Goodfellow et al. §6.5)
- Rumelhart, Hinton & Williams (1986) made it famous
Older than that: Linnainmaa 1970, among others (Griewank 2012).
02 Check the Gradient Check
- Compare backprop with a finite difference: (L(w + εv) − L(w − εv)) ÷ 2ε
- Mine: 0.0033105346 both ways; all 13 weights agree to 3.7 × 10⁻¹⁰
- Drop one activation slope from backward(): 0.1443 against 0.0033. Caught
Run the check once on a tiny net before trusting any curve.
03 The Learning Rate Step
- η 0.01: loss 0.293 after 2 000 steps, still creeping. η 1: 0.048, accuracy 0.98
- η 30: loss 563 at step 3, then a 4-step cycle that never gets below the start (0.549)
- On a bowl of curvature λ each step multiplies the error by (1 − ηλ): it grows once η > 2/λ
Too large a step can go uphill (Goodfellow et al. §4.3).
04 Break the Symmetry Init
- All-zero weights: every hidden unit gets the same gradient; 16 units stay one (accuracy 0.885)
- Random weights, variance gain ÷ fan-in: 1 for sigmoid, 2 for ReLU (He et al. 2015)
- Glorot & Bengio (2010): random starts can saturate sigmoid’s top hidden layer
Randomness here is not noise. It lets units specialise.
05 Try It Yourself PYTHON 3.13.15
sprint32_how_networks_train.py
import numpy as np
from sklearn.datasets import make_moons
X, y = make_moons(200, noise=.2, random_state=32)
y, s = y[:, None], lambda z: .5 + .5 * np.tanh(z / 2)
act = {"sig": (s, lambda a: a * (1 - a)),
"relu": (lambda z: z * (z > 0), lambda a: a > 0)}
aug = lambda A: np.c_[A, np.ones(len(A))] # bias
def init(S, seed, gain): # N(0, gain/fan-in)
r = np.random.default_rng(seed)
return [r.normal(0, (gain / m)**.5, (m + 1, n))
for m, n in zip(S, S[1:])]
def step(W, f="sig"): # forward, backward
g, dg = act[f]; A = [X]
for w in W[:-1]: A.append(g(aug(A[-1]) @ w))
z = aug(A[-1]) @ W[-1]
d, G, D = (s(z) - y) / len(X), [], []
for l in range(len(W) - 1, -1, -1): # chain rule
G[:0], D[:0] = [aug(A[l]).T @ d], [d]
if l: d = d @ W[l][:-1].T * dg(A[l])
L = np.mean(np.logaddexp(0, z) - y * z)
return L, G, D, ((z > 0) == y).mean()
def fit(W, lr=1, f="sig", n=2000):
for _ in range(n):
L, G, D, acc = step(W, f)
W = [w - lr * g for w, g in zip(W, G)]
return float(round(L, 3)), float(acc), W
W, V = init([2, 3, 1], 32, 1), init([2, 3, 1], 1, 1)
e = lambda t: step([w + t * v for w, v in zip(W, V)])[0]
a = sum((g * v).sum() for g, v in zip(step(W)[1], V))
n = (e(1e-5) - e(-1e-5)) / 2e-5 # finite difference
print(f"check {a:.10f} {n:.10f}")
R = [fit(init([2, 16, 1], 1, 2), lr, "relu")
for lr in (.01, 1, 30)]
print("loss, acc:", [r[:2] for r in R])
_, acc, W = fit([np.zeros((3, 16)), np.zeros((17, 1))])
print("zero init:", len({*map(tuple, W[0].T.round(9))}),
"distinct of 16, acc", acc)
N = np.linalg.norm
for f, gain in (("sig", 1), ("relu", 2)):
D = step(init([2] + [32] * 12 + [1], 32, gain), f)[2]
print(f, f"layer 1 / 12: {N(D[0]) / N(D[11]):.1e}")
Output, as printed on the image
check 0.0033105346 0.0033105346
loss, acc: [(0.293, 0.89), (0.048, 0.98), (3.432, 0.665)]
zero init: 1 distinct of 16, acc 0.885
sig layer 1 / 12: 1.0e-07
relu layer 1 / 12: 6.1e-01
Run it. sprint32_how_networks_train.py · versions, data and steps
Check: |a − n| ÷ a = 5.8 × 10⁻¹⁰ · 6.67 × 10⁻¹⁰ ÷ 6.62 × 10⁻³ = 1.0 × 10⁻⁷ · 1.72 ÷ 2.84 = 0.61 · numpy 2.5.3 · scikit-learn 1.9.1
06 General Notes Practice
- Sigmoid’s slope is at most 0.25. In 12 sigmoid layers, layer 1 got 1.0 × 10⁻⁷ of layer 12’s error signal: the vanishing gradient Bengio, Simard & Frasconi (1994) analysed in recurrent nets
- Batch norm helps (Ioffe & Szegedy 2015), but Santurkar et al. (2018) trace it to a smoother landscape, not ‘internal covariate shift’
Plot the loss every run: a sawtooth is a diagnosis.
07 Mini Project Hands-on
- Sweep η = 0.1, 3, 10 (here: 0.126, 0.044, 0.328)
- Start the ReLU net from zeros: loss stays at 0.693 = ln 2
- Put tanh in the 12-layer stack (here: 0.12)
- Delete one line of backward(); let the check catch it
Seeds 1–5 at η 1: loss 0.048 to 0.053.
R Trusted Sources
- Rumelhart, Hinton & Williams (1986). Learning Representations by Back-Propagating Errors. Nature 323, 533–536
- Griewank (2012). Who Invented the Reverse Mode of Differentiation? Doc. Math. ISMP, 389–400
- Bengio, Simard & Frasconi (1994). Learning Long-Term Dependencies with Gradient Descent Is Difficult. IEEE TNN 5(2), 157–166
- Glorot & Bengio (2010). Understanding the Difficulty of Training Deep Feedforward Neural Networks. PMLR 9, 249–256
- He et al. (2015). Delving Deep into Rectifiers. ICCV
- Ioffe & Szegedy (2015). Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. PMLR 37, 448–456
- Santurkar et al. (2018). How Does Batch Normalization Help Optimization? NeurIPS 31
- Goodfellow et al. (2016). Deep Learning, ch. 4, 6
