Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Neural networks / How neural networks learn

Convolutional neural networks (CNNs): from pixels to a digit

Build a convolutional network for MNIST digits. How do learned filters and pooling turn pixels into a vote?

Builds on More inputs, more neurons.

A sorting office gets thousands of handwritten postcodes every hour. A person reads a sloppy 7 or a 1 with a flag on top without thinking. We want a machine to do it. Last time, the network took one number: the month. Now it gets a picture.

What the machine sees

To a computer, a picture is a grid of pixels, each a number for its brightness. A handwritten digit from the MNIST data set (LeCun, Cortes and Burges, 1998) is 28 by 28: 784 numbers. Move across it.

A digit is 784 numbers

Interactive
28 × 28 pixels a real handwritten digit
the numbers in the green box

Move over the picture (or tap it, or use the arrow keys) to read the numbers under the green box. Zero is black, one is white. The picture holds 28 × 28 = 784 of them.

How do we show it to a network?

A network takes numbers in, and a picture is 784 numbers. So the simplest idea: make every pixel one input, laid out in a line, and give the neuron a weight for each.

A neuron that reads a whole pictureEvery one of the 784 pixels is an input to one neuron, each with its own weight, and the neuron gives a single score.pixel 10pixel 20pixel 30.2…pixel 7840Σthe digit7w₁w₂w₃w₇₈₄
One input per pixel: the pixels are laid out in a line, and the neuron has a weight for each. The simplest idea: let its single answer be the digit itself.

Where the simplest idea breaks

Digits are labels, not amounts. A single-number answer treats them as points on a line, so a picture caught between a 4 and a 9 might get the answer 6.5. Slide it.

What a one-number answer does when it is torn

Interactive

The answer is not a digit. A classifier should score each possible label instead; its error measure should treat every wrong class as wrong, rather than ranking mistakes by numerical distance.

One score per digit

So change the question. Instead of “which number?”, ask ten questions: “how much does this look like a 0?”, “a 1?”, and so on. One neuron per digit, each with weights for every pixel. The biggest score wins, and no digit is nearer to any other.

One neuron per digitThe pixels feed ten neurons, one for each digit, and each gives a score for how much the picture looks like its digit.pixel 10pixel 20…pixel 7840ΣΣΣΣΣdigit 00.3digit 10.1…digit 72.4digit 90.6
One score per digit: ten neurons, each with weights for every pixel. Each says how much the picture looks like its own digit, and the biggest score wins.

More neurons, more features

To read digits well, we want many questions: is there a stroke along the top, a loop, a diagonal? Each neuron can learn a different feature. Let us count what that costs.

Add more neurons, each looking at the whole picture

Interactive
Neurons
Weights to learn

More neurons means more features, but every neuron looks at every pixel, so each one brings 784 weights of its own. The cost explodes: a grid of 26 by 26 neurons needs over half a million weights, for one layer, and this is only a tiny 28 by 28 picture.

Idea one: look locally

A stroke or corner is visible in a few neighbouring pixels. Let each neuron look at a 3 by 3 patch. Hover over the neurons to see each patch.

Each neuron looks at its own small patch

Interactive
Neurons
Weights to learn

Each neuron now needs nine weights instead of 784, but every position still learns its own edge detector.

Idea two: share the weights

An edge is an edge wherever it is. Share the same nine weights across positions and slide them over the picture.

Every neuron shares the same filter

Interactive
Neurons
Weights to learn

One filter instead of 676 sets of weights: from 529,984 weights down to nine, plus a bias.

One filter, slid across the picture

Those shared weights are a filter, a tiny pattern detector. Sliding it across the digit is a convolution; its results form a feature map. A ReLU keeps positive results. Try a filter or enter your own nine numbers.

Slide a filter over a real digit

Interactive
The picture 28 × 28
The feature map 26 × 26

patch under the window
filter (you can edit)
nine products
add them up
sum of nine
+ bias
= cell
after ReLU

Real computation on real pixels. The nine weights are the same at every position: that is what makes it a convolution. The map is drawn in reading order and normalised to its own largest value.

More filters, more features

One filter asks one question. Run several over the same picture and stack their feature maps. Add the eight filters learned by the digit reader one at a time.

Add filters, get a map for each

Interactive
Filters
Weights to learn

Eight filters cost only 8 × 10 = 80 weights. A single neuron of the first design cost 785.

Then shrink

The block now has 5,408 numbers, most saying “nothing here”. Max-pooling shrinks it by keeping the biggest number in each 2 by 2 block.

Shrink each map: max-pooling

Interactive

Stack another round

Repeat with filters on the pooled maps: the next layer can find curves, corners and crossings from the edges below.

Stack another round

Interactive

Flatten, then the vote

Finally, flatten the maps into a list. Ten neurons, one per digit, each score the 400 values.

The whole digit reader

Interactive
Parameters
Multiply-adds
MNIST test accuracy

Each neuron gives a raw score, which can be any size. To turn the ten scores into probabilities that add up to 1, we use softmax (John Bridle, 1990):

pi=esi∑jesjp_i = \frac{e^{s_i}}{\sum_j e^{s_j}}

Softmax raises each score to a power, then divides by the total so the probabilities add to 1. Drag the scores.

From raw scores to probabilities

Interactive
Probabilities add up to
Biggest

The scores here are illustrative, not from a trained network. The arithmetic is real.

Only the gaps between scores matter. Training is the same loop as ever: guess, measure, send the error back, nudge.

Try it

The reader has 5,258 weights and biases and gets about 98.9% of 10,000 unseen digits right. Draw a digit and watch it go through every layer.

Draw a digit and watch the network read it

Interactive
?
draw a digit

Loading the network when this scrolls into view (39 KB)…

Parameters
Multiply-adds
MNIST test accuracy

Try drawing a letter. It still names a digit, often confidently. It has only ever seen digits, so every picture is one of ten.

Where this comes from

LeNet-5 (LeCun et al., 1998) read digits with convolutions and pooling. The data set, MNIST, comes from the same group. The softmax is John Bridle’s (1990).

Next

Convolutions build in two useful assumptions: nearby pixels belong together, and a pattern means the same wherever it appears. The final vote works for words too: a language model scores its vocabulary, then uses softmax to estimate the next word.

Next: the same idea on colour photographs and a thousand classes, in classifying images.


Sources: LeCun, Bottou, Bengio and Haffner, “Gradient-based learning applied to document recognition” (1998); Bridle, “Probabilistic interpretation of feedforward classification network outputs” (1990). The digit reader is a small network trained on MNIST; the sizes are worked out from its layers.

← Browse all subjectsBack to the beginning ↑