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

PyTorch · Class

nn.BatchNorm2d

torch.nn.BatchNorm2d normalizes four-dimensional N×C×H×W inputs independently per channel. It normally tracks running statistics for evaluation and can apply learnable channel-wise scale and bias.

How it works

BatchNorm2d normalizes each channel of a four-dimensional tensor across its batch and spatial positions. In training it normally updates per-channel running mean and variance; evaluation then uses those stored estimates, while learnable scale and offset parameters provide the default affine transformation.

Syntax

class torch.nn.modules.batchnorm.BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True, device=None, dtype=None, *, bias=True)

Parameters

NameTypeDescription
num_featuresintNumber of input channels C in the expected N×C×H×W tensor.
epsfloatSmall value added to the denominator for numerical stability; default 1e-5.
momentumfloat or NoneRunning-statistics update factor; None selects a cumulative moving average. Default 0.1.
affineboolWhether to learn affine scale and offset parameters. Default True.
track_running_statsboolWhether to retain running mean and variance; default True.
devicetorch.device or NoneOptional device for module state initialization.
dtypetorch.dtype or NoneOptional data type for module state initialization.
biasboolWhether to learn an additive bias when affine is enabled. Default True.

Returns

A tensor with the same shape as the input, normalized channel-wise.

Key points

  • Use this module for four-dimensional image-like tensors rather than arbitrary-rank inputs.
  • The conventional layout is N×C×H×W: batch, channels, height, and width.
  • Each channel gets its own statistics, gathered across batch and spatial coordinates.
  • momentum determines how the layer updates its stored per-channel running statistics.
  • With affine enabled, the normalized channels receive learnable scale and offset parameters.
  • eps protects the normalization calculation against problematic small variances.

Common mistakes

  • With track_running_stats=False, evaluation also uses statistics from the current batch rather than stored running estimates.
  • Setting momentum=None changes running-statistics updates to a cumulative moving average instead of the default exponential-style update.
  • bias=False removes the learnable additive offset; this option matters only when affine=True.

Example: Normalize image batches

import torch
from torch import nn

layer = nn.BatchNorm2d(3)
x = torch.randn(4, 3, 8, 8)
y = layer(x)
print(y.shape)  # torch.Size([4, 3, 8, 8])

Creates a three-channel spatial batch-normalization layer and applies it to a batch of four 8×8 images.

When to use it

Normalize feature maps after convolution

Convolutional neural networks

Stabilize channel-wise activations in N×C×H×W data

Image and video tensors

Reuse training-time running statistics during evaluation

Inference pipelines

Interview questions

QWhat input does BatchNorm2d normalize, and over which dimensions?

It expects input shaped (N, C, H, W), where num_features must equal C. Statistics are computed independently for each channel over the corresponding N, H, and W values.

Follow-up: What changes in evaluation mode? By default, evaluation uses the running mean and variance accumulated during training; disabling running statistics makes it use batch statistics instead.

QHow does BatchNorm2d behave differently during training and evaluation?

During training, the module normally maintains running estimates of each channel's mean and variance. In evaluation mode, those estimates are used to normalize inputs.

Follow-up: How can evaluation use current-batch statistics? Construct the layer with track_running_stats=False, so running estimates are not kept.

QWhat are the roles of eps, momentum, and affine?

eps is added to the denominator to improve numerical stability. momentum controls the running_mean and running_var update, and None selects a cumulative moving average.

Follow-up: What does affine control? When true, the layer has learnable per-channel scale and bias; bias=False suppresses only the additive bias.

  • torch.nn.BatchNorm1d
  • torch.nn.BatchNorm3d
  • torch.nn.LazyBatchNorm2d

Source: BatchNorm2d — PyTorch 2.11 documentation. Checked 2026-10-16.