Understand torch.nn.AdaptiveAvgPool2d() with Examples – PyTorch Tutorial

By | July 20, 2022

When we are using torch.nn.Conv2d() function, we may also use torch.nn.AdaptiveAvgPool2d(). In this tutorial, we will use some examples to show you how to use it.

torch.nn.AdaptiveAvgPool2d()

It is defined as:

torch.nn.AdaptiveAvgPool2d(output_size)

It will apply a 2D adaptive average pooling over an input.

As to parameter output_size, it can be:

output_size: int or tuple.

If output_size is an integer, it will be converted to (int, int). For example: output_size = 5, it will be converted to (5, 5)

The shape of output_size is (H, W). When output_size is an integer, which means H =W

How about the input and output of torch.nn.AdaptiveAvgPool2d()?

The input should be:  (N, C, H_in, W_in) or (C, H_in W_in). Here N is the batch_size, C is the channel.

The output = (N, C, S_o, S_1) or (C, S_0, S_1). Here S_0 = H, S_1 = W.

Comparing the input and output, we can find C is not changed.

Here we will use an example to show you how to use it.

import torch
N = 40
C_in = 40
H_in = 32
W_in = 32

inputs = torch.rand([N, C_in, H_in, W_in])

H = 5

x = torch.nn.AdaptiveAvgPool2d(H)
y = x(inputs)
print(y)
print(y.shape)

In this example, the ouput_size is an integer, which mean H=W.

Run this code, we will get:

torch.Size([40, 40, 5, 5])

We also can set different H and W.

H = 5
W = 10
x = torch.nn.AdaptiveAvgPool2d((H, W))
y = x(inputs)
print(y)
print(y.shape)

Run this code, we will get:

torch.Size([40, 40, 5, 10])

Leave a Reply