fn

fractional_max_pool2d

Tensor or (Tensor, Tensor)
fractional_max_pool2d(x: Tensor, 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, _random_samples: Tensor | None = None)
source

Fractional max-pooling over a 2-D input (Graham, 2014).

Performs max-pooling with a non-integer effective stride. Instead of a uniform window grid, random per-axis offsets pick "slightly irregular" window boundaries, yielding a smoother downsampling than the abrupt 2× reductions of standard max-pool. Acts as a structural regulariser for image classifiers.

Parameters

xTensor
Input of shape (N, C, H, W).
kernel_sizeint or (int, int)
Size of the pooling window.
output_sizeint or (int, int)= None
Exact desired output spatial shape. Mutually exclusive with output_ratio.
output_ratiofloat or (float, float)= None
Multiplicative shrinkage factor per axis (e.g. 0.5 halves each dim). Mutually exclusive with output_size.
return_indicesbool= False
When True, also return per-output-cell argmax positions (flattened H × W index) as a second tensor.
_random_samplesTensor= None
Per-instance uniform samples of shape (N, C, 2) driving the window-boundary draws. Defaults to fresh lucid.rand.

Returns

Tensor or (Tensor, Tensor)

Output of shape (N, C, oH, oW); when return_indices, also the argmax indices of matching shape.

Notes

Window starts are drawn from Graham's formula (§3 of the paper)

α=HkHout1,starti=(i+u)αuα\alpha = \frac{H - k}{H_{\text{out}} - 1},\quad \text{start}_i = \lfloor (i + u) \alpha \rfloor - \lfloor u \alpha \rfloor

where uu is a per-(sample, channel) uniform sample. The final start clamps to H - k so all windows lie inside the input. Implementation is a pure-Python composite — gradients flow through Tensor.max.

Examples

>>> import lucid
>>> from lucid.nn.functional import fractional_max_pool2d
>>> x = lucid.randn(1, 4, 14, 14)
>>> y = fractional_max_pool2d(x, kernel_size=2, output_ratio=0.5)
>>> y.shape
(1, 4, 7, 7)