Zero padding adds a border of zero-valued pixels around an input before convolution. It controls the output feature map’s spatial dimensions and can preserve the input size. section 4.2.2
Use padding alongside stride to control how convolution changes an image:
- Without padding, convolution produces an output smaller than the input. section 4.2.2
- With padding=1, a 3 × 3 input receives one zero row and column around each border, becoming 5 × 5 before filtering. section 4.2.2
- Stride controls how far the filter moves; increasing it reduces the output’s spatial dimensions. section 4.2.2
A padded 2D convolution
A convolution with kernel_size=2, stride=2, and padding=1 scans a padded input. At the top-left corner, the region [[0, 0], [0, 1]] and filter [[1, 2], [3, 4]] produce 4; another scan produces 32 at the bottom-left. The resulting feature map is [[4, 7], [32, 50]]. section 4.2.2
conv=nn.Conv2d(in_channels=1,
out_channels=1,
kernel_size=2,
stride=2, #1
padding=1) #2
sd=conv.state_dict()
for k in sd:
with torch.no_grad():
sd[k].copy_(weights[k])
output = conv(img)
print(output)
Inspect the nn.Conv2d configuration and its resulting tensor to connect padding, stride, kernel size, and output shape. section 4.2.2

