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)
source

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]=max0kh<kH0kw<kWx ⁣[n,c,hsH+khdH,wsW+kwdW]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+2pHdH(kH1)1sH+1,Wout=Win+2pWdW(kW1)1sW+1H_{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
Not yet supported. 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)

Methods (3)

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)
source

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

fn

forward

Tensor
forward(x: Tensor)
source

Apply the pooling operation to the input tensor.

Parameters

inputTensor
Input tensor of shape (N,C,)(N, C, *) where * are the spatial dimensions appropriate for this pooling layer.

Returns

Tensor

Pooled output tensor.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.