AlgoPlusAlgoPlus
Learn/Machine Learning
Lesson

Convolutional Neural Network

Slide a small shared filter across an image to detect features anywhere — far fewer weights, built-in translation tolerance.

9 min read Watch it move Build it

A convolutional neural network (CNN) reads images by sliding a small grid of weights — a filter — across the picture, instead of wiring every pixel to its own weight. At each spot it multiplies the filter against the underlying pixels and sums. That step is convolution, and its output is a feature map showing *where* the filter's pattern (say a vertical edge) appears.

One pair of glasses for the whole image

The big idea is weight sharing: the *same* filter is reused at every position. A vertical edge looks the same in the top-left corner as the bottom-right, so why learn a separate weight per pixel? Sliding one detector everywhere means drastically fewer weights and gives the network translation tolerance — it spots a feature wherever it occurs.

image patch      filter (3x3)     element-wise multiply + sum
 0 1 1            -1 0 1
 0 1 0     *      -1 0 1     ->     one number in the feature map
 0 0 1            -1 0 1            (bright = pattern found here)

slide the filter one step over and repeat across the whole image.

Convolution, then pooling, stacked

  1. 1Convolution — slide each filter over the image; each produces a feature map of where its pattern fires.
  2. 2Activation — apply ReLU so the maps are non-linear.
  3. 3Pooling — downsample each feature map, usually by keeping the maximum of every small block, which shrinks it and adds tolerance to small shifts.
  4. 4Stack — repeat. Early layers learn edges, deeper layers combine edges into textures, then textures into whole objects.
Hierarchy of features
A CNN builds understanding bottom-up: the first layer's filters find edges and color blobs, the next compose those into corners and textures, and deeper layers assemble parts into objects. Nobody hand-designs this hierarchy — it emerges from training.
Why not a plain MLP on pixels
Flattening a 224x224 color image and feeding a fully-connected layer needs millions of weights for the first layer alone, and it would have to relearn every feature in every location. Weight sharing makes a CNN both far smaller and naturally position-tolerant.
OperationTimeSpace
Convolution layer · weight sharing keeps params smallO(pixels · filters)O(feature maps)
Pooling layer · downsamples, no learned weightsO(pixels)O(1)
Check yourself
What does weight sharing give a CNN compared to a fully-connected network on the same image?