DEV Community

Cover image for AdaptiveAvgPool2d in PyTorch
Ambarish Ganguly
Ambarish Ganguly

Posted on

AdaptiveAvgPool2d in PyTorch

I had trouble understanding the AdaptiveAvgPool2d function in PyTorch. The following examples helped me to teach myself better. Hopefully, somebody may benefit from this.

Example 1

import torch
import torch.nn as nn
import numpy as np

m = nn.AdaptiveAvgPool2d((1))

x = np.array(
[
    [ 2. , 3.],
    [ 4. , 1.],

])

input = torch.tensor(x)
print(input)

output = m(input)
print(output)
print(torch.mean(input))
Enter fullscreen mode Exit fullscreen mode

The output will be equal to torch.mean(input)

Example 2 with a 3 x 3 x 3 tensor

x = np.array(
[
    [
        [ 2. , 3. , 2.],
        [ 2. , 3. , 2.],
        [ 2. , 3. , 2.],

    ],

    [
        [ 1. , 4. , 5.],
        [ 1. , 4. , 5.],
        [ 1. , 4. ,  5. ],

    ],

    [
        [ 7. , 3. , 2.],
        [ 7. , 3. , 2.],
        [ 7. , 3. , 2.],

    ]

])
Enter fullscreen mode Exit fullscreen mode

This is a 3 x 3 x 3 array


input = torch.tensor(x)

m = nn.AdaptiveAvgPool2d((2))

output = m(input)

print(output)

Enter fullscreen mode Exit fullscreen mode

image

Let's investigate why the 1st element is 2.5

We take a 2 x 2 tensor out of the 3 x 3 x 3 tensor and take the mean and see that it is 2.5

x2 = torch.tensor(np.array([2. , 3. , 2. , 3.]))
torch.mean(x2)
Enter fullscreen mode Exit fullscreen mode

image

Example 3

We see that the 6th element is 4.5. How is this calculated?

image

We take the mean of the following section

image

x3 = torch.tensor(np.array([ 4.0 , 5. , 4. , 5.]))
torch.mean(x3)
Enter fullscreen mode Exit fullscreen mode

Example 4 with a 4 x 3 x 3 tensor


x = np.array(
[
    [
        [ 2. , 3. , 2.],
        [ 2. , 3. , 2.],
        [ 2. , 3. , 2.],

    ],

    [
        [ 1. , 4. , 5.],
        [ 1. , 4. , 5.],
        [ 1. , 4. ,  5. ],

    ],

    [
        [ 7. , 3. , 2.],
        [ 7. , 3. , 2.],
        [ 7. , 3. , 2.],

    ],

    [
        [ 8. , 3. , 2.],
        [ 8. , 3. , 2.],
        [ 8. , 3. , 2.],

    ]

])

input = torch.tensor(x)
print(input)
print(input.shape)
Enter fullscreen mode Exit fullscreen mode

This is a 4 x 3 x 3 tensor

image


m = nn.AdaptiveAvgPool2d([1,1])
output= m(input)
print(output)
Enter fullscreen mode Exit fullscreen mode

image

Let us explore why the first element is 2.3333

x4 = torch.tensor(
    np.array( [ 2. , 3. , 2.,  2. , 3. , 2.,  2. , 3. , 2.])
                 )
print(torch.mean(x4))
Enter fullscreen mode Exit fullscreen mode

image

Discussion (0)