Learn
A4.3.9 | CONVOLUTIONAL NEURAL NETWORKS (HL ONLY)
01 | IMAGES HAVE NEIGHBOURHOODS, NOT JUST NUMBERS
A picture is a grid in which nearby pixels form edges, textures and shapes. If every pixel is treated as an unrelated input, that useful spatial structure is harder to exploit efficiently.
A convolutional neural network (CNN) uses local connections and shared learned filters to process spatial patterns. Successive layers combine earlier responses into more complex representations, supporting tasks such as image classification.
This HL lesson follows an image through convolution, activation, pooling and a classification head. We will distinguish a network’s learned parameters from architecture choices, and a forward calculation from training.
02 | REPRESENT THE IMAGE AS A TENSOR
A grayscale image can be represented by a height × width grid of intensity values. An RGB image has red, green and blue channels, so one image may have shape 32 × 32 × 3.
A tensor is a multidimensional array of values. The precise axis order depends on the implementation; this lesson writes height × width × channels, leaving the batch dimension out of single-image examples.
Numerical preparation matters. Resize and scale inputs consistently under a fitted or defined pipeline, and retain meaningful labels. A CNN does not automatically fix wrong labels or discover missing details that the image never recorded.
03 | A FILTER LOOKS AT A SMALL PATCH
A kernel is a small array of weights used to combine a local patch. A 3 × 3 kernel covers a three-row, three-column neighbourhood at one position.
Multiply each patch value by its corresponding weight, sum the products and add a filter bias. Move the same weights to the next position and repeat. The output responses form a feature map.
CNN libraries commonly compute this operation without flipping the kernel; mathematically it is cross-correlation, though it is usually called convolution in deep learning. The activity uses that standard unflipped convention so every product is unambiguous.
04 | FOLLOW ONE FILTER RESPONSE
Consider a binary-intensity image with every row equal to 0, 0, 1, 1, 1. The top-left 3 × 3 patch has three rows of 0, 0, 1. Use the demonstration filter with each row equal to −1, 0, 1 and bias 0.
| Patch row | Filter row | Contribution |
|---|---|---|
| 0, 0, 1 | −1, 0, 1 | 0×(−1) + 0×0 + 1×1 = 1 |
| 0, 0, 1 | −1, 0, 1 | 1 |
| 0, 0, 1 | −1, 0, 1 | 1 |
Add the three contributions: z = 1 + 1 + 1 + 0 bias = 3. This filter responds to a left-to-right intensity change in that patch. A patch of all ones gives zero under these weights.
The response is not a class label and need not be between 0 and 1. The supplied filter demonstrates a computation; a trained CNN normally adapts its filters from data rather than being told every useful edge detector.
05 | WEIGHT SHARING REUSES THE SAME DETECTOR
Weight sharing means the same filter parameters are applied at different spatial positions. The network can respond to a local pattern without learning a separate set of weights for every location.
Different filters within the layer can respond to different patterns. Reusing a filter across positions reduces parameter needs compared with separately learning every local position’s weights.
This supports a related response when a pattern moves, but it does not guarantee perfect translation invariance. Stride, padding, pooling, boundaries and the later classification head can all affect the final prediction.
06 | CHANNELS AND FILTER COUNT HAVE DIFFERENT ROLES
For a standard dense convolution over RGB input, one output filter contains weights across all three input channels. A 3 × 3 spatial kernel therefore has 3 × 3 × 3 weights for that filter, plus one bias if used.
Eight such filters produce eight output feature-map channels. Kernel size describes the local spatial window; filter count describes how many learned response maps the layer produces.
Later layers combine values across previous feature-map channels, not only raw colour channels. Some specialised convolutions use other connection patterns; this lesson uses ordinary fully channel-connected convolution.
07 | ACTIVATION MAKES THE REPRESENTATION NONLINEAR
After a convolutional weighted sum, an activation such as ReLU can apply max(0,z) independently to each response. A sum of −3 becomes 0; a sum of +3 remains 3.
Nonlinear activations let layers model relationships beyond an overall affine mapping. Stacking only weighted sums and biases does not by itself create that expressive nonlinearity.
ReLU is not pooling, training or a class-probability function. It transforms feature responses. Some negative evidence can be useful, so other activation choices are possible; choose and evaluate them for the task.
08 | STRIDE CHANGES WHERE THE FILTER IS APPLIED
Stride is the spacing between successive kernel positions. Stride 1 moves one input position at a time; stride 2 moves two. Larger stride usually produces a smaller spatial output and fewer positions to calculate.
Fewer positions can reduce computation and storage, but can skip fine detail or change which patterns are detected. Changing stride is an architecture choice, not a learned weight adjustment.
In the activity, the 2 × 2 contrast filter detects the vertical boundary at one stride-1 position. With stride 2 and no padding, the selected patches can miss that boundary. That is a concrete effect of sampling positions, not evidence that stride 2 is always unsuitable.
09 | PADDING CONTROLS BORDER TREATMENT
Padding adds a defined border around the input before applying the kernel. Zero padding uses added zeros. It can let filters process positions near the original edge and affect output size.
With no padding, a kernel is used only where it fits fully within the input. This is often called valid padding. With an appropriate same-padding convention and stride 1, spatial output can retain the input height and width.
Added zeros are not newly discovered image information. A filter can respond to the artificial boundary, so padding affects values as well as dimensions. For the activity, padding is explicitly either 0 or one zero-valued border on every side.
10 | CALCULATE THE SPATIAL OUTPUT SIZE
For a square input of size N, square kernel K, stride S, symmetric padding P and dilation 1, one output dimension is floor((N + 2P − K) / S) + 1, when the kernel fits. Floor means round down to an integer.
A 5 × 5 input, 3 × 3 kernel, stride 1 and no padding gives floor((5−3)/1)+1 = 3, so one filter produces a 3 × 3 map.
With stride 2, it gives floor((5−3)/2)+1 = 2. With stride 1 and one zero border, it gives floor((5+2−3)/1)+1 = 5. Filter count adds output channels; it does not replace this spatial calculation.
11 | TRACE THE SLIDING KERNEL
Inspect the image patch, filter weights and resulting map. Reveal one position at a time or show the full calculation. Changing a setting starts a fresh trace. All filters are supplied teaching weights; this activity does not train them.
Try the horizontal filter on the repeated-row image: without padding, its responses are zero. Add padding and inspect the border responses. Then compare stride 1 and 2 with the 2 × 2 vertical filter. The highlighted cells identify the current patch; row and column labels start at 0.
12 | POOLING SUMMARISES LOCAL RESPONSES
Pooling combines a small region of a feature map into a summary. Max pooling takes its largest value; average pooling takes its mean. A 2 × 2 pooling window with stride 2 can halve each spatial dimension when the input size fits.
Pooling is usually applied independently to each channel. It does not normally combine all channels into one, and ordinary max or average pooling has no trainable kernel weights.
Reducing spatial detail can make later computation smaller and can provide some tolerance to small shifts. It also loses information, and exact shift invariance is not guaranteed. Some CNN designs use strided convolutions or other reductions instead.
13 | COMPARE MAX AND AVERAGE POOLING
For the top-left block [1, 3; 4, 2], max pooling gives 4 and average pooling gives (1+3+4+2)/4 = 2.5. The two methods retain different information.
The fixed input is a feature map, not raw class labels. Max pooling preserves strong local responses while discarding the other values; average pooling preserves a local mean but can reduce a sharp response. Neither method learns the filter weights.
14 | RECEPTIVE FIELDS GROW THROUGH THE NETWORK
A unit’s receptive field is the region of earlier input that can affect it. A first 3 × 3 convolution directly sees a local 3 × 3 patch. Later units combine earlier responses, so their effective region in the original image can be larger.
For two consecutive stride-1, dilation-1 3 × 3 convolutions without intervening downsampling, an interior response can depend on a 5 × 5 original region. It does not see only one unchanged 3 × 3 patch forever.
Stride and pooling change the spacing and coverage of later receptive fields. A larger receptive field makes broader context available; it does not prove the unit actually uses every pixel equally or learns a human-named concept.
15 | LEARN A SPATIAL HIERARCHY
| Stage | Possible useful representation | Important qualification |
|---|---|---|
| Earlier convolutional layers | Local contrasts, simple edges or textures | Filters are learned; not every filter is an edge detector |
| Intermediate layers | Combinations of local responses, shapes or motifs | Meaning depends on the task, architecture and training |
| Later representations | Broader combinations useful for category decisions | A unit need not correspond to one recognisable object part |
A spatial hierarchy means successive local computations combine information across increasing context while retaining or transforming spatial organisation. The classifier can use these representations to distinguish patterns.
“Edges → parts → objects” is a useful intuition, not a guaranteed literal label for each layer. Adaptive learning occurs because training changes the parameters to improve the objective, not because the programmer manually names each hierarchy level.
16 | SEE A COMPLETE EXAMPLE ARCHITECTURE
01 | INPUT
32×32×3 RGB image
02 | CONV + RELU
3×3 kernels, 8 filters, stride 1, padding 1 → 32×32×8
03 | MAX POOL
2×2, stride 2, no padding → 16×16×8
04 | CONV + RELU
3×3 kernels, 16 filters, stride 1, padding 1 → 16×16×16
05 | MAX POOL
2×2, stride 2 → 8×8×16
06 | CLASSIFIER
Flatten → 1024 values; dense layer of 32 units with ReLU; 3 class outputs with softmax
Follow the numbered stages. Convolution and activation create representations; pooling reduces spatial dimensions; the classification head uses the representation for the final task.
This is one valid teaching architecture, not a universal CNN recipe. Different networks can use global average pooling, different heads or other layer arrangements.
17 | FLATTENING AND FULLY CONNECTED LAYERS
Flattening rearranges a feature tensor into a vector without changing its values. An 8 × 8 × 16 tensor contains 1024 values. Flattening alone has no learned parameters and is not a pooling operation.
A fully connected or dense layer combines the vector through weights, biases and activation functions, as in the previous ANN lesson. It can learn how combinations of extracted features relate to the target categories.
Flattening removes the explicit grid organisation at that stage, although the ordered values still come from specific positions. CNNs need not all use flattening: a global pooling head can summarise spatial responses differently.
18 | CLASS OUTPUTS ARE SCORES, NOT CERTAINTY
For three mutually exclusive classes, an output layer can produce three logits, then softmax transforms them to nonnegative values summing to 1. A defined decision rule may choose the largest value.
Those values are model probabilities under its fitted assumptions, not proof of calibrated real-world correctness. A highly confident prediction can still be wrong, especially under changed image conditions.
Binary or multi-label tasks can require different output encodings and activations. A network that labels an entire image is not automatically locating objects or segmenting individual pixels; those tasks need suitable architectures and targets.
19 | TRAINING ADAPTS FILTERS THROUGH A LOSS
In supervised image classification, training examples pair images with target labels. A forward pass computes maps and outputs. A loss function measures prediction mismatch, such as cross-entropy for an appropriate class encoding.
Backpropagation computes how learned weights and biases influence the loss; an optimiser adjusts those parameters. Repeating this process lets filters and later layers adapt together to useful patterns.
The loss is not a sliding filter, activation or pooling rule. The activity’s fixed filters illustrate the forward operation only. A complete trained CNN must learn suitable parameters using data and an appropriate training procedure.
20 | ARCHITECTURE CHOICES CHANGE CAPACITY AND COST
| Choice | Possible effect | Trade-off |
|---|---|---|
| More layers | More stages combining context and representations | More computation and training difficulty; possible overfitting |
| Larger spatial kernels | More local input per response and usually more weights at fixed channel counts | More cost; not automatically better features |
| Larger stride | Fewer output positions | Reduced detail and changed sampling |
| More filters | More output channels and representational capacity | More activations, weights and computation |
| Activation choice | Changes nonlinear response and gradient behaviour | Can affect what is learnable and how training proceeds |
| Loss choice | Defines the prediction mismatch being optimised | A mismatch with targets or objective can train the wrong task |
Distinguish these choices from fitted parameters. Kernel dimensions and stride are configuration settings; the values inside the kernels are learned weights. Validate the entire pipeline rather than assuming deeper or larger is always better.
21 | A SMALL PARAMETER COUNT MAKES SHARING VISIBLE
In the first example convolution, each of 8 filters has 3×3×3 = 27 weights plus one bias. Total learned parameters = (27 + 1) × 8 = 224. These parameters are reused across all its spatial positions.
Its output still contains 32×32×8 = 8192 activation values for one image. The number of learned parameters and the amount of intermediate activation storage are different quantities.
The second convolution has (3×3×8 + 1)×16 = 1168 parameters. These counts assume standard convolution with one bias per output channel, no groups and dilation 1. Real training also stores additional working data.
22 | EVALUATE WHAT THE NETWORK ACTUALLY LEARNS
A CNN may exploit background, image borders or data-collection shortcuts instead of the intended object. Split related images appropriately, avoid duplicated examples across partitions and keep final test data outside architecture choices.
Use meaningful task metrics, representative images and checks under relevant lighting, scale or viewpoint changes. Appropriate augmentation can expose training to transformations, but it must preserve label meaning and be applied under an honest evaluation procedure.
Local filters and pooling provide useful structure, not guarantees of robustness, fairness or understanding. Inspect failure cases and resource requirements. A clear hierarchy diagram describes the mechanism; it is not evidence that a particular trained model performs reliably.
23 | CHECK THE CNN MECHANISM
Choose the main concept illustrated by each scenario. Keep learned parameters, forward computations and evaluation separate.
24 | DESCRIBE THE ADAPTIVE HIERARCHY
Connect the stages: the image is a spatial tensor; shared kernels compute local feature maps; activations introduce nonlinearity; pooling or strided operations can reduce spatial dimensions; later layers combine earlier responses over broader context; a task head produces predictions.
Explain adaptation: a loss compares predictions with targets, and training adjusts filter and later-layer parameters. This allows useful feature hierarchies to emerge from experience rather than being entirely hand-coded.
Include the settings: layer count, kernel size, stride and activation affect representation and cost, while the loss affects what training rewards. Then include a limit: useful spatial design does not remove the need for independent evaluation.
Further reading: convolution parameters and output structure and a complete convolutional image classifier.