A Conv2d layer slides a small set of weights (a "kernel") across the image and produces a feature map. Stacking convolutions + pooling builds increasingly abstract features: edges → textures → parts → objects.
A 224×224 RGB image flattened is 150,528 numbers. A single fully-connected layer with 1000 hidden units would have 150 million parameters — wasteful and prone to overfitting. A convolution re-uses the same handful of weights at every position, so it has thousands of parameters and naturally generalizes across translations.
- Input:
(batch, channels, height, width)— e.g.(64, 1, 16, 16). - After
Conv2d(1, 8, kernel_size=3, padding=1):(64, 8, 16, 16). - After
MaxPool2d(2): spatial dims halve. - Eventually
Flatten()collapses everything but the batch dim for the classifier head.
Some layers (Dropout, BatchNorm) behave differently between training and evaluation. Always toggle modes around training/eval steps. We also wrap eval in torch.no_grad() to skip building the autograd graph — faster and uses less memory.
python 06_cnn/main.py