Shortcuts

CircularPad2d

class torch.nn.CircularPad2d(padding)[源代码]

使用输入边界的循环填充来填充输入张量。

维度开始处的张量值用于填充末尾,而末尾的值用于填充开始处。如果应用了负填充,则张量的末端会被移除。

对于N维填充,使用torch.nn.functional.pad()

Parameters

padding (int, tuple) – 填充的大小。如果是int,则在所有边界使用相同的填充。如果是4-tuple,则使用 (padding_left\text{padding\_left}, padding_right\text{padding\_right}, padding_top\text{padding\_top}, padding_bottom\text{padding\_bottom})

Shape:
  • 输入:(N,C,Hin,Win)(N, C, H_{in}, W_{in})(C,Hin,Win)(C, H_{in}, W_{in})

  • 输出: (N,C,Hout,Wout)(N, C, H_{out}, W_{out})(C,Hout,Wout)(C, H_{out}, W_{out}), 其中

    Hout=Hin+上填充+下填充H_{out} = H_{in} + \text{上填充} + \text{下填充}

    Wout=Win+左填充+右填充W_{out} = W_{in} + \text{左填充} + \text{右填充}

示例:

>>> m = nn.CircularPad2d(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([[[[4., 5., 3., 4., 5., 3., 4.],
          [7., 8., 6., 7., 8., 6., 7.],
          [1., 2., 0., 1., 2., 0., 1.],
          [4., 5., 3., 4., 5., 3., 4.],
          [7., 8., 6., 7., 8., 6., 7.],
          [1., 2., 0., 1., 2., 0., 1.],
          [4., 5., 3., 4., 5., 3., 4.]]]])
>>> # 对不同边使用不同的填充
>>> m = nn.CircularPad2d((1, 1, 2, 0))
>>> m(input)
tensor([[[[5., 3., 4., 5., 3.],
          [8., 6., 7., 8., 6.],
          [2., 0., 1., 2., 0.],
          [5., 3., 4., 5., 3.],
          [8., 6., 7., 8., 6.]]]])
优云智算