mr2.nn.GroupNorm
- class mr2.nn.GroupNorm[source]
Bases:
GroupNormA 32-bit GroupNorm with (optional) automatic group size selection.
Casts to float32 before calling the parent class to avoid instabilities in mixed precision training.
If
n_groupsis not provided, the number of groups is selected automatically as follows:start from
1group,try powers of two (
2, 4, 8, ...),keep the largest candidate that divides
n_channels,enforce at most
32groups and at least4channels per group.
This yields a stable default that stays close to common GroupNorm choices while adapting to small channel counts.
- __init__(n_channels: int, n_groups: int | None = None, affine: bool = False, features_last: bool = False)[source]
Initialize GroupNorm.
- Parameters:
n_channels (
int) – The number of channels in the input tensor.n_groups (
int|None, default:None) – The number of groups to use. If None, the number of groups is determined automatically as the largest power of 2 that dividesn_channels, is less than or equal to 32, and leaves at least 4 channels per group.affine (
bool, default:False) – Whether to use learnable affine parameters.features_last (
bool, default:False) – Whether the features are last in the input tensor, as common in transformer models, or in the second dimension, as common in CNNs.