Skip to content
class

MaxPool2d

extendsModule
MaxPool2d(kernel_size: _Size2d, stride: _Size2d | None = None, padding: _Size2d = 0, dilation: _Size2d = 1, return_indices: bool = False, ceil_mode: bool = False)

Implementing kernel

C++ engine symbols that back this Python API.

Applies 2-D max pooling over a spatial feature map.

For each position in the output, the module selects the maximum value from a rectangular kernel_size window applied over the height and width dimensions of the input.

y[n,c,h,w]=max⁡0≤kh<kH0≤kw<kWx ⁣[n, c, h⋅sH+kh⋅dH, w⋅sW+kw⋅dW]y[n, c, h, w] = \max_{\substack{0 \le k_h < k_H \\ 0 \le k_w < k_W}} x\!\left[n,\, c,\, h \cdot s_H + k_h \cdot d_H,\, w \cdot s_W + k_w \cdot d_W\right]

where (kH,kW)(k_H, k_W) is kernel_size, (sH,sW)(s_H, s_W) is stride, and (dH,dW)(d_H, d_W) is dilation.

Output spatial sizes are:

Hout=⌊Hin+2pH−dH(kH−1)−1sH+1⌋,Wout=⌊Win+2pW−dW(kW−1)−1sW+1⌋H_{out} = \left\lfloor \frac{H_{in} + 2p_H - d_H(k_H - 1) - 1}{s_H} + 1 \right\rfloor, \quad W_{out} = \left\lfloor \frac{W_{in} + 2p_W - d_W(k_W - 1) - 1}{s_W} + 1 \right\rfloor

Parameters

kernel_sizeint or tuple[int, int]
Size of the pooling window. A single int is broadcast to (kernel_size, kernel_size).
strideint or tuple[int, int] or None= None
Step between successive windows. Defaults to kernel_size.
paddingint or tuple[int, int]= 0
Zero-padding added to all four sides of the input. Default: 0.
dilationint or tuple[int, int]= 1
Spacing between kernel elements. Default: 1.
return_indicesbool= False
If True, returns (output, indices). Default: False.
ceil_modebool= False
Use ceiling instead of floor for output size. Default: False.

Attributes

kernel_sizeint or tuple[int, int]
strideint or tuple[int, int] or None
paddingint or tuple[int, int]
dilationint or tuple[int, int]
ceil_modebool

Notes

  • Input: (N, C, H_in, W_in)
  • Output: (N, C, H_out, W_out)
  • Max pooling is translation-equivariant and provides a form of local invariance to small spatial shifts.
  • When dilation > 1, the effective receptive field grows without extra memory cost (atrous / dilated pooling).
  • A common configuration for a 2× spatial downsampling is MaxPool2d(kernel_size=2, stride=2).

Examples

Halving spatial resolution:
>>> import lucid
>>> import lucid.nn as nn
>>> pool = nn.MaxPool2d(kernel_size=2, stride=2)
>>> x = lucid.ones((1, 3, 32, 32))
>>> y = pool(x)
>>> y.shape
(1, 3, 16, 16)
Asymmetric kernel with padding:
>>> pool = nn.MaxPool2d(kernel_size=(3, 5), stride=1, padding=(1, 2))
>>> x = lucid.ones((2, 16, 24, 24))
>>> y = pool(x)
>>> y.shape
(2, 16, 24, 24)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(kernel_size: _Size2d, stride: _Size2d | None = None, padding: _Size2d = 0, dilation: _Size2d = 1, return_indices: bool = False, ceil_mode: bool = False)

Initialise the MaxPool2d module. See the class docstring for parameter semantics.

Instance methods

2
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.