Adjusting tensor boundaries
Preparing image, audio, or sequence tensors for a layer that expects larger spatial or temporal dimensions.
PyTorch · Function
F.pad pads selected trailing dimensions of an N-dimensional tensor. It supports constant, reflection, replication, and circular modes, with zero-filled constant padding by default.
F.pad returns a padded tensor by applying the requested padding to dimensions counted from the input's last dimension backward. The tuple alternates right and left padding for each selected dimension, while mode chooses constant, reflection, replication, or circular behavior.
torch.nn.functional.pad(input, pad, mode='constant', value=None)| Name | Type | Description |
|---|---|---|
input | Tensor | N-dimensional tensor to pad. |
pad | tuple | Even-length tuple of padding sizes; values are specified from the last dimension backward. |
mode | str | Padding strategy: 'constant', 'reflect', 'replicate', or 'circular'. Defaults to 'constant'. |
value | float | None | Fill value for constant padding. Defaults to 0. |
Tensor
import torch
import torch.nn.functional as F
x = torch.tensor([[1., 2.], [3., 4.]])
y = F.pad(x, (1, 2), mode="constant", value=0.5)
print(y)
# tensor([[0.5, 1.0, 2.0, 0.5, 0.5],
# [0.5, 3.0, 4.0, 0.5, 0.5]])Pads the last dimension with one value on the left and two on the right.
Preparing image, audio, or sequence tensors for a layer that expects larger spatial or temporal dimensions.
Adding nonzero borders around data before convolution or other local operations.
Choosing boundary behavior that reuses or reflects existing edge values.
F.pad pads an N-dimensional tensor and returns a Tensor. Padding is specified from the last dimension toward earlier dimensions, with each dimension represented by a pair of values.
Follow-up: How does a four-value tuple apply to a two-dimensional tensor? It pads the last dimension first, then the second-to-last dimension.
The default mode is constant, which fills newly added elements with value; value defaults to 0. The other documented modes are reflect, replicate, and circular.
Follow-up: What happens if value is omitted in constant mode? The inserted elements use zero because the documented default is 0.
Constant padding works for arbitrary dimensions. Reflection, replication, and circular padding are limited to particular trailing dimensions and input ranks, so the selected mode must match the tensor shape.
Follow-up: What should be checked before using reflection on a tensor? Confirm that its rank and trailing dimensions fall within the documented supported cases.
torch.nn.functional.padtorch.nn.ConstantPad1dtorch.nn.ConstantPad2dtorch.nn.ReflectionPad1d