class

AvgPool1d

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

Applies 1-D average pooling over a sequence.

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

y[n,c,i]=1Wk=0ks1x ⁣[n,c,is+k]y[n, c, i] = \frac{1}{|W|} \sum_{k=0}^{k_s - 1} x\!\left[n,\, c,\, i \cdot s + k\right]

where W|W| is the effective window size (see count_include_pad) and ss is stride.

The output length follows:

Lout=Lin+2pkss+1L_{out} = \left\lfloor \frac{L_{in} + 2p - k_s}{s} + 1 \right\rfloor

Parameters

kernel_sizeint
Size of the averaging window.
strideint or None= None
Step between successive windows. Defaults to kernel_size.
paddingint= 0
Zero-padding added to both ends of the input before pooling. Default: 0.
ceil_modebool= False
Use ceiling instead of floor for the output length. Default: False.
count_include_padbool= True
If True (default), zero-padded values are counted in the denominator. If False, only real input values are averaged.

Attributes

kernel_sizeint
strideint or None
paddingint
ceil_modebool
count_include_padbool

Notes

  • Input: (N, C, L_in)
  • Output: (N, C, L_out)
  • When count_include_pad=True the denominator always equals kernel_size, even for border windows that overlap the padding. Setting count_include_pad=False can produce slightly higher values near the boundaries.
  • Average pooling is commonly used in sequence models to obtain a fixed-size summary of variable-length inputs.

Examples

Non-overlapping average pooling:
>>> import lucid
>>> import lucid.nn as nn
>>> pool = nn.AvgPool1d(kernel_size=4)
>>> x = lucid.ones((1, 8, 16))
>>> y = pool(x)
>>> y.shape
(1, 8, 4)
Overlapping pooling with padding, excluding pad from average:
>>> pool = nn.AvgPool1d(kernel_size=3, stride=1, padding=1,
...                     count_include_pad=False)
>>> x = lucid.ones((2, 4, 10))
>>> y = pool(x)
>>> y.shape
(2, 4, 10)

Methods (3)

dunder

__init__

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

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