class
AvgPool2d
extends
ModuleAvgPool2d(kernel_size: _Size2d, stride: _Size2d | None = None, padding: _Size2d = 0, ceil_mode: bool = False, count_include_pad: bool = True)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:
where is the effective window area (governed by
count_include_pad).
Output spatial sizes:
Parameters
kernel_sizeint or tuple[int, int]Size of the averaging window.
strideint or tuple[int, int] or None= NoneStep between successive windows. Defaults to
kernel_size.paddingint or tuple[int, int]= 0Zero-padding added to all four sides. Default:
0.ceil_modebool= FalseUse ceiling instead of floor for output sizes. Default:
False.count_include_padbool= TrueCount zero-padded values in the denominator. Default:
True.Attributes
kernel_sizeint or tuple[int, int]strideint or tuple[int, int] or Nonepaddingint or tuple[int, int]ceil_modeboolcount_include_padboolNotes
- 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_sizeequal 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)Initialise the AvgPool2d module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor)Apply the pooling operation to the input tensor.
Parameters
inputTensorInput tensor of shape where are the
spatial dimensions appropriate for this pooling layer.
Returns
TensorPooled output tensor.
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.