ReflectionPad2d
- 类torch.nn.ReflectionPad2d(padding)[源代码]
-
用输入边界镜像来填充输入张量。
对于 N 维填充,可以使用
torch.nn.functional.pad()
函数。- 参数
-
padding (int, tuple) – 填充的大小。如果为 int,则在所有边界使用相同的填充值。如果是 4-元组,则使用 ($\text{padding\_left}$, $\text{padding\_right}$, $\text{padding\_top}$, $\text{padding\_bottom}$)。需要注意的是,填充大小应小于相应的输入维度。
- 形状:
-
-
输入: $(N, C, H_{in}, W_{in})$ 或 $(C, H_{in}, W_{in})$。
-
输出为 $(N, C, H_{out}, W_{out})$ 或 $(C, H_{out}, W_{out})$
$H_{\text{out}} = H_{\text{in}} + \text{padding\_top} + \text{padding\_bottom}$
$W_{\text{out}} = W_{\text{in}} + \text{padding\_left} + \text{padding\_right}$
-
示例:
>>> m = nn.ReflectionPad2d(2) >>> input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3) >>> input tensor([[[[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]]]]) >>> m(input) tensor([[[[8., 7., 6., 7., 8., 7., 6.], [5., 4., 3., 4., 5., 4., 3.], [2., 1., 0., 1., 2., 1., 0.], [5., 4., 3., 4., 5., 4., 3.], [8., 7., 6., 7., 8., 7., 6.], [5., 4., 3., 4., 5., 4., 3.], [2., 1., 0., 1., 2., 1., 0.]]]]) >>> # using different paddings for different sides >>> m = nn.ReflectionPad2d((1, 1, 2, 0)) >>> m(input) tensor([[[[7., 6., 7., 8., 7.], [4., 3., 4., 5., 4.], [1., 0., 1., 2., 1.], [4., 3., 4., 5., 4.], [7., 6., 7., 8., 7.]]]])