fn

adaptive_avg_pool1d

Tensor
adaptive_avg_pool1d(x: Tensor, output_size: int | tuple[int, ...])
source

1-D adaptive average pooling — produces a fixed output length.

Computes kernel / stride dynamically so that the output's spatial length equals output_size regardless of input length. Handy when feeding variable-length sequences into a head expecting a fixed embedding size.

Parameters

xTensor
Input of shape (N, C, L).
output_sizeint or tuple of int
Desired length of the output spatial axis.

Returns

Tensor

Output of shape (N, C, output_size).

Notes

For each output index ii, the corresponding input window is

[iL/Lout,  (i+1)L/Lout)\big[\lfloor i \cdot L / L_{\text{out}} \rfloor,\; \lceil (i+1) \cdot L / L_{\text{out}} \rceil\big)

and the cell is the mean over that window. When L divides evenly by L_out the operation reduces to plain avg_pool1d with kernel == stride == L / L_out; otherwise Lucid falls back to a per-slot Python-level computation that still keeps all data on the original device.

Examples

>>> import lucid
>>> from lucid.nn.functional import adaptive_avg_pool1d
>>> x = lucid.randn(2, 32, 13)
>>> y = adaptive_avg_pool1d(x, output_size=5)
>>> y.shape
(2, 32, 5)