torch.nn.functional.pad
- torch.nn.functional.pad(input, pad, mode='constant', value=None) → Tensor
Pads tensor.
- Padding size:
Pads tensor.
The padding size by which to pad some dimensions of input
are described starting from the last dimension and moving forward.
dimensions
of input will be padded.
For example, to pad only the last dimension of the input tensor, then
pad has the form
;
to pad the last 2 dimensions of the input tensor, then use
;
to pad the last 3 dimensions, use
.
See torch.nn.CircularPad2d, torch.nn.ConstantPad2d,
torch.nn.ReflectionPad2d, and torch.nn.ReplicationPad2d
for concrete examples on how each of the padding modes works. Constant
padding is implemented for arbitrary dimensions. Circular, replicate and
reflection padding are implemented for padding the last 3 dimensions of a
4D or 5D input tensor, the last 2 dimensions of a 3D or 4D input tensor,
or the last dimension of a 2D or 3D input tensor.
Examples:
>>> t4d = torch.empty(3, 3, 4, 2)
>>> p1d = (1, 1) # pad last dim by 1 on each side
>>> out = F.pad(t4d, p1d, "constant", 0) # effectively zero padding
>>> print(out.size())
torch.Size([3, 3, 4, 4])
>>> p2d = (1, 1, 2, 2) # pad last dim by (1, 1) and 2nd to last by (2, 2)
>>> out = F.pad(t4d, p2d, "constant", 0)
>>> print(out.size())
torch.Size([3, 3, 8, 4])
>>> t4d = torch.empty(3, 3, 4, 2)
>>> p3d = (0, 1, 2, 1, 3, 3) # pad by (0, 1), (2, 1), and (3, 3)
>>> out = F.pad(t4d, p3d, "constant", 0)
>>> print(out.size())
torch.Size([3, 9, 7, 3])