转载:https://blog.csdn.net/weixin_41978699/article/details/121176286
torh.nn.AdaptiveAvgPool2d
官方文档:https://pytorch.org/docs/stable/generated/torch.nn.AdaptiveAvgPool2d.html
(H, W)
,也可以为一个数字H
表示(H, H)
,H,W
可以为int
或者None
类型,如果是None
默认与输入相同大小二维平均自适应池化,只需要给出输出的参数就可以自动寻找相应的kernal size以及stride
Applies a 2D adaptive average pooling over an input signal composed of several input planes.
The output is of size H x W, for any input size. The number of output features is equal to the number of input planes.
input = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=torch.float64)
input = torch.unsqueeze(input, 0)
# input = torch.cat((input, input, input), 0)
m = nn.AdaptiveAvgPool2d((2,2))
output = m(input)
'''
input:
tensor([[[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]]], dtype=torch.float64)
output:
tensor([[[3., 4.],
[6., 7.]]], dtype=torch.float64)
'''
# 将AdaptiveAvgPool2d((2,2))换成AdaptiveAvgPool2d(2)
# 输出依然为(2, 2)维度,不变
p = nn.AdaptiveAvgPool2d(2)
output = p(input)
'''
input:
tensor([[[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]]], dtype=torch.float64)
output:
tensor([[[3., 4.],
[6., 7.]]], dtype=torch.float64)
'''
# 其中一维改为None,这一维与输入相同
q = nn.AdaptiveAvgPool2d((None, 2))
output = q(input)
'''
input:
tensor([[[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]]], dtype=torch.float64)
output:
tensor([[[1.5000, 2.5000],
[4.5000, 5.5000],
[7.5000, 8.5000]]], dtype=torch.float64)
'''