Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

F.pad

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.

How it works

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.

Syntax

torch.nn.functional.pad(input, pad, mode='constant', value=None)

Parameters

NameTypeDescription
inputTensorN-dimensional tensor to pad.
padtupleEven-length tuple of padding sizes; values are specified from the last dimension backward.
modestrPadding strategy: 'constant', 'reflect', 'replicate', or 'circular'. Defaults to 'constant'.
valuefloat | NoneFill value for constant padding. Defaults to 0.

Returns

Tensor

Key points

  • The pad tuple is interpreted from the input's last dimension toward earlier dimensions.
  • Mode constant can pad tensors across arbitrary dimensions.
  • The function supports four padding modes: constant, reflect, replicate, and circular.
  • The value argument supplies the element used for newly added regions in constant mode.

Common mistakes

  • Non-constant modes have dimensionality restrictions; they do not support arbitrary input dimensions like constant padding does.
  • On CUDA, the backward computation may be nondeterministic and the documentation says this cannot always be controlled.

Example: Constant padding on the last dimension

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.

When to use it

Adjusting tensor boundaries

Preparing image, audio, or sequence tensors for a layer that expects larger spatial or temporal dimensions.

Applying custom constant borders

Adding nonzero borders around data before convolution or other local operations.

Non-constant boundary handling

Choosing boundary behavior that reuses or reflects existing edge values.

Interview questions

QWhat does F.pad do, and how is pad interpreted?

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.

QWhat padding modes and fill behavior does F.pad provide?

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.

QWhat shape restrictions apply to non-constant modes?

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.pad
  • torch.nn.ConstantPad1d
  • torch.nn.ConstantPad2d
  • torch.nn.ReflectionPad1d

Source: torch.nn.functional.pad — PyTorch 2.11 documentation. Checked 2026-10-16.