Skip to content

MaxPool2d

MaxPool2d is a Module wrapper around the functional max_pool2d op. It down-samples spatial feature maps by retaining the maximum activation in each pooling window, reducing computational cost and providing a degree of spatial invariance. It holds no learnable parameters and is typically placed after a Conv2d layer in a CNN.

import simplegrad as sg
import simplegrad.nn as nn

pool = nn.MaxPool2d(kernel_size=2, stride=2)
x = sg.normal((1, 16, 28, 28), requires_grad=True)
out = pool(x)  # shape: (1, 16, 14, 14)

MaxPool2d

Bases: Module

2D max pooling layer.

Slides a window over the input and keeps the maximum value in each window.

Parameters:

  • kernel_size (int | tuple) –

    Pooling window size. Int or (kH, kW).

  • stride (int | tuple) –

    Step between windows. Int or (sH, sW).

  • pad_width (int | tuple[int], default: 0 ) –

    Padding. Int (all sides) or (top, bottom, left, right). Defaults to 0.

  • pad_mode (str, default: 'constant' ) –

    Padding mode. Defaults to "constant".

  • pad_value (int, default: 0 ) –

    Fill value for constant padding. Defaults to 0.

Attributes

Attribute Type Description
.kernel_size int \| tuple Size of the pooling window.
.stride int \| tuple Step size of the sliding window.
.padding int \| tuple Zero-padding added to both spatial dimensions.

Methods

Method Description
.forward() Apply max-pooling to the input feature map.

Inherits all methods from Module: .parameters(), .submodules(), .to_device(), .summary(), .set_train_mode(), .set_eval_mode().