CNN
A CNN (convolutional neural network) finds patterns in images by sliding a small grid of weights (a filter) across every location and scoring how strongly each patch matches. Because the same weights are reused at every position, a CNN detects a feature anywhere in the image with a tiny fraction of the parameters a dense layer would need.
Think of the filter as a stencil: cut a vertical slot in a card, hold it against each window of a photograph, and check whether the patch behind it lines up with the slot. One card tests the same pattern at every position instead of needing a separate stencil for each location. The filter's nine numbers define the shape of that slot.
How a CNN Works
A convolution lays the filter over a 3×3 patch of the image, multiplies each pixel by its corresponding weight, and adds the nine products into one number. That number becomes one cell of the feature map, measuring how strongly the patch matches the pattern. The filter then slides one step right and repeats, dropping to the next row at the end of each line. Because the filter never hangs off the edge here ("valid" convolution), a 6×6 image under a 3×3 filter yields a 4×4 feature map. All sixteen positions share the same nine weights.
Raw convolution scores can be negative: a filter tuned to bright-to-dark edges produces a large negative score on a dark-to-bright edge. ReLU, defined as max(0, x), clamps every negative score to zero, keeping only locations that match the filter's polarity and adding nonlinearity to the layer.
Max-pooling then shrinks the map: a 2×2 window steps across it with stride 2 and keeps only the largest value in each window, reducing the 4×4 map to 2×2. The exact pixel of the strongest match is dropped; only the presence of a strong match within that quadrant survives. Stacked over many layers, this local position tolerance helps a CNN recognize a cat whether it sits left or right of center.
This lesson shows one small layer: a single hand-picked filter and one convolve → ReLU → pool pass. A real CNN learns dozens of filters per layer and stacks many layers, with early layers detecting edges and later layers composing them into shapes. Real architectures repeat the convolve-and-activate step; the choice of activation and whether to pool after a given layer depend on the architecture.
CNN Step by Step
The default run sweeps the vertical-edge filter (three rows of [1, 0, −1]) across a 6×6 image containing a bright vertical bar: pixels of value 9 in columns 2–3, rows 1–4, and zeros elsewhere. Each row of the filter adds the patch's left pixel and subtracts its right pixel, so the filter computes left brightness minus right brightness.
- Position (0, 0). The top-left 3×3 patch is
[[0, 0, 0], [0, 0, 9], [0, 0, 9]]. Multiplying elementwise, the left column multiplies by +1 and contributes 0, the middle column multiplies by 0, and the right column multiplies by −1 and contributes −9 twice, for a sum of −18. The patch holds a dark-to-bright edge, opposite in polarity to the filter, so the score is strongly negative. - Sweep the remaining 15 positions. The completed 4×4 feature map reads, row by row:
[−18, −18, 18, 18],[−27, −27, 27, 27],[−27, −27, 27, 27],[−18, −18, 18, 18]. Negative scores trace the bar's left edge, positive ones its right edge, and the magnitudes peak at 27 where all three filter rows sit on the full bar. - ReLU.
max(0, x)zeroes the left half: the map becomes[0, 0, 18, 18],[0, 0, 27, 27],[0, 0, 27, 27],[0, 0, 18, 18]. Only the edge whose contrast matches the filter survives. - Max-pool, 2×2, stride 2. The two left windows contain only zeros (output 0); the two right windows each contain 27 (output 27). The result is
[[0, 27], [0, 27]], a quarter-size summary locating the strong vertical edge on the right side.
On the canvas, the purple read-window moves across the image while the workbench beneath it shows the live arithmetic: the current patch, the nine products, and their sum. Feature-map cells fill as they are computed (dark for positive scores, blue for negative). On the ReLU beat the blue cells clear, and the pooled grid takes a green border when the layer's output finishes.
Why Convolutions Beat Dense Layers on Images
A single 3×3 filter uses nine weights whether the input is a 6×6 grid or a 4K photograph, because the same weights apply at every position. A dense layer assigns each input pixel its own weight per unit: even a 28×28 image requires 784 weights per neuron, and nothing ties a neuron's weights at one location to those at another, so a pattern learned in one region is not automatically reused elsewhere. Weight sharing provides both parameter efficiency and position-independent feature detection. The computation cost is O(out_h · out_w · k²), one k×k dot product per output cell, scaling linearly with image area rather than quadratically with pixel count.
The Random filter recipe serves as a control experiment. Running it on the default vertical-bar image applies nine unstructured weights through the exact same convolution, ReLU, and pooling steps. On the first press, the feature map comes out as a diffuse pattern of 9s and pools to [[9, 9], [18, 9]], which fails to localize the edge (each further press cycles to another fixed untrained filter on the current image). Compared with the vertical filter's [[0, 27], [0, 27]], the difference comes from the filter's weights. In a real CNN, gradient descent learns those values; the hand-picked edge filters in the other recipes stand in for learned weights.
The filter in the Inputs panel is a 3×3 grid of clickable cells that cycle through −1, 0, and 1, recomputing the entire pipeline on each change. Three targeted checks show how the weights shape the output. Setting all nine cells to 1 builds a local brightness detector whose pooled output is [[54, 54], [54, 54]]: the responses retain brightness information, but this pooled result does not localize the edge. Taking the horizontal-edge filter and zeroing out its middle column preserves the location ([[0, 0], [18, 18]] instead of [[0, 0], [27, 27]]) while reducing the peak by a third (27 → 18) and halving the outer responses (18 → 9), showing each column's contribution to the sum. Finally, try to design a filter that lights up only the bar's top edge on the horizontal image, then compare the pooled 2×2 output against your prediction.
Edge Cases
- Valid vs padded convolution. This lesson never lets the filter hang off the image, so the output shrinks 6×6 → 4×4. For this 3×3 kernel and stride 1, one zero-padding cell on each border keeps the output size equal to the input size, which is standard in deep stacks where repeated shrinking would collapse the spatial dimensions.
- Flat regions. Where every pixel in a patch is equal, left minus right is exactly zero. The corners of the default feature map read ±18 rather than 0 only because the bar's edge clips them.
- Opposite-contrast edges. A dark-to-bright edge scores large and negative; ReLU discards that polarity; the negative responses themselves are still meaningful signed edge information.
- Pooling that doesn't divide evenly. A 2×2 / stride-2 window tiles a 4×4 map perfectly. Over an odd-sized map the last window falls off the edge; real implementations pad or drop the remainder.
Common Mistakes
- Imagining a different filter per position. It is one stencil, reused everywhere. Giving each local position its own weights creates a locally connected layer; a dense layer additionally connects each unit to all inputs.
- Skipping the nonlinearity. A stack containing only linear convolutions remains a linear map, although its boundary and parameter structure can differ; ReLU and max-pooling add nonlinearity in this lesson.
- Confusing window size with stride. Size sets how many cells each pooling window reads; stride sets how far it jumps. Equal size and stride gives the non-overlapping tiling shown here.
- Reading the feature map as a smaller picture. A high value means the filter matched that patch, not that the underlying pixel was bright. Each cell records a filter response rather than a downsampled pixel intensity.
- Assuming max is the only pooling. Average pooling takes the window's mean and smooths responses instead of selecting the peak; strided convolutions can also downsample. Max-pooling is common in classification, but the downsampling operation depends on the architecture.
Watch the 4K narrated walkthrough
A 5:45 English walkthrough on YouTube built directly from this interactive canvas.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Found a mistake or something unclear?