Working of nn.nn.AdaptiveAvgPool2d()?

A=torch.rand(8,64,6,15)
m = nn.AdaptiveAvgPool2d(1)
o=m(A)
o.shape
>>torch.Size([8, 64, 1, 1])

Here what I am grasping is every 6*15 will be average to get a single value. Then

A=torch.rand(8,64,6,15)
m = nn.AdaptiveAvgPool2d(2)
o=m(A)
o.shape
>>torch.Size([8, 64, 2, 2])

Here the output shape indicates that every 5*14 grid is average to get this shape 2*2?
So the output size is denoted in kernel??
Am I right ?

1 Like

Looks correct to me according to python - What is Adaptive average pooling and How does it work? - Stack Overflow :

Stride = (input_size//output_size)  
Kernel size = input_size - (output_size-1)*stride  
Padding = 0

Could u plz explain these three lines, I guess for second example stride is 1, is there any explicit way to mention stride, or is this the formula to calculate stride.

1 Like

For nn.AdaptiveAvgPool2d, you just specify the output size, stride will always be 1 (every possible grid will be used to achieve the output size):

torch.nn.AdaptiveAvgPool2d(output_size) according to AdaptiveAvgPool2d — PyTorch 1.9.0 documentation

The formulas above I believe is to calculate kernel size given input size, output size, and stride, (i.e. the size of the grids) :slight_smile:

Overall My answers are correct, actually there is no need for this much confusion here, The output size given in function only, which is required after avg pooling that’s it.

1 Like

Nice! Yea sorry if I misunderstood anything/made things confusing!

1 Like

Here the output shape indicates that every 5*14 grid is average to get this shape 2*2 ?
Am I right ?

Completely correct :slight_smile:

Also just noticed I linked to AvgPool2d rather than AdaptiveAvgPool2d: AdaptiveAvgPool2d — PyTorch 1.9.0 documentation. Sorry about that (just edited the reply)

The output size given in function only, which is required after avg pooling that’s it.

Exactly this, it’s the only argument we can specify! I’d imagine the stride is always 1 for the formulas mentioned above, i.e. every possible grid is used, like you said!

1 Like

Exactly :slight_smile:
Thanks @Epoching

1 Like

No problem, thanks for posting @krishna511, I learned something new haha!