What we want to understand
A color photograph that is 224 pixels wide and 224 high holds numbers. Suppose we wanted a first layer that looks at every one of them and produces, say, 1,000 outputs. Each output would need its own weight for each input number, which is weights. (A thousand outputs is only a round number for the arithmetic. The first layer of AlexNet, which we meet below, has outputs.)
There is a second thing wrong with that layer. A pattern in the top left corner and the same pattern in the bottom right corner would look unrelated to it, so it would have to learn the pattern once for each place it can appear.
A convolution removes both difficulties by two assumptions. First, a pattern is recognized from a small neighborhood, not from the whole picture. Second, the same detector is useful everywhere. Krizhevsky, Sutskever and Hinton (NeurIPS 2012) filter their input with 96 detectors of size , which is weights, plus 96 biases, for the whole first layer. That is about 4,300 times fewer weights than the dense layer. This lecture is about what those 34,848 numbers do.
One dimension first
We will start on a line, because everything important already happens there.
A word on counting first, once and for all. We number everything from 1, as linear algebra does: a table of numbers has entries with , a row of numbers has entries , and a picture has rows and columns numbered from 1. A position outside the picture, with an index below 1 or above , counts as the number 0.
Let be a row of numbers, the input. Let be a short row of numbers, the kernel; its entries are called weights. The output is a new row whose -th entry is
Read it aloud: lay the kernel on the input with its first entry above , multiply each weight by the input number beneath it, and add up. The sum runs over , the position inside the kernel, from 1 to . We can only place the kernel where it fits entirely on the input, and that is why stops at .
An example, with numbers small enough to do in your head. Take
Here and , so runs from 1 to 4.
Before reading on, try . The kernel sits above , so . The next position sits above and gives . Then and . So
Notice what this kernel did. Each output is a weighted sum of three neighbors, with the middle one counting double; divided by 4, the sum of the weights, it would be a weighted average. It smooths. A different kernel on the same input would detect something else, and that is the whole idea: the kernel is the question we ask at every position, and the output records the answer at each one.
for : lay the kernel on the input, multiply, add.
The same idea on a picture
Now let the input be a picture , a grid in which is the value at row and column , and let for any position outside the picture. Take the picture to be ; for a rectangle, work out below for the height and the width separately. Let the kernel be a grid with entries for . We allow two refinements that we will explain shortly: the kernel may move pixels at a time (the stride), and we may place rows and columns of zeros around the picture (the padding). The output is
where is a number called the bias, one per kernel, which you may take to be zero, and and number the rows and columns of the output, from 1 up to the output size , which we work out below. With and the formula is the plain one: lay the kernel on the picture with its top left corner at , multiply each weight by the pixel beneath it, and add. It is the same sum as on the line, with a second index. Each output value is one dot product between the kernel and a patch of the picture.
With and , the output at is the bias plus the patch of the picture with its top left corner at , multiplied weight by weight with the kernel and added.
In the figure you can change any weight of a kernel and any pixel of a small picture. Choose an output square, by clicking it or by selecting it and pressing Enter, and the patch it came from is marked. Say what you expect the output to be, the nine products added together plus the bias , then reveal the products and their sum. Pointing alone does not reveal anything, and choosing a different output square clears your earlier answer. The equation sits inside the figure, with the same symbols and ranges as the formula above: point to the picture, the kernel or the bias and the matching symbol in the equation lights up, and point to a term in the equation and its patch, its kernel or its bias lights up as a group.
Paint an image cell or change a kernel weight, predict an output, then inspect the products that produce it.
The parameters are hand-set.
The input is synthetic: An editable four-by-four image, initially a four-by-four vertical edge with values zero and one; image values and weights range from -1,000 to 1,000.
This toy leaves out: Learning, activation functions, multiple input channels and multiple output filters.
At real scale: Real layers combine channels and learn many filters.
The displayed outputs are from this toy.
Each output is a sum of products, with one bias added. Point to a term to find its part of the picture, or use the Tab key.
The input picture is , the kernel is , the bias is , the stride is , and the padding is . Values of outside the picture are zero.
. Here . The kernel indices and run from 1 to 3. The output indices and run from 1 to 2.
| Row 1 | 0 | 0 | 1 | 1 |
|---|---|---|---|---|
| Row 2 | 0 | 0 | 1 | 1 |
| Row 3 | 0 | 0 | 1 | 1 |
| Row 4 | 0 | 0 | 1 | 1 |
| Row 1 | -1 | 0 | 1 |
|---|---|---|---|
| Row 2 | -1 | 0 | 1 |
| Row 3 | -1 | 0 | 1 |
Fixed worked example: output row 1, column 1
| Input coordinate | Weight coordinate | Input | Weight | Product |
|---|---|---|---|---|
| 1, 1 | 1, 1 | 0 | -1 | 0 |
| 1, 2 | 1, 2 | 0 | 0 | 0 |
| 1, 3 | 1, 3 | 1 | 1 | 1 |
| 2, 1 | 2, 1 | 0 | -1 | 0 |
| 2, 2 | 2, 2 | 0 | 0 | 0 |
| 2, 3 | 2, 3 | 1 | 1 | 1 |
| 3, 1 | 3, 1 | 0 | -1 | 0 |
| 3, 2 | 3, 2 | 0 | 0 | 0 |
| 3, 3 | 3, 3 | 1 | 1 | 1 |
Sum of products: 3. Bias: 0. Output: 3.
Signed scale: blue is negative, red is positive, white is near zero. Table values use up to 12 significant figures.
| Row 1 | 3 | 3 |
|---|---|---|
| Row 2 | 3 | 3 |
Begin with the worked example above, then make your own prediction.
Sign predictions count values within 1e-10 Γ (1 + sum of absolute products + absolute bias) as zero. Small rounding differences do not change that near-zero answer.
Here is a question to answer before you move anything. Take the bias to be . Give a picture wide enough to have flat regions a dark left half, all zeros, and a bright right half, all nines, and give every row of the kernel the weights . (The small picture in the figure has no flat window at : every window there contains the edge, so for the flat response use a uniform picture.) What is the output where the window straddles the edge, and what is it in the flat regions? Take a moment, then choose below.
Work out one row first, then count the rows.
Check the edge prediction
Three rows, each contributing 9
Each of the three rows contributes (-1)Β·0 + 0Β·0 + 1Β·9 = 9 when the window lies across the edge, so with b = 0 the output is 3 Γ 9 = 27. In the flat regions every row sees equal numbers, and the weights in a row add to zero, so the output is 0. With a bias b the outputs would be b + 27 and b.
This kernel is an edge detector. With it gives zero on flat regions, a positive number where the values rise from left to right, and a negative number where they fall.
Zero on flat regions, positive where the values rise from left to right, negative where they fall.
A matrix in disguise
Go back to the line. Write the input as a column of six numbers and look for a matrix with . Each output is a dot product of the kernel with a window, so row of must be the weights, padded with zeros so that they line up with the window:
Each row is the row above it moved one place to the right. The convolution itself, with the bias taken as zero, is a linear map, and a very special one: a matrix with the same few numbers repeated along its diagonals. That is the locality, since most of each row is zero, and the weight sharing, since the same appears in every row. (The same view, a layer as a matrix, is how Chapter 1 of the book treats a model call.) The entries of this matrix are determined by three numbers.
Convolution, and the flip
Deep learning calls the operation above "convolution". Strictly it is cross-correlation. Take and for this section. The mathematician's convolution of with is
Substitute and . The sum becomes the cross-correlation of with the flipped kernel , read at the window whose corner is . So the two differ by turning the kernel half a revolution (a flip in both directions) and by a shift of the output index.
Why does deep learning call it convolution, then? Here is a reason, our own. If a layer can represent a kernel , it can equally represent its flip , because the flip is only another grid of weights. So which of the two operations a network uses changes the labels on the learned weights, and not what the layer can represent.
In the figure there is a switch that shows the flipped result on the same grid, without the shift of the index, so you are looking at the cross-correlation with the flipped kernel. A kernel that changes sign when flipped, , shows it: the kernel with in every row is one, and, with , the edge response goes from to . A kernel that is the same when flipped, such as the rows , , , gives the same output either way.
What happens when the picture moves
Take and , and shift the picture columns to the left, writing . The output of the shifted picture at is
The output of the shifted picture is the original output, shifted by the same amount. We say the operation is equivariant to shifts: the output moves with the input. It is not invariant, which would mean the output stays where it was. With a stride the statement holds only for shifts that are multiples of , and then the output moves by .
Three properties, then, and we can now name them. Each output depends on only input values: locality. One kernel is used at every position, so the number of weights does not depend on the size of the picture: weight sharing. And shifting the picture shifts the output: equivariance.
Locality: each output depends on only input values. Weight sharing: one kernel is used at every position. Equivariance: shifting the picture shifts the output.
The borders spoil equivariance, and in a specific way. A shift pushes content out on one side and brings zeros in on the other. With no padding, the last outputs of the shifted picture come from windows that touch the entering zeros and have no counterpart in the original output. With zero padding , the entering zeros coincide with the padding zeros the original already sees, but the pixels that left are replaced by padding, so outputs within columns of that edge can differ (they coincide when the pixels that left are zero). And, as without padding, the last outputs of the shifted picture have no counterpart in the original output.
How big is the output?
Pad the input with zeros on each side, so that its length is , where is the original length. With stride the -th window starts at position and covers positions up to . It fits when , that is when . Counting gives
This is the in the index form of the sum above, so and run from 1 to . It needs ; otherwise there is no valid position and no output.
Output length: , defined when . The floor drops any leftover input that no window reaches.
Try it on our line. Take and again, with padding and stride . The padded input is , of length 8, and . The windows start at positions 1, 3 and 5 and read , and , giving , and . So . The last number of the padded input is never read: the floor in the formula is that leftover.
Two cases worth remembering. With and odd , taking gives , so the output keeps the input's size ("same" padding). With no padding and stride 4, an -wide kernel on a -wide input gives , and on a -wide input gives .
In the first figure the picture is and the kernel ; set and there and watch the window step along, saying what you expect first. For larger sizes, work from the formula. With , , , the formula gives ; with it gives . When there is no valid position, which is the condition above.
A word on 224 and 227, since you will meet both. The AlexNet paper states a input, kernels and stride 4, and does not state the first layer's padding. With no padding that gives . Padding of 2 or 3 gives ( and ), and so does a input with none. The first layer is usually quoted as ; the formula shows which inputs and paddings give that number, and the paper alone does not say which was used.
Many channels
A color picture has three channels. A kernel for it has depth three: its shape is , and the sum runs over the channels too,
A layer with such kernels produces output maps, and has numbers to learn, counting one bias per kernel. For AlexNet's first layer that is . The kernel has one plane for each of the red, green and blue planes of the picture, and the three results add into one output map.
numbers to learn; for the first layer of the network above, .
What trained kernels look like
Figure 3 of the AlexNet paper shows the 96 kernels the first layer learned. The authors write that the network "has learned a variety of frequency- and orientation-selective kernels, as well as various colored blobs" (Section 6.1).
What is still open
How necessary the built-in assumption is. Dosovitskiy et al. (ICLR 2021) report that a transformer applied to image patches "attains excellent results compared to state-of-the-art convolutional networks" when pre-trained on large datasets, and state that "this reliance on CNNs is not necessary". That leaves open how much locality and sharing matter when data is plentiful, and when it is not. One remark on the comparison: cutting a picture into non-overlapping patches and applying one shared linear map to each is itself a convolution, with a kernel and stride . So the transformer's first layer is still a shared linear map over local patches.
Now you try
Say your answer before you look at the answers at the end.
- A picture, a kernel, , . What is ? Show your working from the formula.
- A kernel on a color picture, 64 kernels. How many numbers to learn?
- Edit the kernel in the first figure to respond to horizontal edges. What sign should the response have where dark lies above bright?
- Shift the picture one pixel and compare the outputs at stride 1 and then at stride 2. What do you expect away from the borders?
- On the line, with and , what is ? Write the matrix as well.
Answers
Answers to the five exercises
- .
- .
- With , the rows , , respond with on a window whose top row is 0 and bottom row is 9, and with the other way round.
- At stride 1 the output shifts by one. At stride 2 a one-pixel shift does not give a shifted copy of the output, while a two-pixel shift moves it by one.
- Every output is , and has in each row, moved one place to the right in each row below.
Sources
More guides are listed in the articles index. Krizhevsky, Sutskever and Hinton, "ImageNet Classification with Deep Convolutional Neural Networks", NeurIPS 2012: read in full for the first layer (224 by 224 by 3 input, 96 kernels of size 11 by 11 by 3, stride 4), Figure 3 and Section 6.1. Dosovitskiy et al., "An Image is Worth 16x16 Words", ICLR 2021 (arXiv text): abstract and introduction. Everything else here is derived on the page; the numbers can be checked with pencil and paper.
A convolution is one linear map with shared weights. Chapter 1 derives what a whole model computes and checks it against a real one.

