pytorch函数AdaptiveMaxPool2d

class torch.nn.AdaptiveMaxPool2d(output_size, return_indices=False)

对输入信号,提供2维的自适应最大池化操作 对于任何输入大小的输入,可以将输出尺寸指定为H*W,但是输入和输出特征的数目不会变化。
可以用来实现全局平均最大池化层output_size=1

参数:
output_size: 输出信号的尺寸,可以用(H,W)表示HW的输出,也可以使用数字H表示HH大小的输出
return_indices: 如果设置为True,会返回输出的索引。对 nn.MaxUnpool2d有用,默认值是False

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)
m = torch.nn.AdaptiveMaxPool2d(7)
input = torch.autograd.Variable(torch.randn(1, 64, 10, 9))
output = m(input)
print(output.shape)
input = torch.autograd.Variable(torch.randn(1, 2, 3, 2))
m = torch.nn.AdaptiveAvgPool2d(1)
output = m(input)
print(input)
print(output,output.shape)

torch.Size([1, 64, 5, 7])
torch.Size([1, 64, 7, 7])
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])

你可能感兴趣的:(pytorch)