Convolution Padding and Stride Parameters

Convolutional operations reduce spatial dimensions when processing inputs. For an input tensor of size \(n_h \times n_w\) and kernel dimensions \(k_h \times k_w\), output dimentions become \((n_h-k_h+1) \times (n_w-k_w+1)\). Sequential convolutions exacerbate this reduction - a \(240 \times 240\) image processed through ten \(5\times5\) convolutional layers would shrink to \(200\times200\), losing significant boundary information. Padding mitigates this while stride optimizes computational efficiency.

Padding Fundamentals

Padding preserves spatial dimensions by adding elements (typically zeros) along tensor boundaries. Adding \(p_h\) rows and \(p_w\) columns yields output dimensions:

\[(n_h - k_h + p_h + 1) \times (n_w - k_w + p_w + 1)\] Symmetric padding (\(p_h = k_h-1\), \(p_w = k_w-1\)) maintains identical input-output dimensions when kernel dimensions are odd. This configuration centers the kernel over each input element during computation.

import torch
from torch import nn

def apply_convolution(conv_module, input_tensor):
    reshaped_input = input_tensor.view(1, 1, *input_tensor.shape)
    output = conv_module(reshaped_input)
    return output.view(output.shape[2:])

# Symmetric padding example
conv_layer = nn.Conv2d(1, 1, kernel_size=3, padding=1)
sample_input = torch.rand(8, 8)
print(apply_convolution(conv_layer, sample_input).shape)  # Output: torch.Size([8, 8])
# Custom height/width padding
conv_layer = nn.Conv2d(1, 1, kernel_size=(5,3), padding=(2,1))
print(apply_convolution(conv_layer, sample_input).shape)  # Output: torch.Size([8, 8])

Stride (\(s\)) controls kernel traversal step size. With vertical stride \(s_h\) and horizontal stride \(s_w\), output dimensions become:

\[\left\lfloor \frac{n_h - k_h + p_h + s_h}{s_h} \right\rfloor \times \left\lfloor \frac{n_w - k_w + p_w + s_w}{s_w} \right\rfloor\] When input dimensions are divisible by stride values, output dimensions simplify to \((n_h/s_h) \times (n_w/s_w)\). Larger strides reduce spatial resolution while improving computational efficinecy.

# Stride reduction example
conv_layer = nn.Conv2d(1, 1, kernel_size=3, padding=1, stride=2)
print(apply_convolution(conv_layer, sample_input).shape)  # Output: torch.Size([4, 4])

# Combined stride and padding
conv_layer = nn.Conv2d(1, 1, kernel_size=(3,5), padding=(0,1), stride=(3,4))
print(apply_convolution(conv_layer, sample_input).shape)  # Output: torch.Size([2, 2])

Tags: pytorch convolutional neural networks padding Stride Kernel Operations

Posted on Fri, 21 Aug 2026 16:29:41 +0000 by jase35750