Real images at last: 2,500 handwritten digits from MNIST, the dataset that taught the world's first practical neural networks to read postcodes. Each is a 28 × 28 grid of brightness values. A dense network would have to learn every stroke at every position separately. A convolutional network learns four tiny 3 × 3 filters and slides them across the picture, so a stroke found in one corner is recognized in every corner. Every stage is the same three moves as before, with one new twist: a shared weight collects blame from every position it was used at.
Cells are drawn darker for larger values; red means negative. Yellow frames mark what is being computed, green what has been computed, red dashed what is receiving blame. The small yellow square on the digit is the patch used in the worked steps.
Keyboard: → or space for Next, ← for Back. An epoch is 2,000 digits and takes a second or two.
A filter is a neuron that moves. A 3 × 3 filter is nine weights and a bias, exactly like the neurons of example 1. Instead of having its own input, it is applied to every 3 × 3 patch of the image in turn, 676 positions for a 28 × 28 picture, producing a 26 × 26 feature map. The same nine weights are used at every position: this is weight sharing, and it is why a detector for a stroke learned in one place works everywhere.
Pooling makes it tolerant. Max pooling takes each 2 × 2 block of a feature map and keeps only the largest value. The map shrinks from 26 × 26 to 13 × 13 and stops caring whether a stroke was one pixel to the left or right. Four maps of 13 × 13 flatten into 676 numbers for a plain dense layer.
Softmax for ten answers. Ten outputs, one per digit. Softmax turns ten sums into ten probabilities that add up to 1, and cross-entropy scores the probability given to the true digit. The error signal is still prediction minus truth: p minus a 1 in the true slot and 0 elsewhere.
Blame through pooling and back into a shared weight. In pooling only the winning pixel of each block passed anything forward, so only it receives blame. Then a kernel weight was used at 676 positions, so its gradient is the sum over all those positions of the map's blame times the pixel under it. Blame adds up, as in example 4, only now across space instead of across neurons.
What to expect. With 2,000 training digits and four filters this network reaches about 94% on digits it has never seen. The full 60,000 digits and a few more layers reach 99%; the remaining mistakes here are mostly genuinely ambiguous handwriting, which you can inspect in the confusion table.
Draw a digit with the mouse, or load a test digit and shift it around the frame.
Nine weights each. Dark cells like a bright pixel, red cells dislike one. After training, look for edge and stroke detectors: dark on one side, red on the other.
Nothing trained yet.
| true | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|
Test digits: rows are the true digit, columns what the network said.
The first 20 of 2,000 training digits, exactly as written. These are the first digits of the official MNIST training file; the 500 test digits are the first of the official test file. See data/README.md for the source.
map_f[i][j] = Σₐ Σ_b K_f[a][b] · img[i+a][j+b] + kb_f then ReLU
pool_f[i][j] = max of the 2×2 block
flat = the 4 pooled maps in a row (676 numbers)
z_k = Σₙ W[k][n] · flat[n] + b_k p_k = e^{z_k} / Σ e^{z}
L = −ln p_true
δ_k = p_k − [k is the true digit]
∂L/∂W[k][n] = δ_k · flat[n] ∂L/∂b_k = δ_k
δflat[n] = Σ_k δ_k · W[k][n]
δmap: δflat goes to the pixel that won its pooling block, if its ReLU was open
∂L/∂K_f[a][b] = Σᵢ Σⱼ δmap_f[i][j] · img[i+a][j+b] ∂L/∂kb_f = Σᵢ Σⱼ δmap_f[i][j]
every parameter: p ← p − 0.02 · ∂L/∂p