Types of Neural Networks / Convolutional Neural Networks
Convolutional Neural Networks
IntermediateCNNs exploit spatial structure using shared filters, making them the backbone of computer vision.
Core building blocks
Wᵢₙ is input width, K is kernel size, P is symmetric padding per side, S is stride, and D is dilation. For D = 1 this reduces to ⌊(Wᵢₙ − K + 2P)/S⌋ + 1. Apply the same formula separately to height.
- Convolution — a small kernel slides over the input producing feature maps.
- Stride & padding — control output size.
- Pooling — max/average pooling downsamples feature maps.
- Fully connected head — maps features to predictions.
Working of a CNN
A CNN transforms an image through a repeating pipeline of stages. Each stage refines the one before it: raw pixels become edges, edges become textures, textures become parts, and parts become whole objects.
1. Input — the image enters as a matrix of pixel values, for example 224 × 224 × 3 for a colour photo (height, width, colour channels).
2. Convolution — learnable filters slide over the input. Each filter is tuned to one pattern, such as a diagonal edge or a corner, and produces a feature map recording where that pattern occurs. Stride and padding control each map's size.
3. Activation — ReLU keeps only positive filter responses, letting the network model non-linear patterns.
4. Pooling — max pooling keeps the strongest response in each small window. This shrinks the map and makes detection robust to small shifts of the object.
5. Classification — after the final block the maps are flattened into one vector and passed through fully connected layers; a softmax turns the output scores into class probabilities.
ᐅ l indexes layers: a⁽ˡ⁻¹⁾ is the previous layer's activation and a⁽ˡ⁾ the new one. C is the number of input channels and K the kernel size. W⁽ˡ⁾ is the learnable kernel, b⁽ˡ⁾ the bias, S the stride and P the padding. The first line shows one element (i, j) of the feature map; the second is the ReLU that follows. Max pooling then replaces each S×S window of a⁽ˡ⁾ by its maximum.
Convolution: a sliding filter
This single-channel example shows how a small filter turns local pixel patterns into a feature map. The highlighted top-left input window gives the first output value; the same weights are reused at every position.
Input · 4 × 4
Filter · 2 × 2
Feature map · 3 × 3
The next window uses 2, 3, 6, and 7, giving 2 + 7 = 9. A 4 × 4 input with a 2 × 2 filter, stride 1 and no padding produces a 3 × 3 output.
Pooling: keep the strongest response
Max pooling operates independently on each feature map. Here, four non-overlapping windows reduce a 4 × 4 map to 2 × 2, with no learnable weights.
Feature map · 4 × 4
Pooled map · 2 × 2
Average pooling would instead use the mean: the top-left window would produce (1 + 2 + 5 + 6) / 4 = 3.5.
From image to classification
Follow a handwritten digit through a simple CNN. Filters extract local patterns, pooling compresses the maps, and the classification head combines all features into a prediction.
- 01Input image
28 × 28 × 1
Grayscale digit
- 02Convolution + ReLU
28 × 28 × 8
8 filters · 3 × 3 · padding 1
- 03Max pooling
14 × 14 × 8
2 × 2 windows · stride 2
- 04Flatten
1,568 features
14 × 14 × 8 → one vector
- 05Classification head791%1: 4%Others: 5%
10 class probabilities
Linear layer → softmax
- 06Prediction7
Digit 7
Highest probability
Key properties
- Parameter sharing
- Local connectivity
- Translation equivariance
- Hierarchical features: edges → textures → parts → objects
Landmark architectures
| Model | Year | Contribution |
|---|---|---|
| LeNet-5 | 1998 | First practical CNN (digits) |
| AlexNet | 2012 | ReLU, dropout, GPU training |
| VGG | 2014 | Deep stacks of 3×3 convs |
| ResNet | 2015 | Residual / skip connections |
| EfficientNet | 2019 | Compound scaling |
Example
nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, padding=1), nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, kernel_size=3, padding=1), nn.ReLU(),
nn.AdaptiveAvgPool2d(1), nn.Flatten(),
nn.Linear(64, 10)
)Interview questions & answers
Q1. Why are convolutional layers useful for images?
Answer: They use local receptive fields and reuse the same filter weights across spatial positions. This reduces parameter count compared with dense connections and helps detect patterns wherever they occur. Deeper layers combine local responses into larger-scale features.
Q2. How do stride and padding affect convolution output size?
Answer: Stride controls how far the filter moves per step; padding adds a border around the input. With dilation 1, a 32-by-32 input, a 3-by-3 kernel, padding 1 and stride 2 produce a 16-by-16 spatial output. Output channels equal the number of filters.
Q3. Is a CNN automatically translation invariant?
Answer: Not exactly. Shared convolution filters are translation equivariant under suitable boundary conditions: shifting the input shifts the feature map. Striding and boundaries can disrupt this property. Pooling or global aggregation may improve shift robustness, but do not guarantee perfect invariance.