Skip to content
class

AdaptiveAvgPool2d

extendsModule
AdaptiveAvgPool2d(output_size: _OutSize)

Applies adaptive 2-D average pooling to produce a fixed output size.

For each spatial output position (h,w)(h, w) the window boundaries are derived from the input-to-output ratio:

hs=⌊h⋅HinHout⌋,he=⌈(h+1)HinHout⌉,ws=⌊w⋅WinWout⌋,we=⌈(w+1)WinWout⌉h_s = \left\lfloor \frac{h \cdot H_{in}}{H_{out}} \right\rfloor,\quad h_e = \left\lceil \frac{(h+1) H_{in}}{H_{out}} \right\rceil,\quad w_s = \left\lfloor \frac{w \cdot W_{in}}{W_{out}} \right\rfloor,\quad w_e = \left\lceil \frac{(w+1) W_{in}}{W_{out}} \right\rceil

and the output is:

y[n,c,h,w]=1(he−hs)(we−ws)∑i=hshe−1∑j=wswe−1x[n,c,i,j]y[n, c, h, w] = \frac{1}{(h_e - h_s)(w_e - w_s)} \sum_{i=h_s}^{h_e-1}\sum_{j=w_s}^{w_e-1} x[n, c, i, j]

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 HinH_{in} is not divisible by HoutH_{out}.

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)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(output_size: _OutSize)

Initialise the AdaptiveAvgPool2d module. See the class docstring for parameter semantics.

Instance methods

2
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor)

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.