ReplicationPad2d
- 类torch.nn.ReplicationPad2d(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.ReplicationPad2d(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([[[[0., 0., 0., 1., 2., 2., 2.], [0., 0., 0., 1., 2., 2., 2.], [0., 0., 0., 1., 2., 2., 2.], [3., 3., 3., 4., 5., 5., 5.], [6., 6., 6., 7., 8., 8., 8.], [6., 6., 6., 7., 8., 8., 8.], [6., 6., 6., 7., 8., 8., 8.]]]]) >>> # using different paddings for different sides >>> m = nn.ReplicationPad2d((1, 1, 2, 0)) >>> m(input) tensor([[[[0., 0., 1., 2., 2.], [0., 0., 1., 2., 2.], [0., 0., 1., 2., 2.], [3., 3., 4., 5., 5.], [6., 6., 7., 8., 8.]]]])