Ch 3 ▸ Stride, padding and dilation ▸ Module 4/4
Putting it together.
🔒 This module is locked
It builds on Dilation. Pass that module’s knowledge check to unlock it, or skip ahead if you already know the material.
Now put the three settings together. Padding grows the input to pixels. The kernel spans of them, so its starting position can range from to . Stepping through that range at a time gives the output size:
The ⌊ ⌋ rounds down. It’s the leftover pixels from the stride module: if the last step would leave the window hanging off the edge, that step isn’t taken.
This is exactly the formula in the PyTorch documentation for nn.Conv2d. The figure below has every setting at once, plus a listing of the dot product behind the highlighted output. Change the settings and check the listing’s out_size line against the formula.
Try the other kernels too. sobel_x and outline have negative weights, so their outputs can go below zero, and the figure switches to blue for negative and red for positive. You can also type your own weights into the kernel.
In code, the three settings are just arguments:
import torch.nn as nn
# 32×32 in → 16×16 out
nn.Conv2d(1, 1, kernel_size=3, stride=2, padding=1)
# 32×32 in → 32×32 out ("same" only works at stride 1)
nn.Conv2d(1, 1, kernel_size=3, padding="same")
# 32×32 in → 32×32 out, each output sees a 5×5 area
nn.Conv2d(1, 1, kernel_size=3, padding=2, dilation=2)Knowledge check ▸ 4 questions
Before you move on.
Answer every question correctly to unlock the next module. A wrong answer gets a hint, and you can try again as many times as you like.
1A 28×28 input, 3×3 kernel, padding 1, stride 2. How many pixels wide is the output?
2A 32×32 input, 3×3 kernel, no padding, dilation 2, stride 1. How many pixels wide is the output?
3A 64×64 input, 3×3 kernel, padding 1, stride 2. How many pixels wide is the output?
4Why does the formula round down (the ⌊ ⌋)?