DEV Community

Cover image for AdaptiveAvgPool2d in PyTorch
Ambarish Ganguly
Ambarish Ganguly

Posted on

4 3

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay