
In this notebook, we walk through the design, training, and testing of convolutional neural networks (CNNs) for image classification. The task is to classify images of handwritten single digits (0-9) from the MNIST data set, a popular benchmark in the machine-learning community. Each image comes with a truth label from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.
We use Keras, the high-level API for Google's TensorFlow library. We start with a base network and then change one thing at a time (the architecture, the optimizer, the regularization, the batch size) to see what each change does to accuracy.
Why convolutions
Convolutional networks differ from traditional feed-forward networks in that they replace the matrix multiplication with a convolution. Each output value looks only at a small patch of the input, and the same small kernel is slid across the whole image. This gives sparse connections and a large reduction in the number of parameters: the weights we train are the kernel elements themselves.
For a 3 by 3 kernel K, each value of the output feature map is a weighted sum over one patch of the input I:
F(i, j) = Σₐ Σᵦ K(a, b) · I(i + a, j + b)
A layer with 32 such kernels learns 32 different small detectors at once, and the next layer combines their outputs into larger shapes.
The data
MNIST contains 60,000 training images, but to keep each run fast we use a subset: 10,000 training images and 5,000 test images, each 28 by 28 pixels in grey-scale, with labels one-hot encoded over 10 classes. Every network below trains for 20 epochs and is scored on the same 5,000 test images after each epoch.
The base network
The base network stacks two convolutions, a max-pooling step, and a small fully connected classifier, with dropout after the pooling layer and before the output.
Dropout in Keras takes the fraction of units to drop, so the two dropout layers remove 25% and 50% of their inputs during training. The loss is categorical cross-entropy, and the final layer is a 10-way softmax:
softmax(zₖ) = eᶻᵏ / Σⱼ eᶻʲ
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
The base run uses the Adadelta optimizer with a batch size of 256. Each epoch takes only a second or two.
Five experiments
Starting from the base network we ran four variations: an extra convolution, the Adam optimizer, a strong L2 penalty on the dense layer, and a smaller batch size. Final results after epoch 20:
| Run | Optimizer | Batch | Test loss | Test accuracy |
|---|---|---|---|---|
| Base network | Adadelta | 256 | 0.0589 | 98.08% |
| Extra Conv2D(64) + pool + dropout | Adadelta | 256 | 0.0449 | 98.64% |
| Base network | Adam | 256 | 0.0529 | 98.44% |
| L2 penalty of 0.85 on the dense layer | Adadelta | 256 | 0.5558 | 93.12% |
| Base network | Adadelta | 64 | 0.0519 | 98.44% |
What each change did
A deeper network. Adding a third convolution, with its own pooling and dropout, gave the best result, 98.64%. A deeper stack can learn more features, which fits the improvement.
Adam. Adam reached 98.44% against the base network's 98.08%. Adam keeps moving averages of the gradient and its square, which acts like momentum and often reaches a lower cost faster than Adadelta.
Strong L2 regularization. An L2 penalty adds a term to the loss that shrinks weights toward zero. At a strength of 0.85 on the 128-unit dense layer the penalty swamps the data term: the first-epoch training loss was 36.47 and test accuracy started at 28.22%. The network underfits and ends at 93.12%, which is not acceptable for this task.
Batch size. Dropping the batch size from 256 to 64 gave 98.44% and took a little longer per epoch. Smaller batches give noisier gradient estimates, which showed up as a much more erratic loss-per-batch curve, and the noise may help a network escape poor local minima.
Each setting was run once, so the differences between the 98.08%, 98.44%, and 98.64% runs correspond to a few dozen of the 5,000 test images. They suggest a direction but should not be read as a firm ranking. The L2 failure is large enough to be real.
A note from 2026
The batch-64 run was written up as using Adam, but the code still uses Adadelta, so it is the base network with a smaller batch. Its 98.44% matching the Adam run is a coincidence. Also, Keras reports training loss with dropout (and, for the L2 run, the penalty) included, so training and test losses are not directly comparable.
Takeaway
A small two-convolution network reaches about 98% on MNIST in under a minute of training, and depth, optimizer, and batch size move the result by well under a point. Regularization strength, by contrast, can wreck it. The full notebook, with every cell and plot, is on GitHub.