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


















