torch.nn.functional.adaptive_avg_pool1d
1D Adaptive Average Pooling: averages to fixed output size automatically.
Applies adaptive average pooling that automatically computes kernel and stride sizes to achieve the desired output size. Useful for:
- Network design: handle variable input sizes without manual kernel calculation
- Image/sequence classification: pools to fixed feature size before classification layer
- Global feature extraction: reduces to single output (global average pooling)
- Transfer learning: adapting pre-trained networks to different input sizes
- Building size-invariant models: networks work with any input length
- Feature standardization: consistent feature vector size regardless of input
Unlike regular pooling where you specify kernel/stride, adaptive pooling automatically calculates them to achieve the target output size. Smooths by taking averages.
- Automatic kernel computation: No manual kernel/stride specification needed
- Input invariance: Works with any input length, always produces desired output
- Global pooling special case: output_size=1 gives global average
- Smoothing effect: Averages values like regular pooling but with automatic sizing
- Gradient distribution: Gradients distributed based on the adaptive windows
- Output size constraints: Should not exceed input length
- Automatic sizing: Kernel size computed automatically; may not be symmetric
- Different from regular pooling: Don't specify kernel/stride, specify desired output
Parameters
inputTensor- 3D input tensor of shape (batch, channels, length)
output_sizenumber | [number]- Target output length (single value or [length])
Returns
Tensor– Tensor with shape (batch, channels, output_size) containing averaged valuesExamples
// Global average pooling: reduce to single value per channel
const seq = torch.randn(8, 64, 100); // Variable length sequence
const pooled = torch.nn.functional.adaptive_avg_pool1d(seq, 1);
// Output: (8, 64, 1) - global average of each channel
// Fixed output size: handle variable input lengths
const seq1 = torch.randn(4, 128, 87); // Input length 87
const fixed1 = torch.nn.functional.adaptive_avg_pool1d(seq1, 10); // → (4, 128, 10)
const seq2 = torch.randn(4, 128, 200); // Different length 200
const fixed2 = torch.nn.functional.adaptive_avg_pool1d(seq2, 10); // → (4, 128, 10)
// Both produce (4, 128, 10) regardless of input length
// Audio feature extraction: pooling to consistent length
const audio_frames = torch.randn(1, 80, 312); // Audio features, 312 frames
const pooled_audio = torch.nn.functional.adaptive_avg_pool1d(audio_frames, 50);
// Output: (1, 80, 50) - 50 fixed output positions
// Feature pyramid reduction: prepare features for classification
const deep_features = torch.randn(32, 512, 7); // Deep CNN features
const cls_features = torch.nn.functional.adaptive_avg_pool1d(deep_features, 1);
// Output: (32, 512, 1) - ready for fully connected layersSee Also
- PyTorch torch.nn.functional.adaptive_avg_pool1d
- adaptive_max_pool1d - Max variant instead of averaging
- avg_pool1d - Regular 1D average pooling with explicit kernel/stride
- adaptive_avg_pool2d - 2D adaptive average pooling variant