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

PyTorch torch.nn.MaxPool2d Function

PyTorch nn.MaxPool2d Function

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


torch.nn.MaxPool2d is a module in PyTorch for two-dimensional max pooling.

Pooling layers can reduce the spatial size of feature maps, lower computational cost, and provide a degree of translation invariance.

Function definition



torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)

Parameter Description:


Usage examples

Example 1: Basic usage

Create a max pooling layer:

Example

import

torch

import

torch.

nn

as

nn

Create a max pooling layer: window 2x2, stride 2

max_pool

=

nn.

MaxPool2d

(

kernel_size

=

2

,

stride

=

2

)

Create an input tensor

input_tensor

=

torch.

randn

(

1

,

1

,

4

,

4

)

print

(

"Input:n"

,

input_tensor.

squeeze

(

)

.

tolist

(

)

)

Forward propagation

output

=

max_pool

(

input_tensor

)

print

(

"nOutput:n"

,

output.

squeeze

(

)

.

tolist

(

)

)

print

(

"nInput shape:"

,

input_tensor.

shape

)

print

(

"Output shape:"

,

output.

shape

)

The output is:



输入:

[[-0.4128, 0.2341, -0.9876, 0.4567],

 [ 0.1234, 0.8765, -0.2345, 0.6789],

 [-0.5678, 0.3456, 0.7890, -0.1234],

 [ 0.9012, -0.4567, 0.2345, 0.5678]]



输出:

[[0.8765, 0.6789],

 [0.9012, 0.7890]]



输入形状: torch.Size([1, 1, 4, 4])

输出形状: torch.Size([1, 1, 2, 2])

As you can see, the maximum value in each 2x2 window is retained.

Example 2: Different kernel_size and stride

Adjust the pooling parameters:

Example

import

torch

import

torch.

nn

as

nn

3x3 pooling with stride 1 (non-overlapping)

pool3x3

=

nn.

MaxPool2d

(

kernel_size

=

3

,

stride

=

1

)

Non-square pooling

pool_rect

=

nn.

MaxPool2d

(

kernel_size

=

(

2

,

3

)

,

stride

=

(

2

,

3

)

)

input_tensor

=

torch.

randn

(

1

,

1

,

6

,

9

)

print

(

"Input shape:"

,

input_tensor.

shape

)

print

(

"3x3 pooling output:"

,

pool3x3

(

input_tensor

)

.

shape

)

print

(

"Rectangular pooling output:"

,

pool_rect

(

input_tensor

)

.

shape

)

Example 3: Using padding

Edge padding can preserve edge information to some extent:

Example

import

torch

import

torch.

nn

as

nn

Pooling with padding

pool_padding

=

nn.

MaxPool2d

(

kernel_size

=

3

,

stride

=

1

,

padding

=

1

)

input_tensor

=

torch.

randn

(

1

,

1

,

4

,

4

)

output

=

pool_padding

(

input_tensor

)

print

(

"Input shape:"

,

input_tensor.

shape

)

print

(

"Output shape (with padding):"

,

output.

shape

)

Example 4: Returning indices

Using return_indices allows positions to be restored in the decoder:

Example

import

torch

import

torch.

nn

as

nn

Create a pooling layer that returns indices

pool_indices

=

nn.

MaxPool2d

(

kernel_size

=

2

,

stride

=

2

,

return_indices

=

True

)

input_tensor

=

torch.

randn

(

1

,

1

,

4

,

4

)

output

,

indices

=

pool_indices

(

input_tensor

)

print

(

"Output shape:"

,

output.

shape

)

print

(

"Index shape:"

,

indices.

shape

)

print

(

"Index values:"

,

indices.

squeeze

(

)

.

tolist

(

)

)

Example 5: Usage in a CNN

The position of pooling layers in a typical CNN architecture:

Example

import

torch

import

torch.

nn

as

nn

class

SimpleCNN

(

nn.

Module

)

:

   

def

init

(

self

)

:

       

super

(

SimpleCNN

,

self

)

.

init

(

)

       

Convolutional layers

       

self

.

conv1

=

nn.

Conv2d

(

3

,

32

,

kernel_size

=

3

,

padding

=

1

)

       

self

.

conv2

=

nn.

Conv2d

(

32

,

64

,

kernel_size

=

3

,

padding

=

1

)

       

self

.

relu

=

nn.

ReLU

(

)

       

Pooling layer: the size is halved each time

       

self

.

pool

=

nn.

MaxPool2d

(

2

,

2

)

   

def

forward

(

self

,

x

)

:

        x

=

self

.

relu

(

self

.

conv1

(

x

)

)

 

32x32

        x

=

self

.

pool

(

x

)

             

16x16

        x

=

self

.

relu

(

self

.

conv2

(

x

)

)

 

16x16

        x

=

self

.

pool

(

x

)

             

8x8

       

return

x

model

=

SimpleCNN

(

)

input_image

=

torch.

randn

(

1

,

3

,

32

,

32

)

output

=

model

(

input_image

)

print

(

"Input shape:"

,

input_image.

shape

)

print

(

"Output shape:"

,

output.

shape

)


Max pooling vs average pooling

Type Formula Feature Applicable scenarios
MaxPool2d max(region) Retains salient features and is more robust to noise Image classification, object detection (commonly used)
AvgPool2d mean(region) Smooths features and preserves background information Global average pooling, feature extraction

Common questions

Q1: Can pooling layers be removed?

Modern networks such as ResNet and DenseNet tend to use smaller-stride convolutions instead of pooling, but pooling is still commonly used for fast downsampling.

Q2: What is the relationship between stride and kernel_size?

When stride = kernel_size, pooling windows do not overlap; when stride < kernel_size, pooling windows overlap.


Use Cases

nn.MaxPool2d Main application scenarios include:


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