torch.nn.AdaptiveMaxPool2d
class AdaptiveMaxPool2d extends Modulenew AdaptiveMaxPool2d(output_size: number | [number, number], options?: AdaptiveMaxPool2dOptions)
- readonly
output_size(number | [number, number]) - readonly
return_indices(boolean)
2D adaptive max pooling: reduces spatial dimensions to fixed output size with automatic kernel selection.
Applies max pooling with automatically computed kernel_size and stride to produce exact output_size (H_out × W_out), regardless of input size. Enables networks to accept variable image sizes. Essential for:
- Networks processing images of different sizes (without explicit resizing)
- Fixed feature representation before classification heads
- Transfer learning with different resolution inputs
- Building size-invariant computer vision systems
- Efficient spatial downsampling with guaranteed output dimensions
Most commonly used adaptive pooling layer. Output spatial dimensions are exactly specified, kernel/stride computed automatically for any input size.
When to use AdaptiveMaxPool2d:
- Images of varying resolutions (networks must accept multiple sizes)
- Transfer learning (fine-tune models on different image sizes)
- Fixed-size feature vectors before dense layers
- Variable-resolution input datasets
- Robust to image size variations
- Fixed output: Always produces exactly specified spatial dimensions
- Variable input: Works with any input size (computed dynamically)
- Common pattern: GlobalMaxPool (output_size=1) reduces to single value per channel
- Automatic kernel: No need to calculate kernel_size for different input sizes
- Transfer learning: Perfect for fine-tuning with different resolution inputs
- Information loss: Non-max values discarded within each region
- Uneven pooling: If input not divisible by output, regions vary
- Peak preservation: May amplify noise if max is noise-dominated
Examples
// Variable-size images to fixed feature size (7x7 is common)
const pool = new torch.nn.AdaptiveMaxPool2d([7, 7]); // Always output 7x7
// Different input sizes -> same 7x7 output
const x1 = torch.randn([32, 512, 224, 224]); // ImageNet-style
const y1 = pool.forward(x1); // [32, 512, 7, 7]
const x2 = torch.randn([32, 512, 448, 448]); // Higher resolution
const y2 = pool.forward(x2); // [32, 512, 7, 7] - same output!// Global max pooling (reduce to 1x1)
const pool = new torch.nn.AdaptiveMaxPool2d(1); // output_size = [1, 1]
const x = torch.randn([32, 2048, 7, 7]); // Feature maps
const y = pool.forward(x); // [32, 2048, 1, 1] - global max per channel// Transfer learning with varying image sizes
class FlexibleResNetHead extends torch.nn.Module {
avgpool: torch.nn.AdaptiveMaxPool2d;
fc: torch.nn.Linear;
constructor() {
super();
this.avgpool = new torch.nn.AdaptiveMaxPool2d(1);
this.fc = new torch.nn.Linear(2048, 1000);
}
forward(x: torch.Tensor): torch.Tensor {
x = this.avgpool.forward(x); // [B, 2048, 1, 1]
x = x.view(x.shape[0], -1); // [B, 2048]
x = this.fc.forward(x); // [B, 1000]
return x;
}
}