A convolutional neural network (CNN) classifies image-like data by building spatial structure into its architecture. Rather than engineering features manually, it learns filters that detect local patterns such as lines, color changes, and grids, then combines them into increasingly complex patterns. section 3.0
Images are represented as tensors whose dimensions preserve pixel order, height, width, and— for color—channels such as red, green, and blue. This matters because shuffling neighboring pixels destroys image meaning, while CNNs encode the prior belief that nearby items are related. section 3.1
A convolution slides the same filter across every input location. At each position, it multiplies corresponding input and filter values, sums the products, and stores the result in an output feature map. Stacking convolutional layers lets later filters combine earlier local detections. section 3.2.1 section 3.0
A practical CNN workflow combines these components:
- Represent images with channel-aware tensors and choose an architecture matching their spatial structure. section 3.1
- Define convolutional layers in PyTorch, commonly placing nonlinear activations between them and ending with Flatten and Linear layers for classification. section 3.0 section 3.5.1
- Add max pooling to reduce spatial dimensions and computation, while monitoring whether repeated shrinking removes too much information. section 3.5.1
- Train on image-classification data, then test filter sizes, convolution depth, pooling rounds, and image transforms. section 3.7
Pooling tradeoff
For a 28×28 MNIST image, two rounds of 2×2 pooling reduce each spatial dimension by four, while five rounds would shrink it below one pixel. Pooling can lower memory and training cost, but excessive pooling can erase the visual evidence needed for classification. section 3.5.1
model_cnn_pool = nn.Sequential(
nn.Conv2d(C, filters, 3, padding=3//2),
nn.Tanh(),
nn.Conv2d(filters, filters, 3, padding=3//2),
nn.Tanh(),
nn.Conv2d(filters, filters, 3, padding=3//2),
nn.Tanh(),
nn.MaxPool2d(2),
nn.Conv2d(filters, 2*filters, 3, padding=3//2),
nn.Tanh(),
nn.Conv2d(2*filters, 2*filters, 3, padding=3//2),
nn.Tanh(),
nn.Conv2d(2*filters, 2*filters, 3, padding=3//2),
nn.Tanh(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(2*filters*D//(4**2), classes), #1
)
cnn_results_with_pool = train_simple_network(model_cnn_pool, loss_func,
mnist_train_loader, test_loader=mnist_test_loader,
score_funcs={’Accuracy’: accuracy_score}, device=device, epochs=20)
#1 Why reduce the number of units into the Linear layer by a factor of 4<sup>2</sup>? Because pooling a 2×2 grid down to one value means going from four values to one, and we do this twice.
Inspect a complete PyTorch CNN with repeated convolutions, two max-pooling stages, Flatten, and a size-adjusted Linear classifier. section 3.5.1

















