class

AvgPool2d

extendsModule
AvgPool2d(kernel_size: _Size2d, stride: _Size2d | None = None, padding: _Size2d = 0, ceil_mode: bool = False, count_include_pad: bool = True)
source

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]=1Wkh=0kH1kw=0kW1x ⁣[n,c,hsH+kh,wsW+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+2pHkHsH+1,Wout=Win+2pWkWsW+1H_{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.

Attributes

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

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)

Methods (3)

dunder

__init__

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

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

fn

forward

Tensor
forward(x: Tensor)
source

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.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.