class
AdaptiveAvgPool1d
extends
ModuleAdaptiveAvgPool1d(output_size: int | tuple[int, ...])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
the window boundaries are:
so that .
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)(anyL_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, ...])Initialise the AdaptiveAvgPool1d 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.