fn
batch_norm
→Tensorbatch_norm(x: Tensor, running_mean: Tensor | None, running_var: Tensor | None, weight: Tensor | None = None, bias: Tensor | None = None, training: bool = False, momentum: float = 0.1, eps: float = 1e-05)Batch normalization (Ioffe & Szegedy, 2015).
Normalises each channel using statistics computed across the batch and all spatial axes, then applies a learnable per-channel affine transform. Acts as a strong regulariser and an enabler of higher learning rates by reducing "internal covariate shift".
Parameters
xTensorInput of shape
(N, C, *) with 2–5 dimensions:
(N, C), (N, C, L), (N, C, H, W), or
(N, C, D, H, W).running_meanTensor or NoneRunning mean buffer of shape
(C,); consulted in eval mode.running_varTensor or NoneRunning variance buffer of shape
(C,); consulted in eval mode.weightTensor= NonePer-channel scale of shape
(C,). Defaults to
ones (no scaling).biasTensor= NonePer-channel shift of shape
(C,). Defaults to
zeros (no shift).trainingbool= FalseWhen
True, statistics come from the current batch; when
False and running buffers are supplied, those are used.momentumfloat= 0.1Exponential-moving-average coefficient for the running buffers.
epsfloat= 1e-05Numerical safety added inside the square root.
Returns
TensorSame shape as x.
Notes
Math (over batch and spatial dims):
Dispatches internally by ndim to the matching engine kernel:
ndim==2 is treated as (N, C, 1); ndim==3 uses
batch_norm1d; ndim==4 uses the 2-D op; ndim==5 uses
batch_norm3d. Eval mode bypasses statistics computation
entirely and uses running_mean / running_var directly.
Examples
>>> import lucid
>>> from lucid.nn.functional import batch_norm
>>> x = lucid.randn(8, 16, 32, 32)
>>> rm = lucid.zeros(16); rv = lucid.ones(16)
>>> y = batch_norm(x, rm, rv, training=True)
>>> y.shape
(8, 16, 32, 32)