Spatial downsampling
Downsample convolutional feature maps while retaining channel identity.
PyTorch · Class
Applies 2D average pooling to spatial dimensions while preserving the number of channels. Its window, stride, padding, boundary rounding, padding inclusion, and divisor are configurable.
AvgPool2d slides an averaging window over the spatial height and width of a 3D or 4D input. The window advances by stride, optionally uses implicit zero padding, and produces one averaged value per window; output dimensions follow the documented floor-or-ceiling shape calculation.
class torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True, divisor_override=None)| Name | Type | Description |
|---|---|---|
kernel_size | int or tuple of int | Size of the pooling window; an integer or a two-element tuple (height, width). |
stride | int or tuple of int, optional | Window step; defaults to kernel_size. |
padding | int or tuple of int, optional | Implicit zero padding on both sides; default 0. |
ceil_mode | bool, optional | Use ceiling rather than floor in output-size calculation; default False. |
count_include_pad | bool, optional | Include zero padding in the averaging calculation; default True. |
divisor_override | int, optional | Replacement divisor for the average; default None. |
A tensor shaped (N, C, H_out, W_out) for batched input or (C, H_out, W_out) for unbatched input.
import torch
from torch import nn
pool = nn.AvgPool2d(kernel_size=2, stride=2)
x = torch.tensor([[[[1., 2., 3., 4.],
[5., 6., 7., 8.],
[9., 10., 11., 12.],
[13., 14., 15., 16.]]]])
y = pool(x)
print(y)
# tensor([[[[ 3.5000, 5.5000],
# [11.5000, 13.5000]]]])Creates a 2×2 average-pooling layer with stride 2 and applies it to a one-channel 4×4 tensor.
Downsample convolutional feature maps while retaining channel identity.
Reduce local activation variation by replacing each region with its mean.
Aggregate variable spatial regions before a classifier or prediction head.
AvgPool2d applies a 2D average over each sliding kernel window. It accepts unbatched input shaped (C, H, W) or batched input shaped (N, C, H, W), preserving the channel count while changing the spatial dimensions.
Follow-up: How is the output height computed? It uses the documented kernel, padding, stride, and ceil_mode formula; ceil_mode selects ceiling rather than floor for the quotient.
kernel_size specifies the pooling-window dimensions. stride specifies the window step and defaults to kernel_size when omitted; padding adds implicit zero padding, and count_include_pad controls whether those zeros participate in the average.
Follow-up: What happens when height and width need different settings? Supply two-element tuples, such as kernel_size=(2, 3) and stride=(2, 1).
The layer returns a tensor with the same channel structure as the input: (N, C, H_out, W_out) for batched input or (C, H_out, W_out) for unbatched input. H_out and W_out are determined by the pooling configuration.
Follow-up: How can the denominator be customized? Set divisor_override to an integer; otherwise the pooling-region size is used, subject to the documented padding behavior.
torch.nn.functional.avg_pool2dtorch.nn.AdaptiveAvgPool2dtorch.nn.MaxPool2d