liveBook · Manning

Padding

As explained in Math and Architectures of Deep Learning

The treatment of positions where a convolution kernel extends beyond the input array, such as stopping early or pretending missing values are zero.

Padding determines what happens when a convolution kernel reaches an input boundary and part of the kernel extends beyond the available array. It changes which positions produce outputs and therefore affects output size. section 10.1 section 10.8

Two boundary strategies are central:

  • Valid padding stops the sliding operation as soon as any kernel element would fall outside the input. Every kernel position therefore uses valid input values, but the output is smaller. section 10.1 section 10.8
  • Same (zero) padding continues the operation by treating positions outside the input as zero. With stride 1, the output size matches the input size. section 10.1 section 10.8

For an input of size n, kernel size k, and stride s, valid padding allows shifts only while the kernel’s right end remains within the final input index. If p zeros are added to each side, the effective input size becomes n + 2p; the same reasoning extends independently to each dimension. section 10.2

A sliding kernel at the boundary

A size-3 kernel on a size-7 array with valid padding stops before producing an output centered on the final input positions. With zero padding, missing boundary values are replaced by zeros, allowing additional slide positions. The same boundary choice applies to image and video convolutions. section 10.8

Listing 10.1 PyTorch code for 1D local averaging convolution
import torch

x = torch.tensor(                                          #1
        [-1.,  4., 11., 14., 21., 25., 30.])

w = torch.tensor([0.33, 0.33, 0.33])                       #2

x = x.unsqueeze(0).unsqueeze(0)
w = w.unsqueeze(0).unsqueeze(0)                            #3

conv1d = torch.nn.Conv1d(1, 1, kernel_size=3,              #4
            stride=1, padding=[0], bias=False)

conv1d.weight = torch.nn.Parameter(w, requires_grad=False) #5

with torch.no_grad():                                      #6

    y = conv1d(x)                                          #7

Inspect the PyTorch Conv1d example, where padding is supplied as a convolution parameter alongside kernel size and stride. section 10.1.4

How Math and Architectures of Deep Learning covers it

How deep it goes
Defines valid and zero padding, derives output-size reasoning, and connects padding to PyTorch parameters.
Examples
Uses size-3 kernels on one-dimensional arrays, boundary figures, output-size equations, and PyTorch Conv1d listings.
What it assumes
Assumes familiarity with input arrays, kernels, stride, and sliding convolution positions.
What you can do after
Apply it via PyTorch by selecting padding alongside kernel size and stride in a convolution layer.
How it connects
Connects padding to convolution, kernel size, stride, output size, receptive field, and PyTorch Conv1d.

Also explained in

Compare how 19 books explain padding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math and Architectures of Deep Learning. The section links point to the parts of the book it draws on.