Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Convolutional Layer Mechanics

Introduction

In our last lesson, we established the theoretical foundation of Convolutional Neural Networks, focusing on how the convolution operation and parameter sharing give rise to translation equivariance and enable the learning of spatial feature hierarchies. We noted that the convolution operation itself, depending on the filter size, alters the spatial dimensions of the feature maps.

Today, we will build directly on that knowledge by exploring the mechanisms that give us explicit control over these spatial dimensions. This lesson focuses on three fundamental components of a CNN architecture: padding, stride, and pooling layers. You will learn not only their mechanical function but also their strategic purpose in designing effective and efficient deep learning models for vision. Understanding these tools is essential for controlling information flow, managing computational complexity, and building a degree of invariance into the network.

Controlling Spatial Dimensions: Padding and Stride

In the previous lesson, you saw that convolving an input with a filter (e.g., a 3x3 filter on a 32x32 image) produces a slightly smaller output (e.g., 30x30). If we stack many such layers, the feature maps would shrink rapidly, causing two problems:

  1. The spatial dimensions would diminish, possibly before we have extracted sufficiently high-level features.
  2. Information at the borders of the original image would be progressively "washed away," as those pixels participate in far fewer convolutions than pixels at the center.

To manage this, we use two key hyperparameters in our convolutional layers: padding and stride.

Padding: Preserving Boundaries and Dimensions

Padding is the process of adding extra pixels, typically with a value of zero, around the borders of an input volume before the convolution.

The primary motivations for padding are:

  • Preserving Spatial Size: By adding the right amount of padding, we can ensure the output feature map has the same width and height as the input. This is often called "same" padding and is a very common strategy. It simplifies network design, as we don't need to constantly track shrinking dimensions.
  • Retaining Border Information: Padding allows the filter's receptive field to be centered on border pixels, ensuring they are treated more equally to interior pixels.

The following resource provides a clear introduction to padding and the formula for the resulting output size.

6.3. Padding and Stride

This section from the "Dive into Deep Learning" book explains the motivation for padding and provides the formula for the output shape when padding is applied.

Please read the section on Padding. Pay close attention to the formula for the output shape and the common practice of choosing a padding amount that preserves the input's spatial dimensions, especially with odd-sized kernels.

As the reading explains, if an input has width , the filter has width , the padding is , and the stride is , the output width is given by:

A common rule of thumb for "same" padding with a stride of is to set the padding . For a 3x3 filter, we use . For a 5x5 filter, we use .

Stride: Downsampling and Efficiency

Stride defines the step size the filter takes as it slides across the input. In all our examples so far, we have implicitly used a stride of 1. By increasing the stride, say to 2, the filter skips every other pixel.

The primary motivations for using a stride greater than one are:

  • Downsampling: It directly reduces the spatial dimensions of the output feature map, acting as a form of subsampling.
  • Computational Efficiency: A larger stride means fewer convolution operations are performed, reducing the overall computational load.
  • Increasing Receptive Field: Downsampling causes the effective receptive field of subsequent layers to grow more quickly with respect to the original input.

This interactive lecture segment provides an excellent walkthrough of how stride works and its effect on output size.

Lecture 5 | Convolutional Neural Networks

This clip from a Stanford lecture on CNNs visually demonstrates the effect of different stride values on the output dimension of a convolution.

Please watch from this section. The lecturer clearly shows how a stride of 1, 2, and 3 impacts the output size on a 7x7 input with a 3x3 filter, leading to the general formula for output size.

The image below visualizes four common scenarios involving different padding and stride settings.

Four examples of a 3x3 filter applied to a 4x4 input. From left to right: (1) No padding and stride 1 shrinks the output. (2) "Same" padding (P=1) and stride 1 preserves the output size. (3) Padding 1 and stride 2 downsamples the output. (4) Padding 2 and stride 1 increases the output size.

For a deeper dive into the technical details and a classic real-world example, the CS231n notes are an invaluable resource.

https://cs231n.github.io/convolutional-networks/

This resource offers a detailed breakdown of the hyperparameters that control a convolutional layer's output, including stride and padding.

Focus on the subsection Spatial arrangement. This part formally defines stride and padding and gives the general formula. Then, examine the real-world example of the AlexNet architecture. The note about the discrepancy in the input size (227 vs. 224) is a famous detail that highlights the importance of getting these calculations exactly right.

Pooling Layers: Summarizing and Invariance

While a strided convolution effectively downsamples the feature maps, a more explicit and historically dominant method is the pooling layer. Pooling is a separate, non-parametric layer typically inserted between successive convolutional layers.

Its function is to progressively reduce the spatial size of the representation. This serves several purposes:

  1. It further reduces the number of parameters and computational cost in the network.
  2. It helps to control overfitting by providing a summarized, more abstract representation.
  3. Critically, it introduces a degree of translation invariance.

Recall that convolution is translation equivariant: shifting the input shifts the output. A pooling operation, by summarizing a local neighborhood, can make the representation robust to small shifts of a feature within that neighborhood. For example, with 2x2 max pooling, as long as the most activated neuron remains within that 2x2 window, the output will be identical, regardless of its precise position.

The most common type of pooling is max pooling. It operates independently on each depth slice of the input volume. A window (e.g., 2x2) slides over the feature map, and at each position, only the maximum value within the window is passed to the output.

An illustration of a pooling operation. It takes an input volume (e.g., 224x224x64) and spatially downsamples it (e.g., to 112x112x64) by applying an operation like 'max' over local regions. The depth of the volume remains unchanged.

The following resource provides a thorough explanation of pooling layers.

https://cs231n.github.io/convolutional-networks/

This section of the CS231n notes describes the function, hyperparameters, and common types of pooling, including max pooling.

Read the section on the Pooling Layer. Pay attention to the distinction between pooling and convolution: pooling operates independently on each depth slice and typically has no learnable parameters. Also, note the common settings, such as a 2x2 filter with a stride of 2. Finally, read the subsection Getting rid of pooling, which discusses the modern trend of sometimes using strided convolutions as an alternative.

To see pooling in action, this short video offers a clear, animated explanation.

CNN Explained Visually: Padding, Stride, Pooling, Receptive Fields, Dilation & Layer Architecture

This animated video from ByteQuest explains pooling layers visually and concisely.

Watch from this segment on the pooling layer. It clearly demonstrates how both max pooling and average pooling work and states their purpose.

Architectural Patterns

With these components, we can define a standard "block" in a typical CNN architecture: CONV -> RELU -> POOL. A common and effective design pattern is to use the layers for distinct purposes:

  • Convolutional Layers: Use small filters (e.g., 3x3), a stride of 1, and "same" padding. Their role is to perform feature extraction, transforming the input depth-wise while preserving the spatial dimensions.
  • Pooling Layers: Use a 2x2 filter with a stride of 2. Their role is exclusively to downsample the spatial dimensions, typically halving them.

This separation of concerns—feature learning in CONV layers, downsampling in POOL layers—makes designing and debugging architectures much more straightforward.

https://cs231n.github.io/convolutional-networks/

This final reading summarizes the common rules of thumb for sizing the layers in a ConvNet.

Please read the section Layer Sizing Patterns. It provides practical advice that consolidates the functions of padding, stride, and pooling into a coherent design strategy.

Conclusion

In this lesson, we have moved beyond the core convolution operation to understand the tools that shape the flow of information through a CNN. These hyperparameters and layers are not mere implementation details; they are fundamental design choices with significant theoretical and practical implications.

Here are your key takeaways:

  • Padding adds a border of pixels to an input volume, primarily to preserve spatial dimensions across convolutional layers and ensure features at the image boundary are fully processed.
  • Stride is the step size of the filter. A stride greater than 1 is a method of downsampling, reducing both the feature map size and the computational cost.
  • Pooling Layers, most commonly implementing max pooling, serve as dedicated downsampling stages. They reduce dimensionality, control overfitting, and introduce a small degree of local translation invariance, a crucial property for robust recognition.
  • A common architectural pattern involves using padded, stride-1 convolutions for feature learning and stride-2 pooling layers for spatial downsampling.

In our next lesson, we will pivot from architectures designed for spatial data to those designed for sequential data. You will be introduced to the Recurrent Neural Network (RNN), an architecture that processes information through time, opening the door to tasks in natural language processing, time-series analysis, and more.

Can't find a good explanation? Sign up and we'll make it for you

Sign up