class

AdaptiveAvgPool1d

extendsModule
AdaptiveAvgPool1d(output_size: int | tuple[int, ...])
source

Applies adaptive 1-D average pooling to produce a fixed output length.

Rather than specifying kernel_size and stride manually, you provide the desired output length and the module automatically derives the window parameters for each output position. For output index ii the window boundaries are:

start(i)=iLinLout,end(i)=(i+1)LinLout\text{start}(i) = \left\lfloor \frac{i \cdot L_{in}}{L_{out}} \right\rfloor, \quad \text{end}(i) = \left\lceil \frac{(i+1) \cdot L_{in}}{L_{out}} \right\rceil

so that y[n,c,i]=mean(x[n,c,start(i):end(i)])y[n, c, i] = \text{mean}\bigl(x[n, c, \text{start}(i):\text{end}(i)]\bigr).

Parameters

output_sizeint or tuple[int, ...]
Desired output length. Pass None inside a tuple to keep that dimension unchanged.

Attributes

output_sizeint or tuple[int, ...]

Notes

  • Input: (N, C, L_in) (any L_in)
  • Output: (N, C, output_size)
  • Adaptive pooling decouples the network architecture from the input size, which is particularly valuable when fine-tuning models on data with spatial dimensions different from the original training set.
  • The window sizes may be non-uniform across output positions; all averages are still exact (no overlap or gap).

Examples

Compress any sequence to length 1 (global average pooling):
>>> import lucid
>>> import lucid.nn as nn
>>> gap = nn.AdaptiveAvgPool1d(output_size=1)
>>> x = lucid.ones((4, 512, 100))
>>> y = gap(x)
>>> y.shape
(4, 512, 1)
Downsample to a fixed length regardless of input:
>>> pool = nn.AdaptiveAvgPool1d(output_size=8)
>>> for l in [32, 64, 128]:
...     x = lucid.ones((1, 16, l))
...     assert pool(x).shape == (1, 16, 8)

Methods (3)

dunder

__init__

None
__init__(output_size: int | tuple[int, ...])
source

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