Skip to content
fn

avg_pool2d

→Tensor
avg_pool2d(input: 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)

Implementing kernel

C++ engine symbols that back this Python API.

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

inputTensor
Input of shape (N, C, H, W), or (C, H, W) unbatched.
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
Divide each window's sum by this instead of |R| (rare; useful for symmetric / weight-style averages). Must not be 0.

Returns

Tensor

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

Hout=⌊H+2pH−kHsH+1⌋H_{\text{out}} = \left\lfloor \frac{H + 2 p_H - k_H}{s_H} + 1 \right\rfloor

Notes

Math:

yi,c,h,w=1∣R∣∑(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/∣R∣1/|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)

Used by 2