Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.nn.AdaptiveAvgPool2d function

PyTorch torch.nn.AdaptiveAvgPool2d function

PyTorch torch.nn Reference Manual PyTorch torch.nn Reference Manual


torch.nn.AdaptiveAvgPool2d is the adaptive average pooling module in PyTorch.

It can pool an input feature map of any size to a specified target size, without manually calculating kernel_size and stride.

Function definition



torch.nn.AdaptiveAvgPool2d(output_size)

Parameter Description:


Usage examples

Example 1: Basic usage

Pool the feature map to a fixed size:

Example

import

torch

import

torch.

nn

as

nn

Pool to 1x1

adaptive_pool

=

nn.

AdaptiveAvgPool2d

(

1

)

Inputs of different sizes

x1

=

torch.

randn

(

1

,

64

,

32

,

32

)

x2

=

torch.

randn

(

1

,

64

,

16

,

16

)

x3

=

torch.

randn

(

1

,

64

,

8

,

8

)

out1

=

adaptive_pool

(

x1

)

out2

=

adaptive_pool

(

x2

)

out3

=

adaptive_pool

(

x3

)

print

(

"32x32 ->"

,

out1.

shape

)

print

(

"16x16 ->"

,

out2.

shape

)

print

(

"8x8   ->"

,

out3.

shape

)

print

(

"nAll outputs are pooled to 1x1"

)

Example 2: Arbitrary output size

Pool to a non-square shape:

Example

import

torch

import

torch.

nn

as

nn

Pool to 4x4

pool

=

nn.

AdaptiveAvgPool2d

(

(

4

,

4

)

)

x

=

torch.

randn

(

2

,

128

,

32

,

32

)

out

=

pool

(

x

)

print

(

"Input shape:"

,

x.

shape

)

print

(

"Output shape:"

,

out.

shape

)

 

(2, 128, 4, 4)

Pool to 1x7 (useful for sequences)

pool_seq

=

nn.

AdaptiveAvgPool2d

(

(

1

,

7

)

)

x_seq

=

torch.

randn

(

2

,

64

,

10

,

20

)

out_seq

=

pool_seq

(

x_seq

)

print

(

"nSequence pooling:"

,

x_seq.

shape

,

"->"

,

out_seq.

shape

)

Example 3: Use in a CNN

Typical global average pooling:

Example

import

torch

import

torch.

nn

as

nn

class

CNNWithGAP

(

nn.

Module

)

:

   

def

init

(

self

,

num_classes

=

10

)

:

       

super

(

CNNWithGAP

,

self

)

.

init

(

)

       

self

.

features

=

nn.

Sequential

(

            nn.

Conv2d

(

3

,

64

,

3

,

padding

=

1

)

,

            nn.

ReLU

(

)

,

            nn.

MaxPool2d

(

2

)

,

            nn.

Conv2d

(

64

,

128

,

3

,

padding

=

1

)

,

            nn.

ReLU

(

)

,

            nn.

MaxPool2d

(

2

)

,

            nn.

Conv2d

(

128

,

256

,

3

,

padding

=

1

)

,

            nn.

ReLU

(

)

,

            nn.

MaxPool2d

(

2

)

,

       

)

       

Global average pooling

       

self

.

avgpool

=

nn.

AdaptiveAvgPool2d

(

1

)

       

self

.

classifier

=

nn.

Linear

(

256

,

num_classes

)

   

def

forward

(

self

,

x

)

:

        x

=

self

.

features

(

x

)

        x

=

self

.

avgpool

(

x

)

        x

=

x.

view

(

x.

size

(

0

)

,

1

)

        x

=

self

.

classifier

(

x

)

       

return

x

model

=

CNNWithGAP

(

)

x

=

torch.

randn

(

4

,

3

,

32

,

32

)

output

=

model

(

x

)

print

(

"Input shape:"

,

x.

shape

)

print

(

"Output shape:"

,

output.

shape

)

print

(

"nFeature maps are globally pooled into a single vector"

)

Example 4: Comparison with regular pooling

Example

import

torch

import

torch.

nn

as

nn

Adaptive pooling vs. regular pooling

adaptive

=

nn.

AdaptiveAvgPool2d

(

2

)

regular

=

nn.

AvgPool2d

(

kernel_size

=

16

,

stride

=

16

)

x

=

torch.

randn

(

1

,

64

,

32

,

32

)

out_adaptive

=

adaptive

(

x

)

out_regular

=

regular

(

x

)

print

(

"Input:"

,

x.

shape

)

print

(

"Adaptive pooling (output 2x2):"

,

out_adaptive.

shape

)

print

(

"Regular average pooling (16x16):"

,

out_regular.

shape

)


Adaptive pooling vs. regular pooling

Type Advantages Applicable scenarios
AdaptiveAvgPool Arbitrary input size Inputs at different scales
AvgPool Faster computation Fixed input size

Common questions

Q1: When to use adaptive pooling?

When the input size is not fixed, or when you need to standardize to a fixed size.

Q2: What is Global Average Pooling?

AdaptiveAvgPool2d(1) pools each feature map into a single value.


Use Cases

Tip: Global average pooling can significantly reduce the number of parameters.


PyTorch torch.nn Reference Manual PyTorch torch.nn Reference Manual