A small grid of pixel intensities contains an edge, and the same kind of edge could appear in any part of the image. A dense layer gives every pixel its own weight for every unit, so what it learns about a pattern in one corner does not carry over to the same pattern in another corner. A convolutional layer instead applies one small set of weights at every position and records how strongly each patch matches. This lesson computes that operation by hand, derives the output size from stride and padding, follows the part of the image each unit can see, and counts the parameters of a compact image classifier.
Key terms
Convolution
Definition Let be a single-channel input with rows and columns, and let be a array of weights with and . With indices starting at 0, stride 1, no padding and no bias, the operation that deep learning libraries call a convolution produces
for and . In mathematical terms this is cross-correlation. The convolution of signal processing and analysis first flips the filter in both spatial directions, so on the same positions it uses in place of . When the weights are learned, both conventions describe the same family of layers, because training simply finds the flipped filter. A calculation by hand, or a filter copied from another source, has to follow the convention of the implementation that will use it.
In plain words Lay the filter over one patch, multiply the entries that sit on top of each other, and add the products. Then move the filter and do the same with the same weights. Each output number answers how strongly one patch matches the filter.
Example Take the constructed image of dimensionless intensities and the filter that adds the left column of a patch and subtracts the right column.
At the upper left position the left column of the patch sums to and the right column to , so . Flipping this filter in both directions gives exactly , so the flipped convention would report at the same position.
Filter and feature map
Definition A filter, also called a kernel, is the array of weights that a convolutional layer applies at every position, together with one bias . For an input with channels, such as the three color channels of an image, a filter has shape , and with stride 1 and no padding its output is
The two-dimensional array produced by one filter is a feature map. A layer with filters produces feature maps, which form the output channels, and it has parameters whatever the height and width of the input. An activation such as ReLU is usually applied to every entry afterward, and many texts also call the activated array a feature map.
In plain words A filter is a pattern detector of fixed size whose weights are learned, and its feature map is the grid of its scores across the image. A filter always reads every input channel at once, so its depth matches the input. The sign of a score carries meaning too, since a negative response to one pattern is a positive response to the opposite pattern.
Example Sliding the filter over all four legal positions of , with bias 0, gives the feature map
The negative entries mark patches whose right column is brighter than their left column. ReLU would set all four entries to 0, which does not make the filter useless. The filter gives the map with entries , , and , so a layer containing both filters keeps both orientations after ReLU.
Stride and padding
Definition The stride is the step, in input positions, between neighboring placements of the filter. The padding is the number of rows and columns of fixed values, usually zeros, added on each side of the input before filtering. The dilation, the spacing between neighboring filter entries, is 1 on this page, so a filter reads adjacent positions. Along one side of length , with filter size and , the number of output positions is
The formula is applied separately to height and width. The floor drops placements that would extend past the padded input, so with a stride above 1 some border rows or columns may never be read. With stride 1 and an odd , padding keeps the side length unchanged. The number of output channels equals the number of filters, and neither stride nor padding changes the parameter count.
In plain words Padding lets the filter be centered on pixels near the edge, and stride decides how many positions are skipped between placements. Padded values are part of the computation that someone chose, not an observed continuation of the image. The output size should be derived before it is flattened or passed to the next layer, not read off a diagram.
Example In the image above, , , and give positions per side, the map already found. A input with a filter, padding 1 and stride 2 gives positions per side. A side of length 10 with , and gives placements, starting at positions 0, 2, 4 and 6, so they cover positions 0 through 8 and the last position is never read.
Receptive field
Definition The receptive field of a unit is the set of input positions whose values can affect that unit’s value. For a stack of convolution and pooling layers with dilation 1, where layer has window size and stride , its side length follows from
starting from and . Here is the distance, in input pixels, between neighboring units of layer . For stacked layers with filters and stride 1 this gives . Near a border, part of the receptive field can lie on padding rather than on the image.
In plain words Each layer looks at a small window of the layer below, and a window of windows covers more of the original image. A stride or a pooling step spreads later units apart, so after it every extra layer widens the view faster. The receptive field says which pixels can matter. How much each one matters depends on the weights, and pixels near the center usually reach the unit through more paths than pixels at the edge.
Example One layer with stride 1 sees a patch. A second such layer combines a block of first-layer units, whose patches together span pixels per side, so it sees a patch, and a third sees . If the first layer has stride 2 instead, neighboring first-layer units are 2 pixels apart and the second layer sees pixels per side after only two layers.
Pooling
Definition A pooling layer replaces each window of a feature map by a fixed summary, computed separately in every channel. Max pooling keeps the largest value in the window, and average pooling keeps the arithmetic mean. A pooling layer has a window size and a stride, often a window with stride 2, its output size follows the same formula as a convolution, and it has no learned parameters. It leaves the number of channels unchanged. During backpropagation, max pooling passes the gradient only to the entry that attained the maximum, with ties broken by the implementation’s rule, and average pooling shares it equally among the window’s entries.
In plain words Pooling shrinks a map by keeping one summary per neighborhood. It lowers the resolution and makes the result insensitive to some small shifts inside a window. It does not make a network invariant to every shift, and it throws away where in the window a response occurred.
Example A window with responses 1, 4, 2 and 3 gives 4 under max pooling and 2.5 under average pooling. In one dimension, max pooling with window 2 and stride 2 sends the row to . Shifting the row one step left to gives again, but shifting it one step right to gives . A shift that crosses a window boundary changes the pooled output.
Convolutional neural network
Definition A convolutional neural network, or CNN, is a neural network in which some layers are convolutional, so that each unit is connected to a local window of the previous layer and the same filter weights are used at every position. A basic image classifier repeats convolution followed by an activation, reduces the spatial size with pooling or with strides, flattens the final maps into a vector, and ends with a dense layer that outputs one logit per class. Training usually minimizes the average cross-entropy on the training data with gradients from backpropagation, and because a filter weight is used at every position, its gradient adds the contributions of all positions,
shown here for one channel, stride 1 and no padding.
In plain words Local connections and shared weights build in the assumption that useful patterns are small and can appear anywhere in the image. That assumption is why a convolutional layer has far fewer parameters than a dense layer with the same numbers of inputs and outputs, and it helps only when it suits the task. A strong response from a filter is a fact about the model’s computation, not proof that the model sees an image the way a person does. A network can find shortcuts such as backgrounds, watermarks or the camera used, so evaluation needs data split by independent subjects or sources and a look at the mistakes.
Example Take an single-channel input and four filters with padding 0 and stride 1. They give four feature maps with parameters. ReLU keeps the shape, and max pooling with stride 2 gives four maps. Flattening gives 36 numbers, and a dense layer to two class logits adds parameters, for 114 in total. For a comparison of sharing, a convolutional layer with three input channels, six filters, padding 0 and stride 1 maps a input to outputs with parameters. A dense layer from the same 147 inputs to 150 outputs needs weights before its biases.
Check yourself
- Apply the filter to the input with the library convention, stride 1, no padding and bias 0. Then repeat the calculation with the filter flipped in both directions, and apply max pooling to the first result.
Show the answer
The output has positions per side. At the upper left the patch has entries 1, 0, 3 and 1, so the response is . The other three patches give , and in the same way, so the feature map has rows and .
Flipping in both directions gives rows and . Repeating the sums with that filter gives rows and , a different map, so a hand calculation has to state which convention it uses.
Max pooling over the whole map of the library convention keeps its largest entry, 6.
- An input of size , written height, width, channels, goes through a layer of 16 filters of size with padding 2, stride 1 and one bias per filter. Find the output shape and the number of parameters. A classmate counts parameters. What did they miss, and what changes if the stride is 2?
Show the answer
Each side gives positions, and there is one output channel per filter, so the output is .
Each filter reads all three input channels, so it has weights and one bias. The layer has parameters. The classmate counted a single channel per filter.
With stride 2 each side gives positions, so the output is . The parameter count stays 1,216, because the same filters are used and only the number of placements changes. For contrast, a dense layer from the 3,072 inputs to the 16,384 outputs of the stride 1 case would need weights.
- A network applies a convolution with stride 1, then max pooling with stride 2, then another convolution with stride 1, all with dilation 1. How large is the receptive field of a unit in the last layer? A classmate adds for every layer, as in a stack with stride 1, and gets . Why is that wrong?
Show the answer
Start from and . The first convolution gives with . The pooling layer gives , and its stride makes . The second convolution gives
so the receptive field is pixels, away from the borders.
The classmate’s sum treats every layer as if neighboring units were one pixel apart. After the pooling stride, neighboring pooled units are 2 pixels apart in the image, so each extra step of the last filter reaches 2 pixels further, not 1.