class
MaxPool2d
extends
ModuleMaxPool2d(kernel_size: _Size2d, stride: _Size2d | None = None, padding: _Size2d = 0, dilation: _Size2d = 1, return_indices: bool = False, ceil_mode: bool = False)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.
where is kernel_size, is
stride, and is dilation.
Output spatial sizes are:
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= NoneStep between successive windows. Defaults to
kernel_size.paddingint or tuple[int, int]= 0Zero-padding added to all four sides of the input. Default:
0.dilationint or tuple[int, int]= 1Spacing between kernel elements. Default:
1.return_indicesbool= FalseNot yet supported. Default:
False.ceil_modebool= FalseUse ceiling instead of floor for output size. Default:
False.Attributes
kernel_sizeint or tuple[int, int]strideint or tuple[int, int] or Nonepaddingint or tuple[int, int]dilationint or tuple[int, int]ceil_modeboolNotes
- 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)Initialise the MaxPool2d module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor)Apply the pooling operation to the input tensor.
Parameters
inputTensorInput tensor of shape where are the
spatial dimensions appropriate for this pooling layer.
Returns
TensorPooled output tensor.
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.