Inside a CNN
Draw a digit and follow it through filters, feature maps and pooling to a guess.
Draw a digit and follow it through filters, feature maps and pooling to a guess.
A real convolutional network (a CNN) reads your handwriting. It learnt from MNIST’s 60,000 handwritten digits. Instead of giving every pixel its own weight, it slides small 3 × 3 filters over the picture and makes feature maps: pictures of where each filter found its pattern. It learnt only 1,626 numbers, and you can see every map it makes.
Draw a digit on the pad, or pick one from MNIST’s test set, and follow it through two layers of filters and pooling to a guess. The question: how do small filters, slid across a picture, add up to recognising a digit?
It reads 7>99.9% sure
Digits from MNIST (Yann LeCun, Corinna Cortes and Christopher Burges), CC BY-SA 3.0, shrunk to 16 × 16.
Layer 1: 4 filters, each a 3 × 3 kernel, + ReLU
its kernel
kernel × the 3 × 3 patch of the input under the orange box
sum = 3.95 + bias 0.00 = z = 3.95 → ReLU → 3.95
Max pooling, 2 × 2: keep the largest of each window
Layer 2: 8 filters, each a 3 × 3 × 4 kernel (a 3 × 3 slice per map), + ReLU
Max pooling, 2 × 2
Flatten: 8 × 4 × 4 = 128 numbers. Dense layer: a score per digit
Filters. A layer-1 filter’s kernel is a 3 × 3 grid of learnt weights. At each position it multiplies the 9 numbers under it by its 9 weights, adds them up with a bias, and ReLU turns a negative total into 0. Doing that at every position makes a feature map. The Image convolution simulator slides hand-made kernels over a picture the same way.
Channels. Layer 2’s filters read all 4 of layer 1’s pooled maps at once, so each kernel is as deep as its input: 3 × 3 × 4 = 36 weights, one 3 × 3 slice per map. Each slice multiplies its map’s patch, and the 4 sums are added together.
Pooling. Max pooling keeps the largest number in each 2 × 2 window, halving each side, and has nothing to learn. A pooled number stays the same as long as its window’s largest number stays in the window.
The guess. The last maps, 128 numbers in all, feed an ordinary dense layer: each digit’s score is a weighted sum of all 128, and softmax turns the ten scores into probabilities. Training on MNIST’s 60,000 training digits set every weight; the test digits here were not used.
Filters. A layer-1 filter’s kernel is a 3 × 3 grid of learnt weights. At each position it multiplies the 9 numbers under it by its 9 weights, adds them up with a bias, and ReLU turns a negative total into 0. Doing that at every position makes a feature map. The Image convolution simulator slides hand-made kernels over a picture the same way.
Channels. Layer 2’s filters read all 4 of layer 1’s pooled maps at once, so each kernel is as deep as its input: 3 × 3 × 4 = 36 weights, one 3 × 3 slice per map. Each slice multiplies its map’s patch, and the 4 sums are added together.
Pooling. Max pooling keeps the largest number in each 2 × 2 window, halving each side, and has nothing to learn. A pooled number stays the same as long as its window’s largest number stays in the window.
The guess. The last maps, 128 numbers in all, feed an ordinary dense layer: each digit’s score is a weighted sum of all 128, and softmax turns the ten scores into probabilities. Training on MNIST’s 60,000 training digits set every weight; the test digits here were not used.
Things to try