Padding, stride and the size of a convolution's output
10 questions with small calculations on valid and same convolutions, padding values, odd kernel sizes, strided convolutions and output sizes.
- 10 questions
- Stopwatch
- Instant feedback
Sample questions
Play now ↑Answers stay hidden. Start the quiz to play them.
Sample 1
A 7 by 7 kernel is used in a same convolution, with a stride of 1, so that the output has the same size as the input. How much padding is needed on each side?
- 3 pixels
- 7 pixels
- 1 pixel
How it works
- One question per screen. An answer is final once sent.
- You see right away whether you were right, with a short explanation.
- The stopwatch runs: on equal scores, the faster player ranks first.
- Only your best attempt counts, so replaying is never risky.
About this quiz
Slide a three-by-three window across a ten-by-ten picture and count how many positions the window can take. It cannot be centred on the pixels along the border, because part of the window would hang off the edge, so there are only eight positions along each side. The output is smaller than the input. Do this in layer after layer and the picture shrinks until nothing is left. Two simple devices, padding and stride, give designers control over this shrinkage, and they are among the first practical details anyone building a convolutional network has to understand.
This quiz is mostly arithmetic with a purpose. You will work out the size of a feature map for a given image, kernel, padding and stride, and you will see why the formulas look the way they do. You will meet the vocabulary of valid and same convolutions, and see what padding value is the most sensible when the border has to be filled with something. You will see why practitioners almost always use odd kernel sizes. And you will see what a stride does: instead of moving the window one pixel at a time, it moves several, so the output is smaller by roughly the stride in each direction, which is a cheap way of shrinking the data as it flows through the network.
The numbers are chosen to be easy. A small image, a small kernel, a stride of two. If a calculation takes you more than a few seconds, you are overthinking it, and the explanation shows the one-line reasoning that makes it simple. By the end you should be able to predict the shape of any convolutional layer's output at a glance, which is a skill that saves a surprising amount of debugging time.
Common mistakes include forgetting to add the padding on both sides, confusing the padding needed for the output to keep its size with the size of the kernel, and expecting a stride to divide the output size exactly. Another is to treat the border as unimportant, although in small feature maps a lot of the information lives there. Shape arithmetic matters more than it first appears. A mismatch between the size a layer produces and the size the next layer expects is among the most common errors in practice, and a designer who can calculate sizes in their head can plan a whole architecture on the back of an envelope, deciding where to halve the resolution and where to keep it. The same arithmetic returns later, when a network has to bring a shrunken representation back up to the size of the original picture.
The quiz suits learners who have just met convolutions and want practice with shapes, and practitioners who like to check their intuition. There are 10 questions, each with an explanation, and it takes about four minutes.
Questions and answers
›How long does the quiz take?
About 3 to 4 minutes for 10 questions, at roughly 20 seconds each. An explanation follows every answer, including the working for each calculation.
›Do I need to memorise formulas?
No. Each question that needs one gives it in words, or it follows from simple counting of positions. The aim is to understand why the output has the size it has, so that you can reconstruct the formulas whenever you need them.
›Why does a convolution make the output smaller?
A kernel can only be placed where it fits completely inside the image. For a square kernel with side M applied to an image with J rows and K columns, there are J minus M plus one positions vertically and K minus M plus one horizontally. The border pixels never sit at the centre of a window, so the output shrinks by M minus one in each direction.
›What is padding?
Padding means adding extra pixels around the outside of the image before applying the filter. Padding by P pixels on every side adds two P to each dimension, so the output has J plus two P minus M plus one rows, and likewise for columns. It lets the kernel be centred on the original border pixels and keeps the feature map from shrinking.
›What are valid and same convolutions?
A valid convolution uses no padding, so the output is smaller than the input. A same convolution uses just enough padding that the output has the same size as the input, which for a square kernel of side M means padding by M minus one over two pixels on each side. The names describe what the output size is, relative to the input.
›What values should the padding pixels have?
A typical choice is zero, applied after the average has been subtracted from each image, so that zero represents an average pixel intensity. That way the padded border looks like neutral background to the filter, instead of looking like a sharp edge against black. Padding can also be applied to feature maps that feed later convolutional layers.
›Why are kernels usually of odd size?
With an odd side, there is a well-defined central pixel on which the window is centred, and the padding can be the same on every side of the image. With an even side, the window has no centre, and symmetric padding would not give an output of the same size as the input.
›What is a strided convolution?
It is a convolution in which the filter moves across the image in steps of more than one pixel, the stride. The number of output positions shrinks to roughly one over the stride in each direction, so a stride of two makes each side about half as long. It is a simple way of reducing the size of the representation inside a network.