Visual explainer
Convolutional Neural Networks
Trace local filters, shared weights, pooling and ReLU through a small image classifier, then check its spatial limits.
A convolutional neural network (CNN) processes local image neighborhoods with filters reused across the grid. Flattening is a reversible rearrangement when the original shape is known; it does not erase pixel values. But a general dense layer does not build in the local connectivity and weight sharing that a convolution supplies.
In this 4 by 4 row-major example, vertically adjacent A and B become vector indices 0 and 4. The drawing crops the vector to its first five slots; sixteen values remain in total.
A Filter Makes a Map
This synthetic local example has four identical input rows [1, 0, 0, 0]. Use a 3 by 3 kernel with each row [1, 0, −1], stride one, no padding and zero bias. The first window adds its left column and subtracts its right: 1 + 1 + 1 − 0 − 0 − 0 = 3. Move right and both columns are zero, giving 0. Move down and the same results repeat, producing rows [3, 0] and [3, 0].
The highlighted window supplies the highlighted output. Output cells are enlarged for readability. Deep-learning libraries call this unflipped-kernel operation convolution; Conv2d specifies the cross-correlation convention and summation over input channels.
Trace the Tensor Shapes
Now switch from the local arithmetic fixture to a complete, illustrative architecture. Shapes are written height by width by channels; batch size is omitted. The squares' sides share a scale of six drawing units per spatial sample: 28, 14 and 7 become 168, 84 and 42. Interior guide lines are schematic, not a literal count of pixels. Channel count is supplied by the labels, independently of square area.
Start at 28 by 28 by 1. A 3 by 3 convolution with stride one, padding one and eight output channels gives 28 by 28 by 8. ReLU keeps that shape. A 2 by 2 max pool at stride two, without padding, gives 14 by 14 by 8. Repeat with sixteen convolution outputs, ReLU and another pool to obtain 7 by 7 by 16. Increasing channels is a design choice; pooling does not create channels.
This is an invented teaching network, not a trained accuracy claim. The PyTorch network tutorial shows how these kinds of operations compose into a classifier.
Reduce Each Channel
Zoom into a separate illustrative feature patch with rows [1, 5] and [2, 4]. Its unique maximum is 5. Pooling operates independently per channel; with a two-wide window and stride two, an even side length halves. This four-value detail explains the reduction in the architecture, without claiming these are measured activations from a trained model. See pooling layers for overlapping windows and shift limits.
Reuse the Weight Bank
A and B mark two locations in one schematic 28 by 28 output map. The same kernel serves both. The nine kernel cells and separate bias symbol represent one of eight filters, each reading the single input channel. Their combined parameter count is 8 × (3 × 3 × 1 + 1) = 80, including biases. Those parameters produce 28 × 28 × 8 = 6,272 responses for one image.
For the second convolution, the channel sum changes the count: 16 × (3 × 3 × 8 + 1) = 1,168. Fewer parameters do not automatically mean fewer arithmetic operations; shared kernels are applied at many locations. This is a structural count, not an equal-capability comparison against an unrelated dense layer.
Change the Downsampling Rule
Reuse the patch [1, 5; 2, 4]. Max pooling gives 5. A two-by-two convolution with chosen weights [1, 0; 0, 0], zero bias and stride two instead gives 1. The lower route illustrates a possible learned filter, not a recommendation.
Both operations reduce spatial samples. A strided convolution can learn the aggregation but does not guarantee preservation of exact boundaries. Keep higher-resolution information when the task needs it.
Grow the Receptive Field
The dot marks one output location. One 3 by 3 convolution can read the dashed 3 by 3 neighborhood. Two such layers at stride one and dilation one can depend on the outer 5 by 5 input region: each adds two samples to the theoretical receptive-field side, giving 1 + 2 + 2 = 5. This isolates consecutive convolutions, rather than the pooling architecture's full receptive field. At a padded boundary, some of that footprint contains padding rather than observed pixels. Learned weights or inactive responses can reduce actual influence.
Add the Nonlinearity
ReLU applies max(0, x) independently to each response. The illustrative slice [−2, 0, 3] becomes [0, 0, 3]; the number and positions of entries stay fixed. It adds nonlinearity without learned parameters. This step occurs after each convolution in our shape fixture.
Features Become Class Scores
Flatten 7 × 7 × 16 = 784 responses, then apply a dense layer with ten outputs. The drawing shows three of those logits on one signed scale: 30 drawing units per logit, measured from the vertical zero reference. A dot represents zero. The branch junction means that the same feature vector feeds every output.
To check a small numerical head, choose a synthetic vector whose first two values are 2 and 1 and whose other 782 values are zero. For the three displayed classes, use first-two weight pairs (1, 0), (0, −1) and (1, −2), all other weights zero and biases zero. The dot products give 2, −1 and 0. Set the remaining seven biases to −2 with zero weights for this example. The first displayed logit wins; logits are scores, not probabilities.
The ten-output layer has 784 × 10 + 10 = 7,850 parameters. The whole illustrative network has 80 + 1,168 + 7,850 = 9,098 parameters. Neither flattening, ReLU nor max pooling adds trainable parameters.
Where It Breaks
Consider a smaller 8 by 8 input and keep applying unpadded 2 by 2, stride-two, floor-rounded pooling. The sides become 4, 2 and 1 after three pools. A fourth window cannot fit: its three missing positions are crossed out. The output-size calculation gives zero, which makes this configuration invalid in MaxPool2d; it is not a useful empty tensor passed to a classifier.
A 1 by 1 map can still feed a classification head. The failure is attempting the additional incompatible pooling operation. Match the reduction schedule to the input shape, and preserve spatial paths when location matters.
The Quick Version
- Flattening preserves values but changes spatial indexing.
- Shared local kernels produce spatially ordered feature maps.
- Convolution chooses channels; pooling reduces spatial samples.
- Max pooling keeps a window's largest response.
- Shared weights serve many output locations.
- Learned stride also discards spatial samples.
- Stacked convolutions can widen theoretical receptive fields.
- ReLU adds nonlinearity while preserving shape.
- A dense head converts features to class logits.
- A pooling window must fit its input under chosen settings.