天天看点

pytorch函数之torch.nn.AdaptiveMaxPool2d()

1、函数介绍

1.1 作用

该函数提供了2维的自适应最大池化操作,对于任何大小的输入,可以得到维度为(H,W)的输出,其中,输入特征和输出特征的channel不会变化。

1.2 参数

  • output_size: 输出特征的尺寸,可以用(H,W)表示得到的输出
  • return_indices: 如果设置为True,会返回输出的索引。对 nn.MaxUnpool2d有用,

    默认值是False

1.3 举例

m = torch.nn.AdaptiveMaxPool2d((5,7))
input = torch.autograd.Variable(torch.randn(1, 64, 8, 9))
# target output size of 7x7 (square)
print(m(input).shape)		# torch.Size([1, 64, 5, 7])

# ----------------------------------------------------------------------------------------------------

m = torch.nn.AdaptiveMaxPool2d(7)	
input = torch.autograd.Variable(torch.randn(1, 64, 10, 9))
output = m(input)
print(output.shape)			# torch.Size([1, 64, 7, 7])

# ----------------------------------------------------------------------------------------------------

m = torch.nn.AdaptiveAvgPool2d(1)
input = torch.autograd.Variable(torch.randn(1, 2, 3, 2))
output = m(input)
print(input)
print(output, output.shape)

tensor([[[[ 0.1300, -0.0319],
          [-1.9817, -0.0691],
          [ 0.5286,  2.1709]],

         [[ 0.2248,  0.4539],
          [-1.0164,  1.1421],
          [-0.0045, -0.1006]]]])
          
tensor([[[[0.1244]],
         [[0.1165]]]]) torch.Size([1, 2, 1, 1])


           

继续阅读