torch.nn.AdaptiveMaxPool1d
class AdaptiveMaxPool1d extends Modulenew AdaptiveMaxPool1d(output_size: number, options?: AdaptiveMaxPool1dOptions)
- readonly
output_size(number) - readonly
return_indices(boolean)
1D adaptive max pooling: reduces sequence to fixed output size with automatic kernel selection.
Applies max pooling with automatically computed kernel_size and stride to produce exact output_size, regardless of input size. Enables networks to accept variable-length inputs. Essential for:
- Networks processing variable-length sequences (text, audio, time series)
- Handling different input lengths without explicit resizing
- Fixed output representation before classification/regression
- Temporal feature extraction with fixed output size
- Avoiding manual kernel/stride computation for adaptive downsampling
The Problem Adaptive Pooling Solves: Regular MaxPool1d requires kernel_size and stride which depend on input length. Adaptive pooling computes these automatically: given input length and desired output_size, it calculates the optimal kernel and stride. Perfect for sequences of varying length.
When to use AdaptiveMaxPool1d:
- Variable-length input sequences (time series, audio clips, text embeddings)
- When output size must be fixed (before dense layers)
- Temporal feature extraction with size-independent output
- Avoiding manual downsampling factor computation
- Networks needing fixed feature vector from variable-length input
Algorithm: Given input_size and output_size:
- kernel_size = input_size / output_size (computed per chunk)
- stride = kernel_size
- Automatically creates non-overlapping pooling windows
- Produces exactly output_size elements
- Fixed output: Always produces output_size elements
- Variable input: Works with any input_size (computed dynamically)
- Automatic kernel: No need to calculate kernel_size manually
- Non-overlapping: Automatically non-overlapping pooling
- Max operation: Preserves maximum values per region
- Deterministic: Same input always produces same output
- Information loss: Non-max values discarded within each pool region
- Uneven regions: If input_size not divisible by output_size, regions vary
- Peak preservation: May amplify noise if max is noise-dominated
Examples
// Variable-length sequence to fixed size
const pool = new torch.nn.AdaptiveMaxPool1d(10); // Always output length 10
// Input 1: length 100 -> output 10
const x1 = torch.randn([32, 64, 100]);
const y1 = pool.forward(x1); // [32, 64, 10]
// Input 2: length 50 -> output still 10 (automatically adjusted kernel)
const x2 = torch.randn([32, 64, 50]);
const y2 = pool.forward(x2); // [32, 64, 10] - same output size!// Text embedding processing with variable sequence lengths
const embedding = new torch.nn.Embedding(1000, 64);
const adaptive_pool = new torch.nn.AdaptiveMaxPool1d(1); // Global max pooling
const x = embedding.forward(torch.randint(0, 1000, [32, 50])); // Variable text lengths
const pooled = adaptive_pool.forward(x); // [32, 64, 1] - fixed global representation// RNN input preprocessing with adaptive pooling
const sequence = torch.randn([64, 128, 200]); // [batch, features, time]
const pool = new torch.nn.AdaptiveMaxPool1d(50);
const fixed_seq = pool.forward(sequence); // [64, 128, 50] - normalized temporal dimension