class
GroupNorm
extends
ModuleGroupNorm(num_groups: int, num_channels: int, eps: float = 1e-05, affine: bool = True, device: DeviceLike = None, dtype: DTypeLike = None)Implementing kernel
C++ engine symbols that back this Python API.Group normalization over the channel dimension.
Divides the channels into num_groups contiguous groups
of size and normalises each group
independently over its spatial elements:
where and are the mean and variance computed over a single group (channels + spatial axes) for each sample in the batch.
Group Norm sits between two extremes: num_groups=1 recovers
Layer Norm (normalize over all channels at once), while
num_groups=num_channels recovers Instance Norm (each channel is
its own group). Unlike Batch Norm, Group Norm statistics are
independent of the batch size, making it stable for small batches
and well-suited to detection and segmentation models.
Parameters
num_groupsintNumber of groups to divide the channels into.
num_channels must be divisible by num_groups.num_channelsintTotal number of channels expected in the input.
epsfloat= 1e-05Small constant for numerical stability. Default:
1e-5.affinebool= TrueIf
True, learns per-channel scale and shift
of shape (num_channels,). Default: True.deviceDeviceLike= NoneDevice for the learnable parameters. Default:
None.dtypeDTypeLike= NoneData type of the learnable parameters. Default:
None.Attributes
Notes
- Input: where denotes zero or more spatial dimensions and .
- Output: same shape as the input.
num_channelsmust be divisible bynum_groups; aValueErroris raised at the functional level if this is violated.- Despite sharing a name with batch-norm affine parameters, the
weightandbiashere have shape(num_channels,)rather than being element-wise over the full normalized region.
Examples
32-channel input split into 8 groups:
>>> import lucid
>>> import lucid.nn as nn
>>> gn = nn.GroupNorm(num_groups=8, num_channels=32)
>>> x = lucid.randn(4, 32, 64, 64)
>>> out = gn(x)
>>> out.shape
(4, 32, 64, 64)
Layer-Norm equivalent (single group) on a 1-D sequence:
>>> gn_layer = nn.GroupNorm(num_groups=1, num_channels=128)
>>> x_seq = lucid.randn(16, 128, 200) # (N, C, L)
>>> out_seq = gn_layer(x_seq)
>>> out_seq.shape
(16, 128, 200)Used by 1
Constructors
1Instance methods
2Return a string representation of the layer's configuration.