Convolutional neural network
A convolutional network trained for real in your browser on generated digits: every feature map and receptive field, the learned kernels, a confusion matrix — and a pad to draw on.
Updated
- Convolution: kernels sliding over an image
- Feature maps and receptive fields
- ReLU and max pooling
- Softmax, cross-entropy and back-propagation through convolutions
- What the filters learn
Intuition
Why not fully connected?
A 24×24 image has 576 pixels. If the first layer is to output six 22×22 feature maps, a fully connected layer needs 576 × 2904 ≈ 1.67 million weights; 3×3 kernels need only 6 × 9 + 6 = 60. The saving rests on two bits of common sense about images:
- whether a stroke is a stroke can be told from a small patch around it (local connectivity);
- a vertical line in the top left is the same kind of thing as one in the bottom right (weight sharing).
The three small labs below really train the same network in your browser (Adam, learning rate 0.003, 32 images per step). Each small picture is a test image with the network's prediction beneath it; red frames mark mistakes.
With random weights the network gets 6.6% of the 500 test images right — slightly worse than guessing. After 50 steps (before it has even seen the whole training set once) it reaches 45.4%, and after 250 steps 88.0%.
Math
Convolution
The input has channels and kernel , , is . The output value at is the patch of the input with its top-left corner there, multiplied element-wise by the kernel and summed, plus a bias:
Without padding, an input gives an output: 24 → 22.
ReLU and max pooling
Each first passes through , then each 2×2 window shrinks to one number: the largest of the four. Pooling halves the feature map and makes the network less sensitive to small shifts. In backpropagation the gradient returns only to the selected position in the window.
The whole network
| Layer | Output | Parameters |
|---|---|---|
| Input | 1 × 24 × 24 | — |
| Convolution 1 (6 kernels, 3×3) | 6 × 22 × 22 | 60 |
| Pooling 1 | 6 × 11 × 11 | 0 |
| Convolution 2 (12 kernels, 6×3×3) | 12 × 9 × 9 | 660 |
| Pooling 2 | 12 × 4 × 4 = 192 | 0 |
| Fully connected + softmax | 10 | 1930 |
| Total | 2650 |
The fully connected layer holds most of the parameters. Each unit after the second pooling sees only a 10×10 patch of the input (its receptive field): a 3×3 convolution sees 3×3, pooling makes it 4×4, the next convolution 8×8 and the next pooling 10×10. The more layers, the larger the receptive field.
Top left is the first test image, top right the 6 feature maps of convolution 1 (after ReLU), in the middle the 12 of convolution 2, and at the bottom the probabilities of the 10 digits. This image is a 0: with random weights the network calls it a 5, and after 250 steps a 0. Compare the feature maps of the two: after training the channels divide the work more clearly, some lighting up only along one side of the stroke.
Loss and training
The last layer outputs 10 logits, softmax turns them into probabilities, and the loss is the negative log of the correct class's probability (cross-entropy). With random weights the test loss is 2.91. Backpropagation passes the gradient back layer by layer; each kernel is used at every position, so its gradient is the sum of contributions from all of them.
The learning rate and optimizer matter a great deal (test accuracy at 250 / 1000 steps):
| Optimizer | Learning rate | 250 steps | 1000 steps |
|---|---|---|---|
| Adam (default) | 0.003 | 88.0% | 94.4% |
| Adam | 0.03 | 67.4% | 70.0% |
| Adam | 0.3 | 10.0% | — |
| Adam | 0.0001 | 12.6% | 44.4% |
| SGD + momentum 0.9 | 0.003 | 72.6% | 91.6% |
| SGD + momentum 0.9 | 0.03 | 91.8% | 94.8% |
With Adam at 0.3 only 10% is left after 250 steps: the steps are so large that every ReLU unit "dies" and the network outputs the same digit for everything. Trained to 2000 steps, the default reaches 95.4% test accuracy.
How it is computed, with real numbers
Once the lab above has loaded, this panel works through the same formulas with its current numbers.
Experiment
Follow the lesson
Course
Lesson 1: convolution, pooling and training
5 steps · each one: set up and run → look → answer
1. Getting started
Train 50 steps with the defaults. Look at the test accuracy on the right.
2. The learned kernels
Train to 250 steps and open "Kernels".
3. Receptive fields
Open "Feature maps" and hover a unit in the top layer to see the region it marks on the input.
4. Too few training images
Cut the training images to 200 and train 1000 steps.
5. Steps too large
Set the learning rate to 0.3.
Try these settings
Distortion controls how slanted and scrawled the digits are. With no distortion, samples of the same digit are almost identical and 250 steps give 100%; at distortion 1.6, 250 steps give only 65.8% and 1000 steps 79.2%. A small network with only 2 and 4 channels (746 parameters) reaches 86.0% after 1000 steps.
Exercises
Exercises
Exercise 1: 90% within four epochs
With the defaults, 250 steps (exactly 4 epochs) give 88.0% test accuracy. Without changing the network or the data — only the optimizer or learning rate — reach 90% within 4 epochs.
Hint
SGD with momentum, learning rate 0.03: 91.8% at 250 steps.
- Test accuracy ≥ 90.0%
- Epochs ≤ 4
Exercise 2: rescue a diverged run
At learning rate 0.3 the network only ever guesses one digit (10%). Still using Adam, change the learning rate to reach at least 85% test accuracy.
Hint
0.003: 88.0% at 250 steps. 0.03 is still too large (70.0% at 1000 steps), 0.0001 too small (44.4% at 1000 steps).
- Test accuracy ≥ 85.0%
Quiz
Further reading
Frequently asked questions
What is the difference between a convolutional network and a fully connected one?
Each neuron of a fully connected layer connects to every input of the previous layer; each output of a convolution layer connects only to a small patch of the input, and all positions share the same weights. This gives convolutional networks far fewer parameters and makes them a natural fit for images, whose structure is local and position-independent.
What does a pooling layer do?
It shrinks the feature map (here the maximum of each 2×2 window, halving the size), reducing computation, enlarging the receptive fields of later layers, and making the network more robust to small shifts and distortions. Pooling layers have no parameters.
What is a receptive field?
The patch of the input a unit's value depends on. Each convolution or pooling layer stacked on top enlarges it: on this page a unit after the second pooling sees a 10×10 region, and the fully connected layer combines them to see the whole image.
Why use ReLU?
ReLU(z) = max(0, z) is cheap and has a constant gradient of 1 on the positive side, so it does not shrink gradients layer after layer the way sigmoid does in deep networks. The downside is that with too large a learning rate a unit can be pushed into the negative range and never fire again ("dying ReLU") — exactly what happens on this page at learning rate 0.3.
Does this page use MNIST?
No. So that nothing has to be downloaded, the stroke digits on this page are generated on the spot: each digit is made of a few strokes with random jitter, shear, scaling, rotation and translation, varying thickness and ink, and the occasional broken stroke, then drawn onto a 24×24 image. The training set has 2000 images and the test set 500, identical every time for the same seed.
Further reading
- Fukushima, Neocognitron, Biological Cybernetics, 1980: the forerunner of convolution-and-pooling architectures.
- LeCun et al., Backpropagation Applied to Handwritten Zip Code Recognition, Neural Computation, 1989: training a convolutional network with backpropagation.
- LeCun, Bottou, Bengio, Haffner, Gradient-Based Learning Applied to Document Recognition, Proceedings of the IEEE, 1998: LeNet-5.
- Krizhevsky, Sutskever, Hinton, ImageNet Classification with Deep Convolutional Neural Networks, NIPS 2012.
- Goodfellow, Bengio, Courville, Deep Learning, chapter 9: convolutional networks.
- Kingma, Ba, Adam: A Method for Stochastic Optimization, 2014.