fn

avg_pool2d

Tensor
avg_pool2d(x: Tensor, kernel_size: int | tuple[int, int], stride: int | tuple[int, int] | None = None, padding: int | tuple[int, int] = 0, ceil_mode: bool = False, count_include_pad: bool = True, divisor_override: int | None = None)
source

2-D average pooling over a sliding window.

Replaces each spatial window with its arithmetic mean. Common as the last spatial reduction before a fully connected classifier (modern nets often use adaptive_avg_pool2d for the final pool because it adapts to arbitrary input sizes).

Parameters

xTensor
Input of shape (N, C, H, W).
kernel_sizeint or (int, int)
Size of the pooling window.
strideint or (int, int)= None
Window step. Defaults to kernel_size.
paddingint or (int, int)= 0
Implicit zero-padding on each spatial side.
ceil_modebool= False
Use ceil instead of floor in the output-size formula.
count_include_padbool= True
When True, padding cells contribute to the denominator.
divisor_overrideint= None
Explicit denominator overriding |R| (rare; useful for symmetric / weight-style averages).

Returns

Tensor

Output of shape (N, C, H_out, W_out) where each dim obeys

Hout=H+2pHkHsH+1H_{\text{out}} = \left\lfloor \frac{H + 2 p_H - k_H}{s_H} + 1 \right\rfloor

Notes

Math:

yi,c,h,w=1R(m,n)Rxi,c,sHh+m,sWw+ny_{i,c,h,w} = \frac{1}{|R|} \sum_{(m, n) \in R} x_{i,\,c,\,s_H h + m,\,s_W w + n}

Average-pool's gradient is the uniform distribution 1/R1/|R| over the window, which produces smoother backward signals than max-pool.

Examples

>>> import lucid
>>> from lucid.nn.functional import avg_pool2d
>>> x = lucid.randn(1, 8, 28, 28)
>>> y = avg_pool2d(x, kernel_size=2)
>>> y.shape
(1, 8, 14, 14)