Blame

36084c lugonthier 2026-07-02 14:39:19
Add new content and images for Linear Models, Regularization, SVMs, and Decision Trees - Added images for linear regression, logistic regression, and perceptron. - Introduced a new section on Regularization and High-Dimensional Inference with detailed explanations and images. - Added content on Support Vector Machines, including definitions, loss functions, and kernel methods. - Created a new section on Decision Trees and Ensemble Methods, covering CART, bagging, random forests, and boosting. - Included relevant images to illustrate concepts in Decision Trees and Ensemble Methods.
1
# 10. Convolutional networks
2
3
A dense layer treats an image as a flat vector, so it must learn a separate weight for every pixel and forgets that nearby pixels belong together. Convolutional networks replace that dense connectivity with a small filter that slides across the grid, reusing the same weights everywhere. This module introduces the convolution as a structured layer for grid data, then builds up stride, padding, channels, and pooling.
4
5
**Objectives**
6
- Motivate convolution from locality, translation equivariance, and parameter sharing.
7
- Define the 2D convolution (cross-correlation) used in deep learning.
8
- Compute the output size from input size, kernel, padding, and stride.
9
- Extend a filter to multiple input and output channels (feature maps).
10
- Use max and average pooling to downsample and add small translation invariance.
11
- Compare the parameter count of a convolution against an equivalent dense layer.
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
12
- Recognize the landmark architectures, LeNet to ResNet, and the one idea each contributed.
36084c lugonthier 2026-07-02 14:39:19
Add new content and images for Linear Models, Regularization, SVMs, and Decision Trees - Added images for linear regression, logistic regression, and perceptron. - Introduced a new section on Regularization and High-Dimensional Inference with detailed explanations and images. - Added content on Support Vector Machines, including definitions, loss functions, and kernel methods. - Created a new section on Decision Trees and Ensemble Methods, covering CART, bagging, random forests, and boosting. - Included relevant images to illustrate concepts in Decision Trees and Ensemble Methods.
13
14
## 10.1 Why not a dense layer
15
16
Consider a modest $224 \times 224$ RGB image. Flattened it has $224 \times 224 \times 3 \approx 150{,}000$ inputs, so a single dense layer with even $1{,}000$ units carries about $150$ million weights. Three facts about images make almost all of them wasteful.
17
18
- **Locality**: a pixel is explained by its neighbours (an edge, a corner, a texture), not by pixels on the far side of the image.
19
- **Translation equivariance**: an edge is an edge wherever it appears, so the same detector should apply at every position. Shifting the input shifts the response by the same amount.
20
- **Parameter sharing**: because the detector is position independent, one small set of weights can be reused across the whole image instead of learning fresh weights per pixel.
21
22
A convolutional layer bakes all three in. It uses a small filter (the shared weights) applied at every location (locality and equivariance), which is why it needs orders of magnitude fewer parameters than the dense layer above.
23
24
*Remark:* recall the notation from the Introduction. A layer $l$ computes $z^{[l]} = W^{[l]} a^{[l-1]} + b^{[l]}$ and $a^{[l]} = g^{[l]}(z^{[l]})$, with explicit bias $b^{[l]}$. A convolution is just a structured $W^{[l]}$ whose entries are tied together and mostly zero, so the same layer equation still holds.
25
26
## 10.2 The 2D convolution
27
28
### 10.2.1 Cross-correlation
29
30
Let $I$ be a 2D input (one channel of an image) and $K$ a kernel of size $k \times k$. The operation used in deep learning slides $K$ over $I$ and takes, at each position $(i, j)$, the sum of elementwise products between the kernel and the patch it covers:
31
32
$$\boxed{ (I * K)_{i,j} = \sum_{m}\sum_{n} I_{i+m,\, j+n}\, K_{m,n} }$$
33
34
Each output value is one dot product between the kernel and a local window of the input, so a small $3 \times 3$ kernel looks at nine pixels regardless of image size.
35
36
![Convolution sliding a kernel over the input](/en/Deep%20Learning/10%20Convolutional%20networks/a/convolution.svg)
37
38
*A convolution slides a small kernel across the input, and each position produces one cell of the output feature map.*
39
40
*Remark:* this is technically cross-correlation. The mathematical convolution flips the kernel first, but deep learning libraries do not flip and still call it convolution, because the learned kernel simply absorbs the flip. We follow that convention throughout.
41
42
### 10.2.2 The layer output
43
44
A convolutional layer applies this operation, adds the explicit bias $b$, and passes the result through the activation $g$:
45
46
$$\boxed{ a^{[l]}_{i,j} = g\!\left( (a^{[l-1]} * K)_{i,j} + b \right) }$$
47
48
The bias is a single scalar shared across every position of the output, exactly one more instance of parameter sharing.
49
50
## 10.3 Stride, padding, and output size
51
52
Two hyperparameters control how the kernel sweeps the input.
53
54
- **Stride** $s$: the step in pixels between successive kernel positions. A larger stride skips positions and shrinks the output.
55
- **Padding** $p$: a border of $p$ zeros added around the input. It lets the kernel reach the edges and controls the output size.
56
57
For a 1D input of size $n$ (the same formula applies per axis for 2D), the output size is:
58
59
$$\boxed{ o = \left\lfloor \frac{n + 2p - k}{s} \right\rfloor + 1 }$$
60
61
*Remark:* two common choices have names. "Valid" padding uses $p = 0$, so the output shrinks by $k - 1$ at stride $1$. "Same" padding picks $p$ so that $o = n$ at stride $1$, which for an odd kernel means $p = (k - 1)/2$.
62
63
For example, with $n = 32$, $k = 5$, $p = 0$, $s = 1$ the output is $\lfloor (32 - 5)/1 \rfloor + 1 = 28$. Adding $p = 2$ ("same") gives $\lfloor (32 + 4 - 5)/1 \rfloor + 1 = 32$.
64
65
## 10.4 Channels and feature maps
66
67
Real images have channels (three for RGB), and a kernel spans all of them. A filter for an input with $C_\text{in}$ channels has shape $k \times k \times C_\text{in}$, and its convolution sums over spatial positions and channels to produce one 2D output, called a **feature map**.
68
69
To detect many patterns a layer stacks $C_\text{out}$ such filters, so the layer has $C_\text{out}$ feature maps and its output is a volume of shape $o \times o \times C_\text{out}$. Each feature map responds to one learned pattern (an edge orientation, a colour blob, later a texture) at every position.
70
71
$$\boxed{ W^{[l]} \in \mathbb{R}^{\,k \times k \times C_\text{in} \times C_\text{out}}, \qquad b^{[l]} \in \mathbb{R}^{\,C_\text{out}} }$$
72
73
*Remark:* the output channel count $C_\text{out}$ of one layer becomes the input channel count $C_\text{in}$ of the next, so depth grows as spatial size shrinks. There is one bias per output channel, which is why $b^{[l]}$ has $C_\text{out}$ entries.
74
75
## 10.5 Pooling
76
77
Pooling downsamples a feature map by summarising each small window with a single number, using a fixed rule and no learned weights. The two common rules are the maximum and the average over each $k \times k$ window:
78
79
$$\boxed{ \text{max}: \max_{m,n} a_{i+m,\, j+n} \qquad \text{avg}: \frac{1}{k^2}\sum_{m,n} a_{i+m,\, j+n} }$$
80
81
Pooling with stride $s = k$ (non-overlapping windows) shrinks each spatial dimension by a factor of $k$, which cuts computation for later layers. It also grants small **translation invariance**: a max over a window returns the same value if the strong response shifts within that window.
82
83
![Max pooling over 2x2 windows](/en/Deep%20Learning/10%20Convolutional%20networks/a/pooling.svg)
84
85
*Max pooling downsamples each region to its largest value, shrinking the feature map and adding small translation invariance.*
86
87
*Remark:* pooling has no parameters and reduces resolution, which is why modern architectures often replace it with strided convolutions instead. Convolution is equivariant to translation (the response moves with the input), whereas pooling adds a little invariance (the response ignores small moves).
88
89
## 10.6 The parameter payoff
90
91
The point of parameter sharing is size. Take an input of $32 \times 32 \times 3$ and a layer producing a $32 \times 32 \times 16$ output with a $3 \times 3$ kernel ("same" padding). The convolution shares one small filter bank across all positions, while a dense layer connecting every input to every output does not.
92
93
| Layer | Weights | Biases | Total parameters |
94
| --- | --- | --- | --- |
95
| Convolution ($3\times3$, $16$ filters) | $3 \cdot 3 \cdot 3 \cdot 16 = 432$ | $16$ | $448$ |
96
| Equivalent dense layer | $(32\cdot32\cdot3)\cdot(32\cdot32\cdot16) \approx 5.0\times10^{10}$ | $16{,}384$ | $\approx 5.0\times10^{10}$ |
97
98
The convolution uses a few hundred parameters against about fifty billion for the dense layer, and it generalizes better because the same feature detector is reused everywhere rather than relearned per position.
99
100
## 10.7 A convolutional stage
101
102
A typical stage chains convolution, activation, and pooling, turning the raw image into a stack of feature maps that later stages refine.
103
104
![A convolutional stage from image to feature maps](/en/Deep%20Learning/10%20Convolutional%20networks/a/conv-pipeline.svg)
105
106
*A convolutional stage: convolution, activation, then pooling, repeated to build feature maps.*
107
108
*Remark:* stacking such stages makes the receptive field (the input region that influences one output value) grow with depth, so early layers see edges and deep layers see whole objects, all built from the same local operation.
109
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
110
## 10.8 From layers to architectures
111
112
The landmark convolutional networks all share one shape: a stack of convolution and pooling stages that extracts features, then a small fully connected head that classifies them.
113
114
![A deep CNN as feature-map blocks feeding into fully connected layers](/en/Deep%20Learning/10%20Convolutional%20networks/a/cnn-stack.svg)
115
116
*A deep CNN progressively reduces spatial size while increasing channel depth, then flattens into fully connected layers.*
117
118
Each generation contributed one idea to the same question, how to stack more layers without the training signal decaying:
119
120
- **LeNet**, the original, alternates a handful of convolution and pooling stages for digit recognition.
121
- **AlexNet** scaled that skeleton to large images and GPUs, made trainable by ReLU activations and dropout.
122
- **VGG** made every convolution $3 \times 3$ and got its depth by stacking: two $3 \times 3$ layers see the same region as one $5 \times 5$ with fewer parameters ($18c^2$ against $25c^2$) and one more nonlinearity.
123
- **Inception** runs branches of several filter sizes in parallel and concatenates them, kept affordable by $1 \times 1$ convolutions, per-position channel maps that squeeze a thick feature map down before the expensive filters.
124
- **ResNet** lets each block learn a correction around an identity skip connection:
125
126
$$\boxed{\ y = F(x, W) + x, \qquad \frac{\partial y}{\partial x} = \frac{\partial F}{\partial x} + I\ }$$
127
128
The $+I$ gives the gradient a backward route that never shrinks, the direct remedy to the [vanishing gradient](/en/Deep%20Learning/07%20Initialization%20and%20vanishing%20gradients) of lesson 7, and networks of hundreds of layers train reliably.
129
130
![Residual block with an identity skip around the convolution path](/en/Deep%20Learning/10%20Convolutional%20networks/a/residual-block.svg)
131
132
*A residual block adds an identity skip connection around the convolution path, so the layer only has to learn a correction F(x).*
133
134
| Architecture | Approx. depth | Key idea |
135
| --- | --- | --- |
136
| LeNet | 5 to 7 layers | conv and pool stack |
137
| AlexNet | 8 layers | ReLU and dropout at scale |
138
| VGG | 16 to 19 layers | stacks of $3 \times 3$ convolutions |
139
| Inception | 22 layers | parallel branches, $1 \times 1$ bottleneck |
140
| ResNet | 50 to 152 layers | residual skip connections |
141
142
*Remark:* the trend is monotonic in depth, and each jump was unlocked by a specific fix: better activations, smaller filters, channel bottlenecks, and finally skip connections.
143
144
*These deep stacks learn feature maps whose deeper activations behave as reusable representations, the entry point of the next module on embeddings and representation learning.*
36084c lugonthier 2026-07-02 14:39:19
Add new content and images for Linear Models, Regularization, SVMs, and Decision Trees - Added images for linear regression, logistic regression, and perceptron. - Introduced a new section on Regularization and High-Dimensional Inference with detailed explanations and images. - Added content on Support Vector Machines, including definitions, loss functions, and kernel methods. - Created a new section on Decision Trees and Ensemble Methods, covering CART, bagging, random forests, and boosting. - Included relevant images to illustrate concepts in Decision Trees and Ensemble Methods.
145
146
---
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
147
Next: [Embeddings and representation learning](/en/Deep%20Learning/11%20Embeddings%20and%20representation%20learning) · [Course overview](/en/Deep%20Learning)