Convolution and Weight Sharing
A convolution slides one small set of weights along the input and takes a dot product at every position, so the same pattern detector is used everywhere. That makes it cheap (a few weights instead of millions) and good at finding a feature wherever it shows up.
What the symbols mean
- Output
One output number: the kernel's dot product with the window starting at position i.
- Kernel weight
One of the k shared weights; the same ones are used at every position.
- Input
The input values under the window.
- Kernel size
How many weights the kernel has (its width).
What the symbols mean
- Input size
The input length (or height, or width).
- Padding
Zeros added to each end of the input.
- Stride
How far the window jumps each time.
What the symbols mean
- Input channels
How many layers the input has (3 for a color image: red, green, blue).
- Output channels
How many different kernels (filters) the layer learns; each also gets one bias.
Explanation
A convolution is simply a small pattern of weights, called a kernel or filter, that slides along the input and takes a dot product at every position. The word comes from mathematics, where convolving means sliding one function across another and adding up the products. In , the pieces are (the input, like a row of pixel brightnesses), (the kernel, only numbers long) and (the output at position : one dot product between the kernel and the window of input under it). Slide, multiply, add, repeat. That's all a convolution layer does.
A pattern detector. Remember from Vectors, Embeddings and Similarity that a dot product is big when two vectors point the same way. So each output measures how much the window at position looks like the kernel. Slide a kernel shaped like an edge along an image and the output lights up wherever there's an edge. A layer with 64 kernels asks 64 such questions at every position. Deeper layers run kernels over the outputs of earlier ones, so they find patterns made of patterns: edges, then corners, then eyes, then faces.
Weight sharing. Here's the big idea. A dense layer gives every input-output pair its own weight. A convolution uses the same weights at every position. That's a deliberate assumption: an edge is an edge whether it's in the top-left corner or the middle, so a detector learned in one place should work everywhere. Two payoffs follow. The parameter count doesn't depend on the input size at all, and shifting the input just shifts the output (the layer is translation equivariant). A convolution is still a linear map, so it has a matrix (see Linear Transformations), but a very special one: every row holds the same weights, shifted over one step, with zeros everywhere else.
The output size, derived. Pad the input with zeros on each end, so it has length . The window of length can start at position , then , then , and so on, as long as it still fits: its start can be at most . How many multiples of are there from up to ? Divide and round down to get the number of jumps, then add for the starting position:
With stride 1 and ("same" padding), the output has the same size as the input. For an image, the formula applies to height and width separately.
Channels and parameters. A color image has 3 channels, so a kernel is really : it looks at all three colors at once. A layer with kernels, each , plus one bias each, has parameters. Compare a dense layer from a image to an output of the same height and width with 16 channels: about 50 million weights, against 1216 for a convolution doing the same job.
Edges are derivatives. The kernel computes , the change in brightness across a pixel. Divide by the spacing and that's the central difference that approximates a derivative (see The Definition of the Derivative). Where the brightness jumps, the derivative is large: that's an edge. The 2D Sobel kernels do this horizontally and vertically, with a little smoothing.
Stacking grows the view. One layer sees a patch. Put another layer on top and each of its outputs sees a patch of outputs, each seeing pixels, which together cover pixels. Each extra layer adds , so layers see pixels across. Three layers see as much as one , with fewer weights.
A common mistake. It's tempting to take the textbook convolution formula, , at face value. That one flips the kernel before sliding it. Neural-network libraries don't flip: what they call convolution is really cross-correlation, the formula at the top of this lesson. Since the kernels are learned, the network doesn't care (it just learns the flipped kernel), but if you check a library's output by hand with the flipped formula, your answer comes out mirrored.
There's an animation further down that slides an edge-detecting kernel along a signal.
See it
Worked examples
Convolve the signal with the kernel (no padding, stride 1).
- 1
First, how many outputs? , , , , so .
A color image (3 channels) goes through a layer of 16 filters, each , with padding 2 and stride 1. Find the output shape and the parameter count, and compare with a dense layer producing the same output.
- 1
Height: . Width is the same, so the padding kept the size.
Practice
Type your answer and check it. Solve them all to complete the lesson.
A image goes through a convolution with no padding and stride 1. What is the output height?
A image goes through a convolution with padding 3 and stride 2. What is the output height?
How many parameters (weights and biases) are in a convolution layer with 64 input channels and 128 output channels?
Convolve with the kernel (no padding, stride 1). Enter the outputs in order.
Apply the Sobel kernel to the image patch : multiply matching entries and add.
A security camera sends grayscale frames through a convolution with stride 2 and padding 1. Enter the output height and width.
The kernel applied to samples of spaced apart gives . For sampled with , what does it output at ?
Three convolution layers (stride 1) are stacked. How large a patch of the original image does one output of the third layer depend on?
The hardest one here. Get it, and you've got this lesson.
A feature map can go through either (A) one convolution, 32 channels in and 32 out, padding 3, or (B) three stacked convolutions, each 32 channels in and 32 out, padding 1 (all stride 1, no biases). Show that both give a output whose entries each see a patch of the input, then find how many fewer weights B uses than A.
Where it's used
Every photo you take on a phone passes through convolution layers: for focusing on faces, for night-mode denoising and for search by content. Medical imaging systems flag tumors in scans with them, factories inspect parts on conveyor belts with them, and 1D convolutions over audio waveforms power wake-word detection ("hey, device") on small chips that can only afford a few thousand weights.