fn
group_norm
→Tensorgroup_norm(x: Tensor, num_groups: int, weight: Tensor | None = None, bias: Tensor | None = None, eps: float = 1e-05)Group normalization (Wu & He, 2018).
Splits the channel dimension into num_groups contiguous groups
and normalises each (sample, group) slice independently across
its channels and spatial axes. Combines the spatial reduction of
BatchNorm with the per-sample stability of LayerNorm — performance
is therefore largely independent of batch size, which makes it the
go-to choice for detection / segmentation models trained with very
small batches.
Parameters
xTensorInput of shape
(N, C, *spatial) where C must be
divisible by num_groups.num_groupsintNumber of channel groups. Two limiting cases:
num_groups == C reduces to InstanceNorm; num_groups == 1 reduces to
LayerNorm over channels + spatial axes.weightTensor= NonePer-channel scale of shape
(C,).biasTensor= NonePer-channel shift of shape
(C,).epsfloat= 1e-05Numerical safety added inside the square root.
Returns
TensorSame shape as x.
Notes
Math (let be the channel set of group and the spatial axes):
Independence from batch size avoids the train/eval mismatch that BatchNorm requires running buffers to fix.
Examples
>>> import lucid
>>> from lucid.nn.functional import group_norm
>>> x = lucid.randn(2, 32, 16, 16)
>>> y = group_norm(x, num_groups=8)
>>> y.shape
(2, 32, 16, 16)