fn

adaptive_avg_pool2d

Tensor
adaptive_avg_pool2d(x: Tensor, output_size: int | tuple[int, int])
source

2-D adaptive average pooling — produces a fixed (H, W).

Computes per-axis kernel / stride so the output shape exactly matches output_size regardless of the input spatial size. The workhorse "global pool to fixed grid" used by virtually every modern classification network (output_size=(1, 1) collapses the spatial axes entirely).

Parameters

xTensor
Input of shape (N, C, H, W).
output_sizeint or (int, int)
Desired spatial shape of the output. Scalar duplicates per axis; (1, 1) performs global average pooling.

Returns

Tensor

Output of shape (N, C, oH, oW).

Notes

For each output cell (i,j)(i, j) the corresponding input window is

[iH/Hout,(i+1)H/Hout)×[jW/Wout,(j+1)W/Wout)\big[\lfloor i H / H_{\text{out}} \rfloor, \lceil (i + 1) H / H_{\text{out}} \rceil\big) \times \big[\lfloor j W / W_{\text{out}} \rfloor, \lceil (j + 1) W / W_{\text{out}} \rceil\big)

Foundational to Spatial Pyramid Pooling and the "global average pool + linear classifier" pattern introduced by NIN / ResNet-style architectures.

Examples

>>> import lucid
>>> from lucid.nn.functional import adaptive_avg_pool2d
>>> x = lucid.randn(1, 512, 7, 9)
>>> y = adaptive_avg_pool2d(x, output_size=(1, 1))
>>> y.shape
(1, 512, 1, 1)