class
AdaptiveAvgPool2d
extends
ModuleAdaptiveAvgPool2d(output_size: _Size2d)Applies adaptive 2-D average pooling to produce a fixed output size.
For each spatial output position the window boundaries are derived from the input-to-output ratio:
and the output is:
Parameters
output_sizeint or tuple[int, int]Target
(H_out, W_out). A scalar is broadcast to both
dimensions.Attributes
output_sizeint or tuple[int, int]Notes
- Input:
(N, C, H_in, W_in)(any spatial size) - Output:
(N, C, H_out, W_out)
- Global average pooling (GAP) is a special case with
output_size=(1, 1)and is the standard final pooling layer in modern classification backbones (ResNet, EfficientNet, etc.). - Unlike
AvgPool2d, the window dimensions may differ across output cells when is not divisible by .
Examples
Global average pooling for a classification head:
>>> import lucid
>>> import lucid.nn as nn
>>> gap = nn.AdaptiveAvgPool2d(output_size=1)
>>> x = lucid.ones((8, 2048, 7, 7))
>>> y = gap(x)
>>> y.shape
(8, 2048, 1, 1)
Resize feature maps to a fixed ``4 × 4`` grid:
>>> pool = nn.AdaptiveAvgPool2d(output_size=(4, 4))
>>> for h, w in [(14, 14), (28, 28), (56, 56)]:
... x = lucid.ones((2, 256, h, w))
... assert pool(x).shape == (2, 256, 4, 4)Methods (3)
dunder
__init__
→None__init__(output_size: _Size2d)Initialise the AdaptiveAvgPool2d 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.