torch.nn.functional.pad¶
- torch.nn.functional.pad(input, pad, mode='constant', value=None) Tensor [source][source]¶
填充张量。
- 填充大小:
从最后一个维度开始描述用于填充input
的填充大小。 维度将进行填充。例如,仅填充输入张量的最后一个维度,则pad
的形式为 ;要填充输入张量的最后一个 2 个维度,则使用 ;要填充最后一个 3 个维度,则使用 。- 填充模式:
请参阅torch.nn.CircularPad2d
、torch.nn.ConstantPad2d
、torch.nn.ReflectionPad2d
和torch.nn.ReplicationPad2d
,了解每种填充模式的具体示例。常量填充适用于任意维度。循环、复制和反射填充适用于填充 4D 或 5D 输入张量的最后一个 3 个维度、3D 或 4D 输入张量的最后一个 2 个维度,或 2D 或 3D 输入张量的最后一个维度。
注意
当使用 CUDA 后端时,此操作的逆过程可能会产生不易关闭的非确定性行为。请参阅可重现性说明以获取背景信息。
- 参数:
输入(张量)- 多维张量
填充(元组)- m 个元素的元组,其中 是输入维度, 是偶数。
模式(字符串)-
'constant'
,'reflect'
,'replicate'
或'circular'
。默认:'constant'
值(可选[浮点数])-
'constant'
填充的填充值。默认:0
- 返回类型:
示例:
>>> 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])