Q&A
What is a Convolutional Neural Network (CNN)?
Overview
Before CNNs, applying neural networks to images meant flattening a 224×224×3 image into 150,528 input features - with fully connected layers, this explodes in parameters. CNNs instead apply small learned filters (kernels) that slide across the image, detecting local patterns. Early layers detect edges and textures; deeper layers detect shapes and objects.
graph LR
A["Input image<br/>(H×W×C)"] --> B["Conv layer<br/>local filters → feature maps"]
B --> C["Activation (ReLU)"]
C --> D["Pooling layer<br/>downsamples"]
D --> E["... more Conv+Pool blocks ..."]
E --> F["Flatten → Fully Connected"]
F --> G["Output (class scores)"]
style B fill:#1e1b4b,color:#fff
The Convolutional Layer
A convolutional layer applies a set of learnable filters (kernels) across the input. Each filter is a small matrix (e.g. 3×3) that slides over the input computing a dot product at each position:
import torch.nn as nn
nn.Conv2d(
in_channels=3, # RGB input
out_channels=64, # learn 64 different filters
kernel_size=3, # 3×3 filter
stride=1, # move 1 pixel at a time
padding=1 # zero-pad to preserve spatial size
)
Key properties:
- Weight sharing - the same filter is applied at every position, dramatically reducing parameters.
- Local connectivity - each output neuron depends on a small local region (receptive field), not the entire input.
- Translation equivariance - the same filter detects a pattern regardless of where it appears.
Feature Maps and Channels
Each filter produces one feature map - a 2D activation map showing where that filter's pattern was detected. With 64 filters you get 64 feature maps (channels). As the network deepens, channels encode increasingly abstract features:
- Layer 1: edges (horizontal, vertical, diagonal)
- Layer 2: corners, simple textures
- Layer 3–5: complex patterns (eyes, wheels)
- Deep layers: object parts and whole objects
Pooling Layers
Pooling downsamples the spatial dimensions, reducing computation and providing some translation invariance:
- Max pooling - take the maximum value in each window (most common).
- Average pooling - take the mean (used in global average pooling before the classifier).
nn.MaxPool2d(kernel_size=2, stride=2) # halves spatial size
Receptive Field
Each neuron in a deeper layer "sees" a larger region of the original input - its receptive field. Stacking 3×3 convolutions: 2 layers → 5×5 effective receptive field; 3 layers → 7×7. Deeper networks capture larger contexts without needing large kernels.
Classic Architectures
| Architecture | Key innovation |
|---|---|
| LeNet-5 (1998) | First successful CNN (handwritten digits) |
| AlexNet (2012) | Deep CNN, GPU training, won ImageNet (sparked the DL revolution) |
| VGG (2014) | Very deep with only 3×3 convolutions |
| ResNet (2015) | Residual / skip connections → train 100+ layer networks |
| EfficientNet (2019) | Compound scaling (depth × width × resolution) |
| Vision Transformer (ViT) (2020) | Transformer applied to image patches; now competitive with CNNs |
A Simple CNN in PyTorch
import torch.nn as nn
class SimpleCNN(nn.Module):
def __init__(self, num_classes=10):
super().__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 32, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2), # 14×14
nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2), # 7×7
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(64 * 7 * 7, 128), nn.ReLU(),
nn.Linear(128, num_classes)
)
def forward(self, x):
return self.classifier(self.features(x))
Why This Matters in Interviews
CNNs are a foundational computer vision topic. Core points: convolutional layers use weight sharing and local connectivity to efficiently process images; the feature hierarchy (edges → shapes → objects); pooling for downsampling; ResNet's residual connections enabling very deep networks; and the recent shift toward Vision Transformers. Explaining why CNNs are better than fully connected nets for images (parameter efficiency via weight sharing, spatial inductive bias) is the conceptual anchor.
Common Pitfalls
- Forgetting that CNN parameter count is determined by kernel size × channels (not image size) - this is the weight-sharing advantage.
- Applying a CNN to non-grid-structured data where there's no spatial locality.
- Not using batch normalization in deeper CNNs (training instability).
- Ignoring the receptive field when designing architecture depth.
References
- LeCun et al., "Gradient-based learning applied to document recognition" (1998): http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf
- CS231n Convolutional Neural Networks for Visual Recognition: https://cs231n.github.io/