Skip to content
Lambdian

Convolutional neural networks

Weight sharing for images: convolutions, feature maps, pooling and deep vision architectures.

Animated explanation

The idea

A 224 × 224 colour image has 150,528 numbers. A fully connected layer of 1,000 units on it would need 150 million weights, and it would have to learn separately that a cat in the top-left is the same as a cat in the bottom-right.

A convolutional layer instead learns a few small filters (say 3 × 3) and slides each one across the whole image, computing a dot product at every position. The result is a feature map that lights up wherever the filter's pattern appears. Two ideas make this powerful: local connectivity (each output looks only at a small patch) and weight sharing (the same filter is used everywhere, so a learned edge detector works at every position). Stacking conv layers builds a hierarchy: edges → textures → parts → objects.

Channels, stride, padding, pooling

A conv layer takes CinC_\text{in} input channels and learns CoutC_\text{out} filters, each of shape k×k×Cink\times k\times C_\text{in}. Stride skips positions (downsampling); padding adds a border of zeros so the output keeps its size. Max pooling keeps the largest value in each 2×2 block, halving the resolution and adding a little tolerance to small shifts. The receptive field of a unit (the input region that affects it) grows with every layer.

Architectures in one paragraph

LeNet (1998) read digits; AlexNet (2012) won ImageNet with GPUs and ReLU; VGG showed that stacks of 3×3 convolutions work; ResNet (2015) added skip connections x+F(x)\mathbf{x} + F(\mathbf{x}) so 100+ layer networks train; modern vision mixes convolutions with attention. In practice you rarely train from scratch: transfer learning fine-tunes a network pretrained on millions of images.

The algorithm, step by step

  1. 1
    Input: an image tensor of shape (C, H, W), normalised.
  2. 2
    Conv layer: for each output channel, slide its k×k×Ck\times k\times C kernel over every position; add a bias.
  3. 3
    Apply ReLU; optionally max-pool to reduce resolution.
  4. 4
    Repeat conv/ReLU/pool blocks, increasing channels as resolution falls.
  5. 5
    Global-average-pool, then a dense layer with softmax; train end-to-end with backpropagation.

The maths

2-D convolution (cross-correlation)

S(i,j)=∑c∑u,vIc(i s+u, j s+v) Kc(u,v)+bS(i,j) = \sum_{c}\sum_{u,v} I_c(i\,s+u,\ j\,s+v)\,K_c(u,v) + b

Output size

nout=⌊(nin+2p−k)/s⌋+1n_\text{out} = \lfloor (n_\text{in} + 2p - k)/s \rfloor + 1

Parameters per layer

(k2Cin+1) Cout(k^2C_\text{in} + 1)\,C_\text{out}

Residual block

y=x+F(x; W)\mathbf{y} = \mathbf{x} + F(\mathbf{x};\,W)

Strengths and weaknesses

Strengths

  • Far fewer parameters than dense layers (weight sharing)
  • Built-in sense of locality and translation
  • Pretrained models transfer to new tasks with little data

Weaknesses

  • Needs a lot of data and compute to train from scratch
  • Not inherently rotation- or scale-invariant
  • Hard to interpret (saliency maps help)

Reach for it when…

Images, video, spectrograms, and any grid-structured signal where local patterns matter.

Check your understanding

Question 1

Q1A conv layer has 64 filters of size 3×3 over 32 input channels. How many parameters (with biases)?

Question 2

Q2Input 32×32, kernel 5×5, stride 1, no padding. Output size?

Question 3

Q3What does weight sharing give a CNN?

Lambdian Lab

Convolution and pooling in NumPy

Single-channel operations, as in a CNN layer (cross-correlation, no kernel flip).

  • conv2d(img, kernel, stride=1, pad=0): zero padding, then valid cross-correlation.
  • relu(x) and maxpool2d(x, size=2) (non-overlapping; drop leftover rows/columns).
  • out_size(n, k, p, s) from the formula.
  • conv_params(k, c_in, c_out) including biases.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.