Skip to content

3.6 Convolutional networks

ready≈ 11.5 hoursNeeds: 3.2Unlocks: 3.7, 6.1
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Convolution scanner, Feature map viewer)
  4. 4 Do
  5. 5 Check

A convolution is a constrained dense layer. A dense layer from a 32×32×3 image to a same-sized output needs about 9.4 million weights. A 3×3 convolution with three input and three output channels needs 81 (plus 3 biases). Each output connects only to a small window of the input (sparse), and every window uses the same weights (shared). Because the same weights run everywhere, shifting the input shifts the output: convolution is translation equivariant (UDL §10.1–10.2). Deep learning libraries compute cross-correlation, sliding the kernel without flipping it, and call it convolution.

Output size. Along each axis, an input of size nn with kernel kk, padding pp, stride ss and dilation dd gives ⌊(n+2p−d(k−1)−1)/s⌋+1\lfloor (n + 2p - d(k-1) - 1)/s \rfloor + 1 outputs. Padding p=(k−1)/2p = (k-1)/2 with stride 1 keeps the size (“same”); stride 2 roughly halves it (CS231n convolution notes; Dumoulin and Visin 2016).

Receptive fields and pooling. One unit of a stack of 3×3 layers sees a region that grows by 2 per layer, and every stride multiplies later growth. Max-pooling keeps the largest value in each window: it shrinks the map, adds a little invariance to small shifts, and in the backward pass sends the gradient only to the winner (UDL §10.4). Early filters typically detect edges and strokes; later ones combine them into parts (Zeiler and Fergus 2014).

im2col. Copy every receptive field into a row of a matrix, and the whole convolution becomes one matrix product with the flattened kernels, which is how fast libraries compute it (CS231n). The backward pass of a convolution is again a convolution:

Gradient of a convolution is a convolution with the flipped kernelstep 1 of 4
  1. yij=∑a,bwab xi+a, j+by_{ij} = \sum_{a,b} w_{ab}\, x_{i+a,\,j+b}

★ Convolution scanner

Feature maps, output sizes, receptive fields

Static view on small screens. Open this page on a laptop to interact.

input (padding dashed)0-1000100000-2101211100-1101111100111110011111001111100000000output 5×53000-34000-44000-44000-43000-3
output size ⌊(n + 2p − d(k − 1) − 1) / s⌋ + 1 = ⌊(7 + 0 − 2 − 1) / 1⌋ + 1 = 5 receptive field of one output: 3×3 input cells

y[0][0] = -1·0 + 1·0 + -2·0 + 2·1 + -1·0 + 1·1 = 3

The same 3×3 weights slide over every position: that weight sharing is why a convolution has 9 parameters per input–output channel pair, whatever the image size, and why shifting the input shifts the output. Stride skips positions, padding adds a border of zeros so edges get full windows, and dilation spreads the kernel's taps apart to see a wider area with the same weights. Click an output cell to see its window.

An input grid of numbers with its zero padding drawn dashed, and the output grid of a 3×3 convolution. Selecting an output cell highlights the input cells it reads, labels each with its weight, and writes out the sum of products. Sliders set the input size, stride, padding and dilation; the readout evaluates the output-size formula. Scan steps through every output position. Keyboard: Tab through the controls; Tab to an output cell and press Enter to select it.

Feature map viewer

What each layer detects

Static view on small screens. Open this page on a laptop to interact.

input 28×28 (drag to draw)

Layer 1: stroke detectors, conv 3×3 → ReLU → max-pool 2 (14×14)

vertical stroke |
horizontal stroke —
diagonal stroke ↘
diagonal stroke ↗

Layer 2: junction detectors, conv 3×3 over the 4 maps + bias → ReLU (14×14)

top-left corner ┌
bottom-right corner ┘
plus crossing +
diagonal crossing ×

The weights here are set by hand so you can read them: each layer-1 filter is +2 along one orientation and −1 beside it, and each layer-2 unit adds up two layer-1 maps near the same spot. A single stroke already gives a fair sum, so the negative bias is what makes a layer-2 unit an AND: it only fires where both strokes meet. Drag a bias to 0 and watch the unit fire along every stroke. Trained CNNs learn filters like these in their first layer, and more abstract combinations further up.

A 28×28 image (a shape, or your own drawing) passes through a two-layer convolutional network with hand-set weights. Four layer-1 maps show where vertical, horizontal and diagonal strokes are. Four layer-2 maps show where two strokes meet as a corner or a crossing; their bias sliders set how strongly both inputs must agree before the unit fires. Keyboard: Choose an image from the list; drawing needs a pointer; Tab to the bias sliders.

Write the output-size formula, im2col, a convolution forward pass built on it, the backward pass and max-pooling. The tests compare you with a direct four-loop convolution, run the gradient checker for several strides and paddings, and check equivariance.

Convolutional networks stubs/m3_6_conv.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m3_6_conv.py and run mlc check 3.6. The tests are identical; import the results on the progress page.

Hints · im2col 0/3 revealed
Hints · conv2d_backward 0/3 revealed
Hints · maxpool_backward 0/3 revealed

Locally: mlc check 3.6.

1. A 32×32 input, 5×5 kernel, stride 1, no padding. Output size?
2. Input 224×224, kernel 7×7, stride 2, padding 3. Output size?
3. How many parameters does a 3×3 conv layer with 64 input and 128 output channels have (with biases)?
4. Three stacked 3×3 conv layers with stride 1. How large is one output unit’s receptive field?
5. What does max-pooling send backwards?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.