Search

Search papers and lessons

Convolutional Neural Network

Level 3 · 15 min read

A convolutional network reuses one small set of weights at every position in an image, so it finds the same local patterns anywhere and needs far fewer parameters than a fully connected network.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Neural network basics
  • Probability
  • Architectures
  • Losses & regularization
  • Sign in to see your progress

TL;DR

A convolutional neural network processes data that has a grid structure, such as images, by sliding the same small filter across every position and feeding the results into further layers. Because the filter's weights are shared across positions, the network detects a pattern wherever it appears and needs far fewer parameters than a network whose every input connects to every neuron. Stacked layers build from edges to textures to object parts.

Where you'll see it: in image classification, object detection and segmentation, medical scans, and audio models that treat a spectrogram as an image.

Intuition

A fully connected network does not know about neighbours

Suppose you feed a 28×2828 \times 28 grey image into a Feedforward Neural Network by flattening it into 784 numbers. Every neuron in the first layer is connected to all 784 pixels, with its own weight for each. The network has no built-in idea that nearby pixels are related. A vertical edge in the top-left corner and the same edge in the bottom-right corner are two unrelated sets of weights that must each be learned from examples. Move the object a few pixels and the network sees a different input.

A convolutional network fixes both problems with two ideas. The first is local connectivity: each neuron looks only at a small patch, say 3×33 \times 3 pixels. The second is weight sharing: the same 3×33 \times 3 weights are used for every patch. One filter that detects a vertical edge is therefore applied across the whole image. It finds the edge wherever it is. This is what people mean by translation equivariance: shift the input, and the output shifts by the same amount.

One filter, many positions

Think of a small window passed over a photograph. At each position it asks "does the patch here look like a vertical edge?" and writes down a score. The result is a new grid of scores, one per position, called a feature map. A layer usually has many filters, each producing its own feature map, and each filter learns to look for a different pattern.

flowchart LR
  A[Input image] --> B[Convolution + ReLU]
  B --> C[Max pooling]
  C --> D[Convolution + ReLU]
  D --> E[Max pooling]
  E --> F[Flatten]
  F --> G[Dense layer]
  G --> H[Softmax over classes]

Early layers respond to edges and colour blobs. Deeper layers combine these into textures, then parts such as eyes or wheels, then whole objects. Pooling between layers shrinks the grid, so later filters cover a larger part of the original image. That growth in coverage is what lets a deep network recognise large objects using only small local filters.

The math

Discrete convolution as used in networks

Let the input be a grid XX with height HH, width WW, and CC channels. A filter, or kernel, is a block of weights KK with size k×k×Ck \times k \times C and a bias bb. The output at position (i,j)(i, j) is

S(i,j)=∑u=0k−1∑v=0k−1∑c=0C−1X(i+u, j+v, c) K(u,v,c)+bS(i, j) = \sum_{u=0}^{k-1} \sum_{v=0}^{k-1} \sum_{c=0}^{C-1} X(i+u,\, j+v,\, c)\, K(u, v, c) + b

This is a weighted sum of the patch under the filter. Here (i,j)(i, j) is the position of the output, (u,v)(u, v) index the rows and columns of the filter, and cc indexes the input channels. Each output position uses the same KK and bb.

The mathematical definition in Convolution flips the kernel before sliding it. Deep learning libraries skip the flip and compute what is properly called cross-correlation. The difference does not matter in practice, because the network learns whatever weights it needs, and a flipped kernel is just another kernel.

Each filter produces one output channel. With FF filters, the layer's output has FF channels, and its parameter count is

F (k2C+1)F \,(k^2 C + 1)

This count does not depend on the size of the image. That is the point of weight sharing.

Output size

With stride ss (how far the window moves each step) and padding pp (zeros added around the border), the output width along one side is

nout=⌊nin+2p−ks⌋+1n_{\text{out}} = \left\lfloor \frac{n_{\text{in}} + 2p - k}{s} \right\rfloor + 1

The same formula applies to height. Padding of p=(k−1)/2p = (k-1)/2 with stride 1 keeps the size unchanged for odd kk.

Nonlinearity and pooling

After each convolution, an activation such as Rectified Linear Unit (ReLU) is applied entry by entry. Without it, a stack of convolutions would collapse into a single convolution, because the composition of linear maps is linear. Pooling then reduces the grid size. Max pooling with a 2×22 \times 2 window and stride 2 takes the largest value in each non-overlapping 2×22 \times 2 block, halving the height and width. See Max Pooling for the details.

Classification head

After the final convolutional stage, the feature maps are flattened into a vector and passed through one or more dense layers. The last layer produces one score per class, and Softmax turns the scores into probabilities. Training minimises the Cross-Entropy Loss.

Gradients for a shared filter

Backpropagation through a convolution follows the same chain rule as in Backpropagation, with one new feature. A single kernel weight K(u,v,c)K(u, v, c) is used at every output position, so its total gradient is the sum of its contributions from all positions:

∂L∂K(u,v,c)=∑i∑j∂L∂S(i,j) X(i+u, j+v, c)\frac{\partial \mathcal{L}}{\partial K(u, v, c)} = \sum_{i} \sum_{j} \frac{\partial \mathcal{L}}{\partial S(i, j)} \, X(i+u,\, j+v,\, c)

This is the rule for any weight that is used more than once. Its gradients add up across the uses. The gradient with respect to the input XX is a transposed convolution of the output gradient with the same kernel, which is why Transposed Convolution appears in networks that upsample.

Worked examples

Example
A one-dimensional edge detector

Take the input sequence x=[1,2,3,0,1]x = [1, 2, 3, 0, 1] and the kernel K=[1,0,−1]K = [1, 0, -1]. The kernel has size k=3k = 3, stride 1, and no padding. The output length is (5−3)/1+1=3(5 - 3)/1 + 1 = 3.

  1. Position 0 covers [1,2,3][1, 2, 3]. The sum is 1⋅1+2⋅0+3⋅(−1)=−21 \cdot 1 + 2 \cdot 0 + 3 \cdot (-1) = -2.
  2. Position 1 covers [2,3,0][2, 3, 0]. The sum is 2⋅1+3⋅0+0⋅(−1)=22 \cdot 1 + 3 \cdot 0 + 0 \cdot (-1) = 2.
  3. Position 2 covers [3,0,1][3, 0, 1]. The sum is 3⋅1+0⋅0+1⋅(−1)=23 \cdot 1 + 0 \cdot 0 + 1 \cdot (-1) = 2.

The output is [−2,2,2][-2, 2, 2]. At each position the kernel subtracts the last entry of the window from the first, so it responds to change across the window. The first output is −2-2 because the input rises from 1 to 3 across that window, and the last two outputs are +2+2 because the first entry of each of those windows is larger than the last (2 against 0, then 3 against 1). A sharp change in a one-dimensional signal is called an edge, and this filter's output is large in size where one occurs.

Interpretation: a tiny, fixed set of three numbers produces a useful response at every position. A network learns the three numbers from data instead of having them written by hand.

Example
Parameters in a realistic first layer

Consider an RGB image of size 224×224×3224 \times 224 \times 3. The first convolutional layer uses 64 filters of size 3×3×33 \times 3 \times 3, with stride 1 and padding 1, so the output keeps its 224×224224 \times 224 size and has 64 channels.

Parameters in the convolution. Each filter has 3⋅3⋅3=273 \cdot 3 \cdot 3 = 27 weights and one bias, so the layer has

64⋅(27+1)=1,79264 \cdot (27 + 1) = 1{,}792

parameters.

Parameters in a fully connected layer of the same shape. The input has 224⋅224⋅3=150,528224 \cdot 224 \cdot 3 = 150{,}528 values. The output has 224⋅224⋅64=3,211,264224 \cdot 224 \cdot 64 = 3{,}211{,}264 values. A dense layer between them needs one weight per input-output pair:

150,528⋅3,211,264≈4.8×1011150{,}528 \cdot 3{,}211{,}264 \approx 4.8 \times 10^{11}

Comparison. The convolution uses about 1,800 parameters where a dense layer would use roughly 4.8×10114.8 \times 10^{11}. The convolution also has the translation property, which the dense layer lacks.

Interpretation: the gap is so large that dense layers are not used on raw images at this scale. Convolution is what makes image models feasible.

Tradeoffs and failure modes

Convolutional networks are strong when the data has local structure and the important patterns can appear anywhere. They are weaker when the relationships are global or the positions carry meaning.

SituationProblemBetter choice
Patterns depend on absolute position, such as a face's eyes being at the topShared filters ignore position unless the network is toldAdd coordinate channels, or use position encodings
Very small training setA large model still overfits, even with sharingData augmentation, pretrained weights, or fewer filters
Long-range dependencies, such as a sentence or a video where events are far apartLocal filters need many layers to connect distant positionsTransformer or Vision Transformer (ViT) with global attention
Dense per-pixel output, such as segmentationPooling discards resolutionEncoder-decoder designs like U-Net with skip connections
Deep stacks of layersGradients can vanish, and training stalls[[residual-connection
Input sizes that change between examplesDense layers at the end need a fixed sizeGlobal average pooling before the classifier

Convolutional networks also need care with padding and stride. A stride that does not divide the input size evenly can make the output grid awkward to align, and a poorly chosen padding can shift features by a pixel in each layer.

History

Kunihiko Fukushima described the Neocognitron in 1980. It was a multilayer network with local receptive fields inspired by the visual cortex, trained with a layer-wise learning rule rather than backpropagation.

Yann LeCun and colleagues combined convolution with backpropagation in the late 1980s. A 1989 paper applied this to recognising handwritten zip codes. Their later network, LeNet-5, was published in 1998 and read cheques in banks. It showed that the design worked on a real task, although it was limited by the computing power of its time.

The field changed in 2012. Alex Krizhevsky, Ilya Sutskever and Geoffrey Hinton trained a deep convolutional network, later named AlexNet, on the ImageNet dataset using graphics processors. It won the ImageNet competition by a wide margin, and deep convolutional networks became the standard approach to vision. The combination of large datasets, GPUs, and the ideas from LeCun's group made the difference.

Check yourself

QuestionRecall: what are the two main ideas that give a convolutional network fewer parameters than a fully connected one?Show answer

Local connectivity, where each neuron looks only at a small patch of the input, and weight sharing, where the same filter weights are used at every position. Together they mean the parameter count depends on the filter size and the number of filters, not on the image size.

QuestionComputation: a layer has 32 filters of size 5×55 \times 5 applied to an input with 3 channels. How many parameters does it have, including biases?Show answer

Each filter has 5⋅5⋅3=755 \cdot 5 \cdot 3 = 75 weights and one bias, so 76 parameters per filter. With 32 filters the total is 32⋅76=2,43232 \cdot 76 = 2{,}432.

QuestionWhy: why does the network need a nonlinearity such as ReLU between convolutions?Show answer

Convolution is a linear operation, and a composition of linear operations is itself linear. Without a nonlinearity, a stack of convolutional layers would be equivalent to one convolution, which cannot represent the complex functions needed for recognition. The ReLU after each convolution lets each layer combine features in a way that a single layer cannot.

QuestionApplied: a network for satellite images must classify whether a road crosses the centre of each tile, and the road can be anywhere in the tile. Would you use a convolutional network, and what would you check?Show answer

Yes. Roads are local structures that appear at different positions, which is exactly what weight sharing exploits. I would check that the receptive field after the final layer is large enough to see a whole road segment, since a road that spans much of a tile needs more than small filters can see. I would also check that pooling does not remove the thin road signal, and I would confirm that the labels really mean "a road crosses the centre" rather than "a road appears somewhere in the tile", because those are different tasks.

Sources