class
FractionalMaxPool2d
extends
ModuleFractionalMaxPool2d(kernel_size: int | tuple[int, int], output_size: int | tuple[int, int] | None = None, output_ratio: float | tuple[float, float] | None = None, return_indices: bool = False)Applies fractional max-pooling over a 2-D spatial feature map.
Implements the random fractional pooling scheme of Graham (2014). Instead of a fixed integer stride, the spatial extent is divided by randomly sampled fractional boundaries, so the effective downsampling ratio is a random variable in during training.
For a given random partition of the height axis into intervals and the width axis into intervals :
The boundaries are drawn independently per batch-channel pair from
_random_samples (or internally sampled when not provided).
Parameters
kernel_sizeint or tuple[int, int]Size of the max pooling window applied at each sampled position.
output_sizeint or tuple[int, int] or None= NoneFixed target output size
(H_out, W_out). Exactly one of
output_size and output_ratio must be specified.output_ratiofloat or tuple[float, float] or None= NoneTarget output size as a fraction of the input size. Each value
must lie in . Exactly one of
output_size and
output_ratio must be specified.return_indicesbool= FalseIf
True, also return the flat indices of the selected maxima.
Default: False.Attributes
kernel_sizeint or tuple[int, int]output_sizeint or tuple[int, int] or Noneoutput_ratiofloat or tuple[float, float] or Nonereturn_indicesboolNotes
- Input:
(N, C, H_in, W_in) - Output:
(N, C, H_out, W_out)
- Because the boundaries are random, each forward pass produces a different spatial partition during training. This acts as a form of stochastic spatial data augmentation and can improve generalisation.
- At inference (
model.eval()), the same random draw is applied; if deterministic output is required, fix the random seed before calling forward. - Original reference: B. Graham, "Fractional Max-Pooling", arXiv 1412.6071, 2014.
- Implemented as a pure-Python composite using
lucid.nn.functional.pooling.fractional_max_pool2d; gradients flow throughTensor.maxautomatically.
Examples
Downsample to a fixed target size:
>>> import lucid
>>> import lucid.nn as nn
>>> pool = nn.FractionalMaxPool2d(kernel_size=2, output_size=(7, 7))
>>> x = lucid.ones((1, 64, 14, 14))
>>> y = pool(x)
>>> y.shape
(1, 64, 7, 7)
Downsample by a ratio (approximately halve each dimension):
>>> pool = nn.FractionalMaxPool2d(kernel_size=2, output_ratio=0.5)
>>> x = lucid.ones((2, 32, 20, 20))
>>> y = pool(x)
>>> y.shape[0], y.shape[1]
(2, 32)Methods (2)
dunder
__init__
→None__init__(kernel_size: int | tuple[int, int], output_size: int | tuple[int, int] | None = None, output_ratio: float | tuple[float, float] | None = None, return_indices: bool = False)Initialise the FractionalMaxPool2d 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.