torch.nn.functional.max_pool1d
function max_pool1d(input: Tensor, kernel_size: number | [number], options?: MaxPool1dFunctionalOptions): Tensorfunction max_pool1d(input: Tensor, kernel_size: number | [number], stride: number, padding: number, dilation: number, ceil_mode: boolean, options?: MaxPool1dFunctionalOptions): Tensor1D Max Pooling: downsamples sequences by taking maximum values.
Applies max pooling over a 1D sequence dimension (length) using sliding windows. Takes the maximum value in each window, useful for:
- Feature extraction: preserves strongest signal peaks (important events in time series)
- Dimensionality reduction: reduces sequence length while retaining key features
- Audio processing: extracting phoneme-level features from frame-level signals
- NLP: pooling over word embeddings or character sequences
- Temporal downsampling: reducing computational cost for RNNs and Transformers
- Anomaly detection: highlighting extreme values in monitoring data
Operates on 3D inputs: (batch, channels, length). Each of the batch * channels features
is pooled independently along the length dimension.
- Preserves signal peaks: Max pooling emphasizes local maxima, useful for detecting important features like edges or sudden changes in time series
- Stride == kernel: With stride=kernel_size (default), creates non-overlapping windows
- Output shape: Smaller stride creates more output positions (overlapping windows)
- Different from average: Max preserves spikes while average smooths; choose based on task
- Gradient computation: Only the maximum element in each window receives gradient
- Receptive field: Window size controls how much context each output sees
- Information loss: Pooling discards non-maximum values - can lose information
- Boundary effects: Padding affects how boundaries are handled
Parameters
inputTensor- 3D input tensor of shape (batch, channels, length)
kernel_sizenumber | [number]- Size of the pooling window (single value for 1D)
optionsMaxPool1dFunctionalOptionsoptional
Returns
Tensor– Tensor with shape (batch, channels, out_length) where: out_length = floor((length + 2*padding - kernel_size) / stride) + 1Examples
// Simple 1D pooling: reduce sequence length by half
const seq = torch.randn(8, 64, 100); // Batch of 8, 64 features, length 100
const pooled = torch.nn.functional.max_pool1d(seq, 2); // kernel=2, stride=2
// Output shape: (8, 64, 50)
// Audio feature extraction: pool frames to phoneme level
const frames = torch.randn(1, 128, 200); // 200 frames, 128 mel-bins
const phonemes = torch.nn.functional.max_pool1d(frames, 5, 5); // 5-frame pooling
// Pool 5 consecutive frames into single phoneme representation
// NLP: preserve important words in sequence
const embeddings = torch.randn(32, 300, 50); // 32 sequences, 300-dim embeddings, length 50
const reduced = torch.nn.functional.max_pool1d(embeddings, 2, 2);
// Extract strongest activations, reduce to 25 word positions
// Time series with overlapping windows
const timeseries = torch.randn(4, 32, 1000); // 4 series, 32 channels, 1000 timesteps
const downsampled = torch.nn.functional.max_pool1d(timeseries, 10, 5);
// kernel=10, stride=5 creates overlapping pooling windowsSee Also
- PyTorch torch.nn.functional.max_pool1d
- avg_pool1d - Alternative that averages instead of taking max (smoothing effect)
- max_pool2d - 2D variant for spatial data
- adaptive_max_pool1d - Adaptive variant with automatic kernel/stride calculation