Skip to content
class

AvgPool2d

extendsModule
AvgPool2d(kernel_size: _Size2d, stride: _Size2d | None = None, padding: _Size2d = 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.

Applies 2-D average pooling over a spatial feature map.

For each output position the module computes the arithmetic mean of the values in a kernel_size window:

y[n,c,h,w]=1∣W∣∑kh=0kH−1∑kw=0kW−1x ⁣[n, c, h⋅sH+kh, w⋅sW+kw]y[n, c, h, w] = \frac{1}{|W|} \sum_{k_h=0}^{k_H-1} \sum_{k_w=0}^{k_W-1} x\!\left[n,\, c,\, h \cdot s_H + k_h,\, w \cdot s_W + k_w\right]

where ∣W∣|W| is the effective window area (governed by count_include_pad).

Output spatial sizes:

Hout=⌊Hin+2pH−kHsH+1⌋,Wout=⌊Win+2pW−kWsW+1⌋H_{out} = \left\lfloor \frac{H_{in} + 2p_H - k_H}{s_H} + 1 \right\rfloor, \quad W_{out} = \left\lfloor \frac{W_{in} + 2p_W - k_W}{s_W} + 1 \right\rfloor

Parameters

kernel_sizeint or tuple[int, int]
Size of the averaging window.
strideint or tuple[int, int] or None= None
Step between successive windows. Defaults to kernel_size.
paddingint or tuple[int, int]= 0
Zero-padding added to all four sides. Default: 0.
ceil_modebool= False
Use ceiling instead of floor for output sizes. Default: False.
count_include_padbool= True
Count zero-padded values in the denominator. Default: True.
divisor_overrideint or None= None
Divide each window's sum by this, never 0, instead of its size.

Attributes

kernel_sizeint or tuple[int, int]
strideint or tuple[int, int] or None
paddingint or tuple[int, int]
ceil_modebool
count_include_padbool
divisor_overrideint or None

Notes

  • Input: (N, C, H_in, W_in)
  • Output: (N, C, H_out, W_out)
  • Average pooling is differentiable everywhere and provides a smooth spatial summary, unlike max pooling which has sub-gradient issues at ties.
  • A kernel_size equal to the full spatial extent is called global average pooling (GAP) and is widely used as the penultimate layer in image classification networks.

Examples

Standard 2× downsampling:
>>> import lucid
>>> import lucid.nn as nn
>>> pool = nn.AvgPool2d(kernel_size=2, stride=2)
>>> x = lucid.ones((1, 64, 56, 56))
>>> y = pool(x)
>>> y.shape
(1, 64, 28, 28)
Smoothing with padding (output same size as input):
>>> pool = nn.AvgPool2d(kernel_size=3, stride=1, padding=1)
>>> x = lucid.ones((4, 32, 16, 16))
>>> y = pool(x)
>>> y.shape
(4, 32, 16, 16)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(kernel_size: _Size2d, stride: _Size2d | None = None, padding: _Size2d = 0, ceil_mode: bool = False, count_include_pad: bool = True, divisor_override: int | None = None)

Initialise the AvgPool2d 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.