How the function torch.nn.AdaptiveAvgPool2d(output_size) works?

I have tried torch.nn.AdaptiveAvgPool2d(output_size) as follow:

m = nn.AdaptiveAvgPool2d((4,1))
a=torch.randn(1, 64, 7, 9)
output = m(input)

then I got the following results:

output[0,1,0]=torch.mean(a[0,1,0:2])
output[0,1,1]=torch.mean(a[0,1,1:4])
output[0,1,2]=torch.mean(a[0,1,3:6])
output[0,1,3]=torch.mean(a[0,1,5:7])

Do anyone know how the function torch.nn.AdaptiveAvgPool2d(output_size) works? Or can someone tell me where I can find the source code of torch.nn.AdaptiveAvgPool2d(output_size)?

1 Like

Hi,

The doc can be found here and the cpu implementation here.

how do i use a linear layer after the AdaptiveAvgPool2d layer?

Consider this example:

m = nn.AdaptiveAvgPool2d((5,7))
dense = nn.Linear(5, 10)

input = torch.randn(1, 64, 8, 9)

output = m2(input) # its shape should be (1, 64, 5, 7)
output2 = dense(output) # this line throws an error.

Please help me with this. Thanks in advance ! :slight_smile:

Answered here.