當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python PyTorch ReplicationPad2d用法及代碼示例

本文簡要介紹python語言中 torch.nn.ReplicationPad2d 的用法。

用法:

class torch.nn.ReplicationPad2d(padding)

參數

padding(int,tuple) -填充的大小。如果是 int ,則在所有邊界中使用相同的填充。如果是 4- tuple ,則使用( )

使用輸入邊界的複製填充輸入張量。

對於 N 維填充,請使用 torch.nn.functional.pad()

形狀:
  • 輸入:

  • 輸出: ,其中

    H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}

    W_{out} = W_{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.]]]])

相關用法


注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.nn.ReplicationPad2d。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。