torch.nn.functional.adaptive_max_pool2d
function adaptive_max_pool2d(input: Tensor, output_size: number | [number, number], options: AdaptiveMaxPoolFunctionalOptions & { return_indices: true }): PoolWithIndicesResultfunction adaptive_max_pool2d(input: Tensor, output_size: number | [number, number], options?: AdaptiveMaxPoolFunctionalOptions): Tensor | PoolWithIndicesResult2D Adaptive Max Pooling: takes max values to fixed spatial size automatically.
Applies adaptive max pooling over 2D spatial dimensions (height, width) with automatic kernel/stride computation. Optionally returns indices of max values. Useful for:
- Image classification: standardizing feature map sizes before classifier
- Transfer learning: adapting models to different image resolutions
- Feature extraction: preserving strongest activations with automatic sizing
- Unpooling: using returned indices to reconstruct higher-resolution features
- Variable-size handling: same network works with any input image size
- Salient feature pooling: keeping only the most important features
Unlike regular pooling, adaptive pooling automatically computes the kernel and stride to achieve the target output spatial size. Preserves maximum values in each adaptive window. Can return indices for unpooling in deconvolutional networks.
- Automatic kernel computation: Kernel/stride computed automatically
- Index preservation: return_indices=true enables feature reconstruction
- Input invariance: Same output spatial size regardless of input resolution
- Max preservation: Keeps strongest activations, useful for saliency
- Global max special case: output_size=1 gives global maximum per channel
- Index semantics: Returned indices correspond to flattened 2D positions
- Asymmetric kernels: May use different kernel sizes at different positions
- Unpooling requirement: Must save indices during pooling if unpooling later
Parameters
inputTensor- 4D input tensor of shape (batch, channels, height, width)
output_sizenumber | [number, number]- Target spatial size: single value for (size, size) or [height, width]
optionsAdaptiveMaxPoolFunctionalOptions & { return_indices: true }- Options for the operation. See
AdaptiveMaxPoolFunctionalOptions.
Returns
PoolWithIndicesResult– - If return_indices=false: Tensor with shape (batch, channels, out_height, out_width) - If return_indices=true: [pooled_tensor, indices_tensor] for unpoolingExamples
// Standard ImageNet pooling: global max before classifier
const features = torch.randn(32, 2048, 7, 7); // ResNet50 features
const pooled = torch.nn.functional.adaptive_max_pool2d(features, 1);
// Output: (32, 2048, 1, 1) - ready for classification head
// Variable input sizes: same network for different resolutions
const small_img = torch.randn(4, 512, 56, 56);
const out1 = torch.nn.functional.adaptive_max_pool2d(small_img, 7); // → (4, 512, 7, 7)
const large_img = torch.randn(4, 512, 112, 112); // 2x larger
const out2 = torch.nn.functional.adaptive_max_pool2d(large_img, 7); // → (4, 512, 7, 7)
// Both produce (4, 512, 7, 7) regardless of input resolution
// Feature extraction with indices for unpooling
const x = torch.randn(1, 256, 28, 28);
const [pooled, indices] = torch.nn.functional.adaptive_max_pool2d(x, 14, true);
// pooled: (1, 256, 14, 14) - max values
// indices: (1, 256, 14, 14) - positions for reconstruction
// Autoencoder: asymmetric feature preservation
const features = torch.randn(8, 512, 16, 16);
const compressed = torch.nn.functional.adaptive_max_pool2d(features, [8, 8]);
// Output: (8, 512, 8, 8) - half resolutionSee Also
- PyTorch torch.nn.functional.adaptive_max_pool2d
- adaptive_avg_pool2d - Average variant for smoothing
- max_pool2d - Regular 2D max pooling with explicit kernel/stride
- adaptive_max_pool1d - 1D variant for sequences
- adaptive_max_pool3d - 3D variant for volumetric data