Convolutional networksabout 10 minutes

Feed a 224×224 colour photo into a dense layer of 1,000 units and that one layer has million weights. Worse, it ignores two facts about images: nearby pixels are related (locality), and a cat's ear is a cat's ear wherever it appears (translation). Convolutional layers build both facts into the architecture.

The operation

A kernel (or filter) is a small grid of weights, say 3×3. Slide it across the image; at each position, multiply the kernel element-wise with the patch underneath and sum. The results form an output image called a feature map:

Pick a kernel and move it over the image. Each output cell is the dot product of the kernel with the patch under it.

A kernel is a pattern detector: the output is large where the patch looks like the kernel (a dot product again). An edge kernel like Sobel responds to brightness changes; a blur kernel averages. In a CNN nobody designs the kernels: they are weights, learned by backpropagation. (Strictly, the operation above is cross-correlation; true convolution flips the kernel first. Deep learning libraries compute cross-correlation and call it convolution, and since the kernel is learned the distinction doesn't matter.)

Shapes: padding, stride, channels

For an input, a kernel, padding (zeros added around the border) and stride (how far the kernel jumps each step), the output side is:

  • No padding ("valid"): a 3×3 kernel shrinks 28×28 to 26×26. Padding 1 ("same") keeps it at 28×28.
  • Stride 2 halves the resolution, a cheap way to downsample.
  • Real images have channels (red, green, blue), and hidden layers have many feature maps. A kernel spans all input channels: its shape is , and a layer with kernels produces feature maps. Parameters: .

Two properties make convolution so effective. Parameter sharing: the same 3×3×3 = 27 weights are used at every position, so a layer with 64 such kernels has 1,792 parameters whatever the image size. Translation equivariance: shift the input and the feature map shifts by the same amount, so a detector learned in one corner works everywhere.

try itedit the code and run it, nothing can break
python sleeps until you press run

A convolution slides one small set of weights across the whole image. That builds in locality and translation, and cuts parameters by orders of magnitude.

← Project: A digit recogniser from scratch