torch.nn.AdaptiveAvgPool2d
class AdaptiveAvgPool2d extends Modulenew AdaptiveAvgPool2d(output_size: number | [number, number])
- readonly
output_size(number | [number, number])
2D adaptive average pooling: reduces spatial dimensions to fixed output size with smooth averaging.
Applies average pooling with automatically computed kernel/stride to produce exact output_size (H_out × W_out), regardless of input size. Smoother than max pooling, retains more information. Most commonly used adaptive pooling for global average pooling in classification heads. Essential for:
- Global average pooling before classification (most common use case)
- Fixed-size feature representation from variable-size images
- Smooth spatial downsampling preserving overall structure
- Transfer learning with different image resolutions
When to use AdaptiveAvgPool2d:
- Images of varying resolutions needing fixed feature size
- Global average pooling (output_size=1) in classification heads (very common)
- Noise reduction while preserving spatial structure
- Transfer learning with flexible input sizes
- Feature aggregation before dense layers
- Fixed output: Always produces exactly specified spatial dimensions
- Smooth averaging: Retains more information than MaxPool Global pooling: output_size=1 is standard in modern CNNs (ResNet, EfficientNet, etc.)
- Variable input: Works with any input size (automatic kernel computation)
- Information preservation: Better than max pooling for overall feature summary
- Gradient flow: All spatial values contribute to gradient
- Information loss: Spatial information compressed to output_size
- Uneven regions: Regions may vary if input not divisible by output
Examples
// Global average pooling (most common: output_size=1)
const pool = new torch.nn.AdaptiveAvgPool2d(1);
const x = torch.randn([32, 2048, 7, 7]); // Feature maps
const y = pool.forward(x); // [32, 2048, 1, 1] - global average per channel
const flat = y.view(y.shape[0], -1); // [32, 2048] - ready for classification// Variable-size images to fixed intermediate feature size
const pool = new torch.nn.AdaptiveAvgPool2d([7, 7]);
const x1 = torch.randn([32, 512, 224, 224]); // ImageNet
const y1 = pool.forward(x1); // [32, 512, 7, 7]
const x2 = torch.randn([32, 512, 448, 448]); // Higher res
const y2 = pool.forward(x2); // [32, 512, 7, 7] - same output!// ResNet-style classification head with adaptive average pooling
class ResNetClassifier extends torch.nn.Module {
features: torch.nn.Sequential; // ResNet backbone
avgpool: torch.nn.AdaptiveAvgPool2d;
fc: torch.nn.Linear;
constructor() {
super();
// ... features set up ...
this.avgpool = new torch.nn.AdaptiveAvgPool2d(1); // Global average
this.fc = new torch.nn.Linear(2048, 1000);
}
forward(x: torch.Tensor): torch.Tensor {
x = this.features.forward(x); // Extract features
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;
}
}