Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

nn.AvgPool2d

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.

How it works

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.

Syntax

class torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True, divisor_override=None)

Parameters

NameTypeDescription
kernel_sizeint or tuple of intSize of the pooling window; an integer or a two-element tuple (height, width).
strideint or tuple of int, optionalWindow step; defaults to kernel_size.
paddingint or tuple of int, optionalImplicit zero padding on both sides; default 0.
ceil_modebool, optionalUse ceiling rather than floor in output-size calculation; default False.
count_include_padbool, optionalInclude zero padding in the averaging calculation; default True.
divisor_overrideint, optionalReplacement divisor for the average; default None.

Returns

A tensor shaped (N, C, H_out, W_out) for batched input or (C, H_out, W_out) for unbatched input.

Key points

  • AvgPool2d performs spatial average pooling independently across each input channel.
  • Kernel size, stride, and padding may be integers or two-element tuples for separate height and width values.
  • Padding adds zeros around the spatial input before pooling.
  • divisor_override lets callers replace the default averaging divisor.

Common mistakes

  • With ceil_mode enabled, output sizing can include windows that extend beyond the input boundary.

Example: Average-pool a feature map

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.

When to use it

Spatial downsampling

Downsample convolutional feature maps while retaining channel identity.

Local smoothing

Reduce local activation variation by replacing each region with its mean.

Feature aggregation

Aggregate variable spatial regions before a classifier or prediction head.

Interview questions

QWhat does AvgPool2d do, and what input shapes does it accept?

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.

QExplain the main spatial parameters and their defaults.

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).

QWhat shape does AvgPool2d return?

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_pool2d
  • torch.nn.AdaptiveAvgPool2d
  • torch.nn.MaxPool2d

Source: AvgPool2d. Checked 2026-10-16.