torch.nn.functional.pad¶
- torch.nn.functional.pad(input, pad, mode='constant', value=None) Tensor [源代码][源代码]¶
填充张量。
- 填充大小
用于填充
input
张量某些维度的填充大小,是从最后一个维度开始向前描述的。 维度的input
将被填充。例如,仅填充输入张量的最后一个维度时,pad
的形式为 ;填充输入张量的最后 2 个维度时,使用 ;填充最后 3 个维度时,使用 。- 填充模式
关于每种填充模式具体如何工作,请参阅
torch.nn.CircularPad2d
,torch.nn.ConstantPad2d
,torch.nn.ReflectionPad2d
和torch.nn.ReplicationPad2d
。 常数填充适用于任意维度。循环 (Circular)、复制 (replicate) 和反射 (reflection) 填充适用于填充 4D 或 5D 输入张量的最后 3 个维度,3D 或 4D 输入张量的最后 2 个维度,或者 2D 或 3D 输入张量的最后一个维度。
注意
使用 CUDA 后端时,此操作在其反向传播过程中可能会导致非确定性行为,且不容易关闭。有关背景信息,请参阅关于可复现性的说明。
- 参数
- 返回类型
示例
>>> 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])