The Convolution Operation
Convolution slides a learnable kernel over an input, computing dot products that measure how strongly a local pattern is present at each position.
Discrete convolution
For a 2D input and a kernel of weights, the output at position (i, j) is the sum over the kernel window of input values times kernel weights. The kernel slides across every position, producing a feature map that lights up where the kernel's pattern matches the input. Strictly, deep learning frameworks compute cross-correlation, which omits the kernel flip of mathematical convolution; because the kernel is learned, the distinction does not affect what the network can represent.
Kernel, stride, padding
- Kernel size: the window dimensions, commonly 3x3 or 5x5.
- Stride: how many pixels the kernel moves between outputs; stride 2 halves resolution.
- Padding: extra border pixels so the output can keep the input size; 'same' padding preserves dimensions.
Output size
For an input of size N, kernel size K, padding P, and stride S, the output size is floor((N - K + 2P) / S) + 1. This formula lets you design a network's spatial dimensions layer by layer. Getting it right matters for architectures like U-Net where feature maps from different depths must align.
Channels and filters
A convolution over a multi-channel input uses a kernel with matching depth, so a 3x3 kernel over a 3-channel RGB image actually has 3x3x3 weights plus a bias. Each such filter produces one output channel. A layer with 64 filters produces 64 output channels, each a different learned feature map. The parameter count is kernel_h x kernel_w x in_channels x out_channels.
import numpy as np
def conv2d_valid(x, k):
H, W = x.shape; kh, kw = k.shape
out = np.zeros((H-kh+1, W-kw+1))
for i in range(out.shape[0]):
for j in range(out.shape[1]):
out[i, j] = np.sum(x[i:i+kh, j:j+kw] * k)
return out
Why it is efficient
Weight sharing means one small kernel is reused across the whole image, so a convolutional layer has orders of magnitude fewer parameters than a dense layer over the same input. On hardware, convolution maps to highly optimized matrix multiplications (via im2col or specialized kernels), which is why GPUs run CNNs so quickly. The combination of few parameters and fast execution made deep vision practical.
- Slides a kernel and computes local dot products.
- Stride and padding set output resolution.
- Each filter spans all input channels and yields one output channel.
- Weight sharing gives few parameters and fast execution.