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
| Parameter | Type | Default | Description |
|---|---|---|---|
num_features | int | required | Number of channels C in input (N, C, H, W). |
eps | float | 1e-5 | Numerical stability constant added to variance. |
momentum | float | 0.1 | EMA factor for updating running_mean and running_var during training. |
affine | bool | True | If True, creates learnable weight (scale) and bias (shift) parameters. |
dtype | str | "float32" | Parameter dtype. |
device | str | None | None | Target device. |
Attributes & Buffers
weight/bias: learnableParametertensors of shape(num_features,)(orNonewhenaffine=False).running_mean/running_var: registered buffers exported instate_dict()and persisted in.ditwhen insideSequential.
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