The short answer
A convolutional layer takes a grid of numbers, such as an image, and slides a set of small learned filters across it. At each position, a filter multiplies its weights by the values underneath and adds the results together. The outputs form feature maps that show where each pattern appears. Stacking several of these layers lets a network build from simple patterns, such as edges, toward more complex ones, such as parts of objects.
What goes into a convolutional layer
The most common input is an image. A grayscale image has one channel, and a color image has three: red, green, and blue. Layers after the first take in the feature maps produced by the layer before them, so their inputs are stacks of grids too.
Convolutional layers work best on data with a grid-like structure, where nearby values are related. Images are the classic example. The same idea is also used on audio spectrograms and on some sensor and time-series data.
Filters, also called kernels
A filter, or kernel, is a small grid of weights, often 3×3 or 5×5. In a trained network the weights are learned from data rather than written by hand.
To see the arithmetic, start with a hand-made 3×3 filter that responds to vertical edges:
-1 0 1
-2 0 2
-1 0 1
Now apply it to a small image with 3 rows and 4 columns. The left three columns are dark (0), and the rightmost column is bright (9):
0 0 0 9
0 0 0 9
0 0 0 9
The filter can sit in two positions across this image, so the output has one row and two columns.
For the first position, the filter covers the first three columns. They are all 0, so the output is 0.
For the second position, the filter covers columns two through four. Each row of that window reads 0, 0, 9. The top row contributes 9, the middle row contributes 18 because its weights are doubled, and the bottom row contributes 9. The total is 9 + 18 + 9 = 36.
So the output is:
0 36
The filter responds strongly where the dark-to-bright change happens and stays at zero where the image is flat. That is what this filter was designed to detect. In a trained network the weights are learned, but early layers often end up with filters that respond to edges and color changes, much like this example.
Output size, padding, and stride
The size of the output depends on the input size, the filter size, and two settings you choose. Padding is the number of border values added around the input, usually zeros. Stride is how many positions the filter moves at each step. For one dimension:
output size = (input size − filter size + 2 × padding) ÷ stride + 1
Round any fraction down. Check this against the example: a width of 4, a filter width of 3, no padding, and a stride of 1 gives (4 − 3 + 0) ÷ 1 + 1 = 2.
A 3×3 filter with stride 1 and padding 1 keeps the output the same size as the input. A stride of 2 skips every other position, which roughly halves the width and height.
Feature maps and channels
Each filter in a layer produces one feature map. A layer with 64 filters outputs 64 feature maps, stacked along a dimension called channels. A feature map has high values where its filter’s pattern appears and lower values elsewhere.
A filter also looks across all of its input channels. A 3×3 filter applied to an RGB image is really a 3×3×3 block of weights, with one 3×3 slice for each color channel. The three slices are computed and added together to give one number per position.
Why weight sharing matters
The same filter is applied at every position in the image. This is called weight sharing, and it keeps the model small.
Count the learnable parameters in one convolutional layer. Each filter has (filter height × filter width × input channels) weights, plus one bias, and the layer has one filter per output channel. For a layer that reads an RGB image and has 64 filters, each with a 3×3 window:
64 × (3 × 3 × 3 + 1) = 64 × 28 = 1,792
Compare that with a fully connected layer that connects a 224 × 224 RGB image, which has 150,528 values, to just 1,000 neurons. That one layer would need about 150 million weights.
Weight sharing also helps with position. If an object moves within an image, its feature map shifts in the same way. Researchers call this translation equivariance. It holds only approximately, but it is one reason why convolutional neural networks are preferred for computer vision applications.
Activation functions after the filter
The filter output is usually passed through a nonlinear function, most often ReLU, which keeps positive values and sets negative values to zero. This step matters. Without a nonlinear step, a stack of convolutional layers would collapse into a single linear operation, no matter how many layers you add.
Pooling
Many networks add a pooling layer after one or more convolutional layers. Max pooling with a 2×2 window and a stride of 2 keeps the largest value in each 2×2 block. That halves the width and height of each feature map, reduces the work for later layers, and makes the output a little less sensitive to small shifts. Some modern designs use strided convolutions instead of pooling.
Stacking layers: from edges to objects
Each convolutional layer looks at a small patch of its input, but later layers see more. The region of the original image that influences one output value is called its receptive field. Two stacked 3×3 layers with stride 1 give a 5×5 receptive field, and three give a 7×7 field.
Studies that visualize what trained networks respond to, such as the work by Zeiler and Fergus in 2014, suggest a common pattern. Early layers tend to respond to edges and colors, middle layers to textures and simple shapes, and deeper layers to larger parts and objects. Stacking many layers like this is what makes a network deep.
Learning the filters
Filters are learned during training. The network makes a prediction, a loss function measures how wrong it was, and backpropagation with gradient descent nudges every weight, including the filter weights, to reduce that error. Over many examples, the filters settle on patterns that help with the task.
Where convolutional layers are used
- Image classification. LeNet, developed by Yann LeCun and colleagues in the 1990s, read handwritten digits with convolutional layers. AlexNet, which won the ImageNet image recognition competition in 2012, helped start the modern wave of deep convolutional networks.
- Object detection and segmentation. Many detectors, including YOLO-style models, use convolutional backbones to pull features out of images.
- Audio and signals. One-dimensional convolutions scan along time in audio or sensor readings.
- Newer alternatives. Vision Transformers use attention over image patches instead of filters. A common question is which is better, CNN or ViT? The answer depends on how much data you have, how much compute you can spend, and the task, so neither is a universal winner.
For ordered data such as speech, recurrent networks take a different approach, which is covered in types of recurrent neural network in deep learning.
A short PyTorch example
Frameworks such as PyTorch build a convolutional layer in one line. The images below are random values, used only to show the shapes:
import torch
import torch.nn as nn
conv = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1)
images = torch.randn(8, 3, 32, 32) # 8 RGB images, each 32 x 32 pixels
features = conv(images)
print(features.shape) # torch.Size([8, 64, 32, 32])
The padding keeps the 32 × 32 size, and the layer turns 3 color channels into 64 feature maps. The layer has 1,792 learnable parameters, matching the count above.
Key terms
- Filter (kernel): a small grid of learned weights that slides across the input.
- Feature map: the output of one filter.
- Channel: one slice of a stacked input or output, such as a color channel or a feature map.
- Stride: how far the filter moves at each step.
- Padding: extra border values added around the input.
- Pooling: a step that shrinks feature maps, often by keeping the maximum value in each block.
- Receptive field: the part of the original input that affects one output value.
Putting it together
A convolutional layer runs learned filters across its input and produces feature maps, one per filter, that show where each pattern appears. Weight sharing keeps the number of parameters manageable, and padding, stride, and pooling control the size of the output. Stacking layers lets a network build from simple features to complex ones, which is why this design is so widely used for images.