liveBook · Manning

Zero padding

As explained in Learn Generative AI with PyTorch

The addition of zero-valued borders around an input to control convolution output size.

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

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains borders, output-size effects, tensor dimensions, and scan calculations, but does not derive general convolution formulas.
Examples
Uses a 3 × 3 input, a 2 × 2 filter, padding=1, stride=2, explicit scan arithmetic, and a 2 × 2 output tensor.
What it assumes
Assumes familiarity with images and convolutional layers while introducing stride, padding, filters, and output feature maps.
What you can do after
Apply it via nn.Conv2d by setting padding alongside kernel_size and stride to shape convolution outputs.
How it connects
Connects zero padding to convolutional layers, stride, kernel size, feature maps, and transposed convolutional layers.

Also explained in

Compare how 2 books explain zero padding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.