Summary - CNN
- Machine Learning course
- Advanced
- Lesson 46/78
- 6 min
In this group you learned Conv2d, MaxPool2d, and feature maps.
Summary: CNN
In this group:
- Convolution slides small filters; weight sharing gives few parameters and position-independent recognition.
nn.Conv2d: (N, C, H, W); output⌊(H + 2p - k)/s⌋ + 1; parametersout·in·k·k + out.- Pooling:
MaxPool2d,AvgPool2d, globalAdaptiveAvgPool2d(1). - Blocks: Conv → BatchNorm → ReLU → Pool; maps shrink, channels grow, the receptive field widens.
- A linear classifier after
nn.Flattenor global pooling.
Practice CNN with global pooling
Build a model with the body Conv2d(3, 16, 3, padding=1) → ReLU → Conv2d(16, 32, 3, padding=1) → ReLU, followed by AdaptiveAvgPool2d(1), Flatten and Linear(32, 5). Print the output shapes for both a (2, 3, 32, 32) and a (2, 3, 50, 70) input on separate lines.
Starter code
import torch
from torch import nn
# buraya yazSample solution
import torch
from torch import nn
model = nn.Sequential(
nn.Conv2d(3, 16, 3, padding=1), nn.ReLU(),
nn.Conv2d(16, 32, 3, padding=1), nn.ReLU(),
nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(32, 5))
print(model(torch.zeros(2, 3, 32, 32)).shape)
print(model(torch.zeros(2, 3, 50, 70)).shape)Expected output
torch.Size([2, 5]) torch.Size([2, 5])
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the parameter count of a 3×3 convolution with 64 input and 128 output channels?
- 73856 Correct answer
- 73728
- 8192
- 1152
Explanation: 128 · 64 · 3 · 3 = 73728 weights + 128 biases = 73856.
2 Reduce each channel to a single value.
gap = nn.____(1)
gap = nn.AdaptiveAvgPool2d(1)
Explanation: AdaptiveAvgPool2d(1) produces a 1×1 output independent of the input size.
3 Match the formulas.
| Term | Meaning |
|---|---|
output size |
⌊(H + 2p - k)/s⌋ + 1 |
Conv2d parameters |
out·in·k·k + out |
MaxPool2d(2) |
H/2, W/2 |
Flatten |
C·H·W |