当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python PyTorch ZeroPad2d用法及代码示例

本文简要介绍python语言中 torch.nn.ZeroPad2d 的用法。

用法:

class torch.nn.ZeroPad2d(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.ZeroPad2d(2)
>>> input = torch.randn(1, 1, 3, 3)
>>> input
tensor([[[[-0.1678, -0.4418,  1.9466],
          [ 0.9604, -0.4219, -0.5241],
          [-0.9162, -0.5436, -0.6446]]]])
>>> m(input)
tensor([[[[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000, -0.1678, -0.4418,  1.9466,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.9604, -0.4219, -0.5241,  0.0000,  0.0000],
          [ 0.0000,  0.0000, -0.9162, -0.5436, -0.6446,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000]]]])
>>> # using different paddings for different sides
>>> m = nn.ZeroPad2d((1, 1, 2, 0))
>>> m(input)
tensor([[[[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
          [ 0.0000, -0.1678, -0.4418,  1.9466,  0.0000],
          [ 0.0000,  0.9604, -0.4219, -0.5241,  0.0000],
          [ 0.0000, -0.9162, -0.5436, -0.6446,  0.0000]]]])

相关用法


注:本文由纯净天空筛选整理自pytorch.org大神的英文原创作品 torch.nn.ZeroPad2d。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。