nn.AdaptiveAvgPool2d

torch.nn.AdaptiveAvgPool2d

torch.nn.AdaptiveAvgPool2d(output_size)

  • output_size:可以为tuple类型(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.

  • 输入:(N, C, H_in, W_in)or(C, H_in, W_in)
  • 输出:(N, C, S_0, S_1)or(C, S_0, S_1),S = output_size
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)
'''

下面是第一个程序的执行过程,值与后面两个执行过程,我猜测可能kernal size并不是一个正方形,而是随着输出调整为矩形,步长依赖输出和核大小而定

nn.AdaptiveAvgPool2d
上一篇:05-Pandas层级索引


下一篇:06-Panda统计计算和描述