Skip to content
Foundations2026-11-0614 min read

How a Convolution Works

A convolution is a linear map with a few numbers repeated along its diagonals. Derive it on a line, then a picture, and see what a first layer learned.

Key takeaways

  • A convolution, with its bias set to zero, is a linear map: a matrix with the same few numbers repeated along its diagonals, which is locality and weight sharing at once. With a bias, the layer is affine.
  • The output of a shifted picture is the shifted output, so a convolution is equivariant to shifts, not invariant.
  • The output size is the input size plus twice the padding, minus the kernel size, divided by the stride and rounded down, plus one.

Rod Rivera

Author

How a Convolution Works

Rod's note: read with a pencil; the margins are for you.

What we want to understand

A color photograph that is 224 pixels wide and 224 high holds 224Γ—224Γ—3=150,528{}224 \times 224 \times 3 = 150{,}528 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 150,528Γ—1,000=150,528,000{}150{,}528 \times 1{,}000 = 150{,}528{,}000 weights. (A thousand outputs is only a round number for the arithmetic. The first layer of AlexNet, which we meet below, has 55Γ—55Γ—96=290,400{}55 \times 55 \times 96 = 290{,}400 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 11Γ—11Γ—3{}11 \times 11 \times 3, which is 96Γ—11Γ—11Γ—3=34,848{}96 \times 11 \times 11 \times 3 = 34{,}848 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 aija_{ij} with i=1,…,ni = 1, \dots, n, a row of NN numbers has entries 1,…,N{}1, \dots, N, and a picture has rows and columns numbered from 1. A position outside the picture, with an index below 1 or above NN, counts as the number 0.

Let x=(x1,x2,…,xN)x = (x_1, x_2, \dots, x_N) be a row of NN numbers, the input. Let w=(w1,w2,…,wk)w = (w_1, w_2, \dots, w_k) be a short row of kk numbers, the kernel; its entries are called weights. The output is a new row yy whose jj-th entry is

yjβ€…β€Š=β€…β€Šβˆ‘m=1kwm xj+mβˆ’1,j=1,2,…,Nβˆ’k+1.y_j \;=\; \sum_{m=1}^{k} w_m \, x_{j+m-1}, \qquad j = 1, 2, \dots, N-k+1.

Read it aloud: lay the kernel on the input with its first entry above xjx_j, multiply each weight by the input number beneath it, and add up. The sum runs over mm, the position inside the kernel, from 1 to kk. We can only place the kernel where it fits entirely on the input, and that is why jj stops at Nβˆ’k+1N-k+1.

An example, with numbers small enough to do in your head. Take

x=(2, 0, 1, 3, 1, 0),w=(1, 2, 1).x = (2,\,0,\,1,\,3,\,1,\,0), \qquad w = (1,\,2,\,1).

Here N=6N = 6 and k=3k = 3, so jj runs from 1 to 4.

Before reading on, try y1y_1. The kernel sits above (2,0,1)(2, 0, 1), so y1=1β‹…2+2β‹…0+1β‹…1=3y_1 = 1\cdot 2 + 2 \cdot 0 + 1 \cdot 1 = 3. The next position sits above (0,1,3)(0, 1, 3) and gives y2=0+2+3=5y_2 = 0 + 2 + 3 = 5. Then y3=1+6+1=8y_3 = 1 + 6 + 1 = 8 and y4=3+2+0=5y_4 = 3 + 2 + 0 = 5. So

y=(3, 5, 8, 5).y = (3,\,5,\,8,\,5).

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.

The one-dimensional rule

yj=βˆ‘m=1kwmxj+mβˆ’1y_j = \sum_{m=1}^{k} w_m x_{j+m-1} for j=1,…,Nβˆ’k+1j = 1, \dots, N-k+1: lay the kernel on the input, multiply, add.

The same idea on a picture

Now let the input be a picture xx, a grid in which xrcx_{rc} is the value at row rr and column cc, and let xrc=0x_{rc} = 0 for any position outside the picture. Take the picture to be NΓ—NN \times N; for a rectangle, work out oo below for the height and the width separately. Let the kernel ww be a kΓ—kk \times k grid with entries wmnw_{mn} for m,n=1,…,km, n = 1, \dots, k. We allow two refinements that we will explain shortly: the kernel may move ss pixels at a time (the stride), and we may place pp rows and columns of zeros around the picture (the padding). The output is

yijβ€…β€Š=β€…β€Šbβ€…β€Š+β€…β€Šβˆ‘m=1kβˆ‘n=1kwmnβ€…β€Šx s(iβˆ’1)+mβˆ’p,β€…β€Šs(jβˆ’1)+nβˆ’p,y_{ij} \;=\; b \;+\; \sum_{m=1}^{k} \sum_{n=1}^{k} w_{mn}\; x_{\,s(i-1)+m-p,\; s(j-1)+n-p},

where bb is a number called the bias, one per kernel, which you may take to be zero, and ii and jj number the rows and columns of the output, from 1 up to the output size oo, which we work out below. With s=1s = 1 and p=0p = 0 the formula is the plain one: lay the kernel on the picture with its top left corner at (i,j)(i, j), 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 kΓ—kk \times k patch of the picture.

The rule on a picture

With s=1s = 1 and p=0p = 0, the output at (i,j)(i, j) is the bias plus the kΓ—kk \times k patch of the picture with its top left corner at (i,j)(i, j), multiplied weight by weight with the kernel and added.

In the figure you can change any weight of a 3Γ—3{}3 \times 3 kernel and any pixel of a small 4Γ—4{}4 \times 4 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 bb, 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.

Inspect a kernel

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 x, the kernel is w, the bias is b, the stride is s, and the padding is p. Values of x outside the picture are zero.

o=⌊N+2pβˆ’ksβŒ‹+1. Here N=4,k=3o=2. The kernel indices m and n run from 1 to 3. The output indices i and j run from 1 to 2.

x1,1=0w1,1=-1b=0
0 = dark1 = light
Initial input image values
Row 10011
Row 20011
Row 30011
Row 40011
Initial kernel weights
Row 1-101
Row 2-101
Row 3-101
Fixed worked example: output row 1, column 1
Selected patch products
Input coordinateWeight coordinateInputWeightProduct
1, 11, 10-10
1, 21, 2000
1, 31, 3111
2, 12, 10-10
2, 22, 2000
2, 32, 3111
3, 13, 10-10
3, 23, 2000
3, 33, 3111

Sum of products: 3. Bias: 0. Output: 3.

3333

Signed scale: blue is negative, red is positive, white is near zero. Table values use up to 12 significant figures.

Initial output map values
Row 133
Row 233
Choose an output
Edit the image

Choose a paint value, then click a cell or focus it and press Enter.

Edit the kernel

The slider covers weights from βˆ’2 to 2. The numeric field also accepts larger values.

With zero bias, the vertical and horizontal edge presets give a positive response where image values increase to the right and downward, respectively. Flipping the kernel reverses that sign. Bias adds to every output.

Window geometry
Predict the selected output

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 b=0b = 0. 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 (βˆ’1,0,1)(-1, 0, 1). (The small 4Γ—4{}4 \times 4 picture in the figure has no flat window at p=0p = 0: every 3Γ—3{}3 \times 3 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.

Quick check β€” Take the bias to be b = 0. A wide picture has a dark left half, all zeros, and a bright right half, all nines. Every row of the 3 by 3 kernel has the weights (-1, 0, 1). What is the output where the window lies across the edge, and what is it in the flat regions?

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 b=0b = 0 it gives zero on flat regions, a positive number where the values rise from left to right, and a negative number where they fall.

Read the sign

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 MM with y=Mxy = Mx. Each output yjy_j is a dot product of the kernel with a window, so row jj of MM must be the weights, padded with zeros so that they line up with the window:

(121000012100001210000121)(201310)=(3585).\begin{pmatrix} 1 & 2 & 1 & 0 & 0 & 0 \\ 0 & 1 & 2 & 1 & 0 & 0 \\ 0 & 0 & 1 & 2 & 1 & 0 \\ 0 & 0 & 0 & 1 & 2 & 1 \end{pmatrix} \begin{pmatrix} 2 \\ 0 \\ 1 \\ 3 \\ 1 \\ 0 \end{pmatrix} = \begin{pmatrix} 3 \\ 5 \\ 8 \\ 5 \end{pmatrix}.

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 ww appears in every row. (The same view, a layer as a matrix, is how Chapter 1 of the book treats a model call.) The 4Γ—6=24{}4 \times 6 = 24 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 s=1s = 1 and p=0p = 0 for this section. The mathematician's convolution of xx with ww is

(xβˆ—w)ijβ€…β€Š=β€…β€Šβˆ‘m=1kβˆ‘n=1kwmnβ€…β€Šx iβˆ’m+1,β€…β€Šjβˆ’n+1.(x * w)_{ij} \;=\; \sum_{m=1}^{k} \sum_{n=1}^{k} w_{mn}\; x_{\,i-m+1,\; j-n+1}.

Substitute mβ€²=k+1βˆ’mm' = k + 1 - m and nβ€²=k+1βˆ’nn' = k + 1 - n. The sum becomes the cross-correlation of xx with the flipped kernel wmβ€²nβ€²β€²=wk+1βˆ’mβ€², k+1βˆ’nβ€²w'_{m'n'} = w_{k+1-m',\, k+1-n'}, read at the window whose corner is (iβˆ’k+1, jβˆ’k+1)(i - k + 1,\, j - k + 1). 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 ww, it can equally represent its flip wβ€²w', 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, wk+1βˆ’m, k+1βˆ’n=βˆ’wmnw_{k+1-m,\,k+1-n} = -w_{mn}, shows it: the kernel with (βˆ’1,0,1)(-1, 0, 1) in every row is one, and, with b=0b = 0, the edge response goes from 27{}27 to βˆ’27-27. A kernel that is the same when flipped, such as the rows (1,2,1)(1, 2, 1), (2,4,2)(2, 4, 2), (1,2,1)(1, 2, 1), gives the same output either way.

What happens when the picture moves

Take s=1s = 1 and p=0p = 0, and shift the picture Ο„\tau columns to the left, writing (TΟ„x)rc=xr, c+Ο„(T_\tau x)_{rc} = x_{r,\,c+\tau}. The output of the shifted picture at (i,j)(i, j) is

b+βˆ‘m,nwmn x i+mβˆ’1,β€…β€Šj+nβˆ’1+Ο„β€…β€Š=β€…β€Šyi, j+Ο„.b + \sum_{m,n} w_{mn}\, x_{\,i+m-1,\; j+n-1+\tau} \;=\; y_{i,\,j+\tau}.

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 ss the statement holds only for shifts Ο„\tau that are multiples of ss, and then the output moves by Ο„/s\tau / s.

Three properties, then, and we can now name them. Each output depends on only kΓ—kk \times k 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.

Three names

Locality: each output depends on only kΓ—kk \times k 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 Ο„\tau outputs of the shifted picture come from windows that touch the entering zeros and have no counterpart in the original output. With zero padding pp, the entering zeros coincide with the padding zeros the original already sees, but the pixels that left are replaced by padding, so outputs within pp columns of that edge can differ (they coincide when the pixels that left are zero). And, as without padding, the last Ο„\tau outputs of the shifted picture have no counterpart in the original output.

How big is the output?

Pad the input with pp zeros on each side, so that its length is L=N+2 pL = N + 2\,p, where NN is the original length. With stride ss the jj-th window starts at position 1+(jβˆ’1)s{}1 + (j-1)s and covers positions up to (jβˆ’1)s+k(j-1)s + k. It fits when (jβˆ’1)s+k≀L(j-1)s + k \le L, that is when jβˆ’1≀(Lβˆ’k)/sj - 1 \le (L - k)/s. Counting j=1,2,…,⌊(Lβˆ’k)/sβŒ‹+1j = 1, 2, \dots, \lfloor (L-k)/s \rfloor + 1 gives

oβ€…β€Š=β€…β€ŠβŒŠN+2 pβˆ’ksβŒ‹+1,o \;=\; \left\lfloor \frac{N + 2\,p - k}{s} \right\rfloor + 1,

This is the oo in the index form of the sum above, so ii and jj run from 1 to oo. It needs Lβ‰₯kL \ge k; otherwise there is no valid position and no output.

The size rule

Output length: o=⌊(N+2 pβˆ’k)/sβŒ‹+1o = \lfloor (N + 2\,p - k)/s \rfloor + 1, defined when N+2 pβ‰₯kN + 2\,p \ge k. The floor drops any leftover input that no window reaches.

Try it on our line. Take x=(2,0,1,3,1,0)x = (2, 0, 1, 3, 1, 0) and w=(1,2,1)w = (1, 2, 1) again, with padding p=1p = 1 and stride s=2s = 2. The padded input is (0,2,0,1,3,1,0,0)(0, 2, 0, 1, 3, 1, 0, 0), of length 8, and o=⌊(6+2βˆ’3)/2βŒ‹+1=⌊2.5βŒ‹+1=3o = \lfloor (6 + 2 - 3)/2 \rfloor + 1 = \lfloor 2.5 \rfloor + 1 = 3. The windows start at positions 1, 3 and 5 and read (0,2,0)(0, 2, 0), (0,1,3)(0, 1, 3) and (3,1,0)(3, 1, 0), giving 0+4+0=4{}0 + 4 + 0 = 4, 0+2+3=5{}0 + 2 + 3 = 5 and 3+2+0=5{}3 + 2 + 0 = 5. So y=(4,5,5)y = (4, 5, 5). The last number of the padded input is never read: the floor in the formula is that leftover.

Two cases worth remembering. With s=1s = 1 and odd kk, taking p=(kβˆ’1)/2p = (k-1)/2 gives o=No = N, so the output keeps the input's size ("same" padding). With no padding and stride 4, an 11{}11-wide kernel on a 224{}224-wide input gives ⌊213/4βŒ‹+1=54\lfloor 213/4 \rfloor + 1 = 54, and on a 227{}227-wide input gives ⌊216/4βŒ‹+1=55\lfloor 216/4 \rfloor + 1 = 55.

In the first figure the picture is 4Γ—4{}4 \times 4 and the kernel 3Γ—3{}3 \times 3; set pp and ss there and watch the window step along, saying what you expect first. For larger sizes, work from the formula. With N=28N = 28, k=5k = 5, p=0p = 0, s=1s = 1 the formula gives 24{}24; with p=2p = 2 it gives 28{}28. When k>N+2 pk > N + 2\,p 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 224Γ—224Γ—3{}224 \times 224 \times 3 input, 11Γ—11{}11 \times 11 kernels and stride 4, and does not state the first layer's padding. With no padding that gives 54{}54. Padding of 2 or 3 gives 55{}55 (⌊217/4βŒ‹+1\lfloor 217/4 \rfloor + 1 and ⌊219/4βŒ‹+1\lfloor 219/4 \rfloor + 1), and so does a 227{}227 input with none. The first layer is usually quoted as 55Γ—55{}55 \times 55; 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 kΓ—kΓ—Cink \times k \times C_{\text{in}}, and the sum runs over the channels too,

yij=b+βˆ‘ch=1Cinβˆ‘m=1kβˆ‘n=1kwmn(ch)β€…β€Šx s(iβˆ’1)+mβˆ’p,β€…β€Šs(jβˆ’1)+nβˆ’p(ch).y_{ij} = b + \sum_{\text{ch}=1}^{C_{\text{in}}} \sum_{m=1}^{k} \sum_{n=1}^{k} w^{(\text{ch})}_{mn}\; x^{(\text{ch})}_{\,s(i-1)+m-p,\; s(j-1)+n-p}.

A layer with CoutC_{\text{out}} such kernels produces CoutC_{\text{out}} output maps, and has CoutΓ—(kβ‹…kβ‹…Cin+1)C_{\text{out}} \times (k \cdot k \cdot C_{\text{in}} + 1) numbers to learn, counting one bias per kernel. For AlexNet's first layer that is 96Γ—(11β‹…11β‹…3+1)=34,944{}96 \times (11 \cdot 11 \cdot 3 + 1) = 34{,}944. 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.

Counting the first layer

CoutΓ—(kβ‹…kβ‹…Cin+1)C_{\text{out}} \times (k \cdot k \cdot C_{\text{in}} + 1) numbers to learn; for the first layer of the network above, 96Γ—(11β‹…11β‹…3+1)=34,944{}96 \times (11 \cdot 11 \cdot 3 + 1) = 34{,}944.

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 16Γ—16{}16 \times 16 patches and applying one shared linear map to each is itself a convolution, with a 16Γ—16{}16 \times 16 kernel and stride 16{}16. 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.

  1. A 32Γ—32{}32 \times 32 picture, a 3Γ—3{}3 \times 3 kernel, p=1p = 1, s=2s = 2. What is oo? Show your working from the formula.
  2. A 7Γ—7{}7 \times 7 kernel on a color picture, 64 kernels. How many numbers to learn?
  3. Edit the kernel in the first figure to respond to horizontal edges. What sign should the response have where dark lies above bright?
  4. 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?
  5. On the line, with x=(1,2,3,4,5,6)x = (1, 2, 3, 4, 5, 6) and w=(1,0,βˆ’1)w = (1, 0, -1), what is yy? Write the matrix MM as well.

Answers

Answers to the five exercises
  1. o=⌊(32+2βˆ’3)/2βŒ‹+1=16o = \lfloor (32 + 2 - 3)/2 \rfloor + 1 = 16.
  2. 64Γ—(7β‹…7β‹…3+1)=9,472{}64 \times (7 \cdot 7 \cdot 3 + 1) = 9{,}472.
  3. With b=0b = 0, the rows (βˆ’1,βˆ’1,βˆ’1)(-1,-1,-1), (0,0,0)(0,0,0), (1,1,1)(1,1,1) respond with +27+27 on a 3Γ—3{}3 \times 3 window whose top row is 0 and bottom row is 9, and with βˆ’27-27 the other way round.
  4. 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.
  5. Every output is βˆ’2-2, and MM has (1,0,βˆ’1)(1, 0, -1) 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.

Read Chapter 1: what a model call computes

A convolution is one linear map with shared weights. Chapter 1 derives what a whole model computes and checks it against a real one.

Learn the mechanism. Test the claim.

Join the Prof Rod newsletter for one educational lesson a week, with worked examples attached. It is free to register for and separate from the Zero Employee community.