ConstantPad2d

torch.nn.ConstantPad2d(padding, value)[源代码]

用常数值填充输入张量的边界。

对于 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.ConstantPad2d(2, 3.5)
>>> input = torch.randn(1, 2, 2)
>>> input
tensor([[[ 1.6585,  0.4320],
         [-0.8701, -0.4649]]])
>>> m(input)
tensor([[[ 3.5000,  3.5000,  3.5000,  3.5000,  3.5000,  3.5000],
         [ 3.5000,  3.5000,  3.5000,  3.5000,  3.5000,  3.5000],
         [ 3.5000,  3.5000,  1.6585,  0.4320,  3.5000,  3.5000],
         [ 3.5000,  3.5000, -0.8701, -0.4649,  3.5000,  3.5000],
         [ 3.5000,  3.5000,  3.5000,  3.5000,  3.5000,  3.5000],
         [ 3.5000,  3.5000,  3.5000,  3.5000,  3.5000,  3.5000]]])
>>> # using different paddings for different sides
>>> m = nn.ConstantPad2d((3, 0, 2, 1), 3.5)
>>> m(input)
tensor([[[ 3.5000,  3.5000,  3.5000,  3.5000,  3.5000],
         [ 3.5000,  3.5000,  3.5000,  3.5000,  3.5000],
         [ 3.5000,  3.5000,  3.5000,  1.6585,  0.4320],
         [ 3.5000,  3.5000,  3.5000, -0.8701, -0.4649],
         [ 3.5000,  3.5000,  3.5000,  3.5000,  3.5000]]])
本页目录