What is CNN?
- Machine Learning course
- Advanced
- Lesson 41/78
- 5 min
CNN is a deep learning model successful in image processing.
What is a CNN?
A convolutional neural network (CNN) captures local patterns (edges, corners, textures) in grid-structured data like images by sliding small filters over the whole image. Because the same filter is used at every position, the parameter count stays small and a pattern is recognised wherever it appears in the image.
# 28x28 grey image, fully connected first layer (128 neurons): 784 * 128 = 100352 weights
# 3x3 convolution, 16 filters: 3 * 3 * 16 = 144 weightsA typical CNN stacks convolution + activation + pooling blocks and uses a fully connected classifier at the end.
The convolution operation
A filter is multiplied element-wise with a small region of the image and summed; the result is a single value in the output map. The filter is shifted by one step and the operation repeats. A filter that finds vertical edges:
img = torch.tensor([[0., 0., 1., 1.],
[0., 0., 1., 1.],
[0., 0., 1., 1.]])
filtre = torch.tensor([[-1., 1.],
[-1., 1.]])
# top-left 2x2 region: (0·-1 + 0·1 + 0·-1 + 0·1) = 0
# middle region: (0·-1 + 1·1 + 0·-1 + 1·1) = 2 -> edgeCNNs learn the values of these filters from the data themselves.
Practice Convolution by hand
Apply filtre to the given image by hand: at every valid position (stride 1, no padding), sum the element-wise product with the 2×2 region and write the result into a 2×3 list. Print the result.
Starter code
import torch
from torch import nn
img = torch.tensor([[0., 0., 1., 1.], [0., 0., 1., 1.], [0., 0., 1., 1.]])
filtre = torch.tensor([[-1., 1.], [-1., 1.]])
# buraya yazSample solution
import torch
from torch import nn
img = torch.tensor([[0., 0., 1., 1.], [0., 0., 1., 1.], [0., 0., 1., 1.]])
filtre = torch.tensor([[-1., 1.], [-1., 1.]])
sonuc = [[(img[i:i + 2, j:j + 2] * filtre).sum().item() for j in range(3)] for i in range(2)]
print(sonuc)Expected output
[[0.0, 2.0, 0.0], [0.0, 2.0, 0.0]]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is a CNN's main advantage over a fully connected network on images?
- Needing no scaling
- Weight sharing: few parameters and position-independent pattern recognition Correct answer
- More parameters
- Needing no labels
Explanation: The same filter is used at all positions.
2 Sum the element-wise product of the region and the filter.
deger = (bolge ____ filtre).sum()
deger = (bolge * filtre).sum()
Explanation: Convolution is an element-wise product and sum over a region.
3 Match the concepts.
| Term | Meaning |
|---|---|
filter |
Small learned weight matrix |
sliding |
Applying at all positions |
weight sharing |
Few parameters |
feature map |
The filter's output |