Convolutional neural networks

core 11 minarchitecturescnncomputer-vision

Builds on:Forward passBackpropagationMatrices transform space

The idea

A convolution layer slides a small learned kernel across the input, taking a dot product at each position — the same handful of weights, reused everywhere. (Frameworks actually compute cross-correlation, no kernel flip; nobody cares, the weights are learned.) For a kernel ww of size k×kk \times k over input patch xx centered at (i,j)(i, j):

yi,j=b+∑u,vwu,v xi+u, j+vy_{i,j} = b + \sum_{u,v} w_{u,v}\, x_{i+u,\, j+v}

With multi-channel input, each filter spans the full channel depth (k×k×Cink \times k \times C_{\text{in}} weights) and produces one output channel; CoutC_{\text{out}} filters stack into the output volume.

Two design commitments do all the work:

The knobs: stride ss (step size; stride 2 halves resolution), padding pp (zeros around the border; p=(k−1)/2p = (k-1)/2 preserves size at stride 1), and filter count CoutC_{\text{out}}. Output size per dimension:

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

Depth builds a hierarchy: edges → textures → parts → objects. Each layer's receptive field (input region influencing one activation) grows with depth, so late layers reason about large structures using features computed once, locally, by early layers. A standard modern trunk: repeated conv → BN → ReLU blocks, downsample by stride-2 convs a few times, global average pool, one linear classifier.

Worked example

A 1-D convolution you can do in your head. Input (1,2,3,4,5)(1, 2, 3, 4, 5), kernel (1,0,−1)(1, 0, -1), stride 1, no padding. Output size: ⌊(5+0−3)/1⌋+1=3\lfloor(5 + 0 - 3)/1\rfloor + 1 = 3.

WindowDot productOutput
(1,2,3)(1,2,3)1⋅1+0⋅2−1⋅31 \cdot 1 + 0 \cdot 2 - 1 \cdot 3−2-2
(2,3,4)(2,3,4)2−42 - 4−2-2
(3,4,5)(3,4,5)3−53 - 5−2-2

This kernel is a difference detector — on a constant-slope ramp it reads a constant −2-2 everywhere. Learned 2-D kernels in first layers are exactly this species: oriented edge and gradient detectors.

Parameter arithmetic that wins interviews. CIFAR-scale input 32×32×332 \times 32 \times 3, one conv layer: 16 filters of 5×55 \times 5, stride 1, padding 2.

The output shape and parameter count are

nout=⌊32+4−51⌋+1=32,output shape=32×32×16,conv parameters=(5⋅5⋅3+1)⋅16=1,216.\begin{aligned} n_{\text{out}} &= \left\lfloor\frac{32+4-5}{1}\right\rfloor+1 = 32, \\ \text{output shape} &= 32\times32\times16, \\ \text{conv parameters} &= (5\cdot5\cdot3+1)\cdot16 = 1{,}216. \end{aligned}

A dense layer mapping the same 3,072 inputs to 16,384 outputs would need

3072×16384≈50.3 million weights,3072\times16384 \approx 50.3\text{ million weights},

a factor of roughly 41,000 more. Same input and output sizes, five orders of magnitude fewer parameters, with position-sharing built in.

Cheat sheet

Formulas

Components

Facts and gotchas