class

FractionalMaxPool2d

extendsModule
FractionalMaxPool2d(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)
source

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 (0,1)(0, 1) during training.

For a given random partition of the height axis into HoutH_{out} intervals [rh(i),rh(i+1))[r_h^{(i)}, r_h^{(i+1)}) and the width axis into WoutW_{out} intervals [rw(j),rw(j+1))[r_w^{(j)}, r_w^{(j+1)}):

y[n,c,i,j]=maxh[rh(i),rh(i+1)),  w[rw(j),rw(j+1))x[n,c,h,w]y[n, c, i, j] = \max_{h \in [r_h^{(i)},\, r_h^{(i+1)}),\; w \in [r_w^{(j)},\, r_w^{(j+1)})} x[n, c, h, w]

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= None
Fixed 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= None
Target output size as a fraction of the input size. Each value must lie in (0,1)(0, 1). Exactly one of output_size and output_ratio must be specified.
return_indicesbool= False
If 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 None
output_ratiofloat or tuple[float, float] or None
return_indicesbool

Notes

  • 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 through Tensor.max automatically.

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

Initialise the FractionalMaxPool2d 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.