A Convolutional Neural Network (CNN) is a neural network architecture specialized for grid-structured data โ most commonly images โ built around a single core operation, convolution, that fundamentally changes how a network connects to its input compared to the fully-connected MLPs covered earlier in this hub.
The High-Level Idea
Instead of connecting every input pixel to every neuron (as an MLP would), a CNN slides small, learnable filters across the image, each one detecting a specific local pattern โ an edge, a texture, a color transition โ at every position. Stacking several such layers builds up a hierarchy: early layers detect simple patterns (edges, corners), middle layers combine these into more complex shapes (textures, object parts), and later layers assemble those into recognizable whole objects.
A Typical CNN Pipeline
Convolution and pooling layers extract increasingly abstract spatial features; flattening and fully connected layers turn those features into a final prediction.
Code โ A Minimal CNN
import torch.nn as nn
model = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1), # convolution
nn.ReLU(),
nn.MaxPool2d(kernel_size=2), # pooling
nn.Conv2d(16, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
nn.Flatten(),
nn.Linear(32 * 8 * 8, 10) # fully connected output layer (assuming input was 32x32)
)
Every Term in the Rest of This Category, Previewed
| Term | Role |
|---|---|
| Convolution | The sliding, weight-sharing pattern-detection operation itself |
| Kernel / Filter | The small learnable weight matrix that gets slid across the input |
| Feature map | The output produced by applying one filter across the whole input |
| Stride, padding | Controls over how the kernel moves and how edges are handled |
| Pooling | Downsampling feature maps to reduce size and add robustness |
Common Mistakes
- Assuming a CNN is just "an MLP for images" with no structural difference โ the entire point of convolution (covered starting next note) is a fundamentally different, far more parameter-efficient way of connecting to spatial input, not simply a renamed MLP.
- Thinking CNNs are exclusively for images โ the same convolution operation applies to any grid-like, spatially-structured data (1-D convolutions for audio/time-series, 3-D convolutions for video or volumetric medical scans).
Interview Relevance
Q: "At a high level, what makes a CNN different from a standard fully-connected network?" A CNN uses convolution โ sliding small, learnable filters across the input, sharing the same weights at every spatial position โ instead of connecting every input value to every neuron. This gives CNNs far fewer parameters for image-sized inputs, an inductive bias toward detecting local patterns, and a degree of translation invariance that plain MLPs lack entirely.
Practice Question
In your own words, why might a network built from convolution operations naturally suit image data better than one built purely from fully-connected layers?