Skip to Content
OchreML
Layers & Activations

BatchNorm2d

2D Batch Normalization over NCHW feature maps with learnable affine parameters and running statistics.

ochreml.nn.BatchNorm2d normalizes each channel across the batch and spatial dimensions (N, H, W) of an NCHW tensor, maintaining exponential moving averages in running_mean and running_var buffers.

Import & Signature

import ochreml.nn as nn

bn = nn.BatchNorm2d(
    num_features,
    eps=1e-5,
    momentum=0.1,
    affine=True,
    dtype="float32",
    device=None,
)

Parameters

ParameterTypeDefaultDescription
num_featuresintrequiredNumber of channels C in input (N, C, H, W).
epsfloat1e-5Numerical stability constant added to variance.
momentumfloat0.1EMA factor for updating running_mean and running_var during training.
affineboolTrueIf True, creates learnable weight (scale) and bias (shift) parameters.
dtypestr"float32"Parameter dtype.
devicestr | NoneNoneTarget device.

Attributes & Buffers

  • weight / bias: learnable Parameter tensors of shape (num_features,) (or None when affine=False).
  • running_mean / running_var: registered buffers exported in state_dict() and persisted in .dit when inside Sequential.

Usage Example

import ochreml.nn as nn

bn = nn.BatchNorm2d(16, eps=1e-5, momentum=0.1)
x = nn.Tensor.randn([8, 16, 16, 16])

bn.train()
y_train = bn(x)  # uses batch stats and updates running_mean / running_var

bn.eval()
with nn.no_grad():
    y_eval = bn(x)  # uses frozen running stats

On this page