【pytorch】nn.AdaptiveMaxPool2d

官方文档地址

output_size – the target output size of the image of the form H x W. Can be a tuple (H, W) or a single H for a square image H x H. H and W can be either a int, or None which means the size will be the same as that of the input.

return_indices – if True, will return the indices along with the outputs. Useful to pass to nn.MaxUnpool2d. Default: False

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

input = torch.tensor([[[[2, 4, 8, 15],
                        [3, 6, 9, 19],
                        [7, 22, 5, 12],
                        [1, 66, 1, 77]]]], dtype=torch.float64)

m = nn.AdaptiveMaxPool2d((3, 3))
output = m(input)
print(input)
print(output)

# padding 是我自己补的,方便观察
# tensor([[[[0.0,  0.0,  0.0,   0.0,  0.0,   0.0]
#           [0.0,  2.,   4.,    8.,   15.,   0.0],
#           [0.0,  3.,   6.,    9.,   19.,   0.0],
#           [0.0,  7.,   22.,   5.,   12.,   0.0],
#           [0.0,  1.,   66.,   1.,   77.,    0.0],
#           [0.0,  0.0,  0.0,   0.0,  0.0,   0.0]]]], dtype=torch.float64)

# tensor([[[[ 6.,  9., 19.],
#           [22., 22., 19.],
#           [66., 66., 77.]]]], dtype=torch.float64)

全局平均池化 GAP(Global Average Pooling)的优势在于:

各个类别于Feature Map 之间的联系更加直观(相比与全连接层的黑箱来说),Feature Map 被转化为分类概率也更加容易,因为在GAP中没有参数需要调,所以避免了过拟合的问题。

GAP汇总了空间信息,因此对输入的空间转换鲁棒性更强,所以目前卷积网络中对后面几个全连接层,大都是用GAP替换。

全局池化在Kreas中有对应的层,为全局最大池化层(GlobalMaxPooling2D),但在Pytorch中虽然没有对应名称的池化层,但是可以使用Pytorch中的自适应池化层(==AdaptiveMaxPool2d(1)或者AdaptiveMaxPool2d(1)==来实现)

你可能感兴趣的:(#,pytorch,pytorch,深度学习,人工智能)