ml.models.norms
Defines general-purpose helper functions for initializing norm layers.
from ml.models.norms import get_norm_linear, get_norm_1d, get_norm_2d, get_norm_3d, cast_norm_type
linear = nn.Sequential(nn.Linear(32, 32), get_norm_linear("layer", dim=32))
conv_1d = nn.Sequential(nn.Conv1d(32, 32, 3), get_norm_1d("layer", dim=32, groups=4))
conv_2d = nn.Sequential(nn.Conv2d(32, 32, 3), get_norm_2d("layer", dim=32, groups=4))
conv_3d = nn.Sequential(nn.Conv3d(32, 32, 3), get_norm_3d("layer", dim=32, groups=4))
# This lets you parametrize the norm type as a string.
linear = nn.Sequential(nn.Linear(32, 32), get_norm_linear(cast_norm_type(my_norm), dim=32))
Choices for the norm type are:
"no_norm"
: No normalization"batch"
or"batch_affine"
: Batch normalization"instance"
or"instance_affine"
: Instance normalization"group"
or"group_affine"
: Group normalization"layer"
or"layer_affine"
: Layer normalization
Note that instance norm and group norm are not available for linear layers.
- ml.models.norms.cast_norm_type(s: str) Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'] [source]
- ml.models.norms.cast_parametrize_norm_type(s: str) Literal['no_norm', 'weight', 'spectral'] [source]
- class ml.models.norms.LastBatchNorm(channels: int, momentum: float = 0.99, affine: bool = True, eps: float = 0.0001, device: device | None = None, dtype: dtype | None = None)[source]
Bases:
Module
Applies batch norm along final dimension without transposing the tensor.
The normalization is pretty simple, it basically just tracks the running mean and variance for each channel, then normalizes each channel to have a unit normal distribution.
- Input:
x: Tensor with shape (…, N)
- Output:
The tensor, normalized by the running mean and variance
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- mean: Tensor
- var: Tensor
- forward(x: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class ml.models.norms.ConvLayerNorm(channels: int, *, dims: int | None = None, eps: float = 1e-05, elementwise_affine: bool = True, device: device | None = None, dtype: dtype | None = None)[source]
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(inputs: Tensor) Tensor [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- ml.models.norms.get_norm_1d(norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'], *, dim: int | None = None, groups: int | None = None, device: device | None = None, dtype: dtype | None = None) Module [source]
Returns a normalization layer for tensors with shape (B, C, T).
- Parameters:
norm – The norm type to use
dim – The number of dimensions in the input tensor
groups – The number of groups to use for group normalization
device – The device to use for the layer
dtype – The dtype to use for the layer
- Returns:
A normalization layer
- Raises:
NotImplementedError – If norm is not a valid 1D norm type
- ml.models.norms.get_norm_2d(norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'], *, dim: int | None = None, groups: int | None = None, device: device | None = None, dtype: dtype | None = None) Module [source]
Returns a normalization layer for tensors with shape (B, C, H, W).
- Parameters:
norm – The norm type to use
dim – The number of dimensions in the input tensor
groups – The number of groups to use for group normalization
device – The device to use for the layer
dtype – The dtype to use for the layer
- Returns:
A normalization layer
- Raises:
NotImplementedError – If norm is not a valid 2D norm type
- ml.models.norms.get_norm_3d(norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'], *, dim: int | None = None, groups: int | None = None, device: device | None = None, dtype: dtype | None = None) Module [source]
Returns a normalization layer for tensors with shape (B, C, D, H, W).
- Parameters:
norm – The norm type to use
dim – The number of dimensions in the input tensor
groups – The number of groups to use for group normalization
device – The device to use for the layer
dtype – The dtype to use for the layer
- Returns:
A normalization layer
- Raises:
NotImplementedError – If norm is not a valid 3D norm type
- ml.models.norms.get_norm_linear(norm: Literal['no_norm', 'batch', 'batch_affine', 'instance', 'instance_affine', 'group', 'group_affine', 'layer', 'layer_affine'], *, dim: int | None = None, device: device | None = None, dtype: dtype | None = None) Module [source]
Returns a normalization layer for tensors with shape (B, …, C).
- Parameters:
norm – The norm type to use
dim – The number of dimensions in the input tensor
device – The device to use for the layer
dtype – The dtype to use for the layer
- Returns:
A normalization layer
- Raises:
NotImplementedError – If norm is not a valid linear norm type
- ml.models.norms.get_parametrization_norm(module: T_module, norm: Literal['no_norm', 'weight', 'spectral'], *, name: str = 'weight', n_power_iterations: int = 1, eps: float = 1e-12, weight_dim: int = 0, spectral_dim: int | None = None) T_module [source]
Returns a parametrized version of the module.
- Parameters:
module – The module to parametrize
norm – The parametrization norm type to use
name – The name of the parameter to use for the parametrization; this should reference the name on the module (for instance,
weight
for ann.Linear
module)n_power_iterations – The number of power iterations to use for spectral normalization
eps – The epsilon value to use for spectral normalization
weight_dim – The dimension of the weight parameter to normalize when using weight normalization
spectral_dim – The dimension of the weight parameter to normalize when using spectral normalization
- Returns:
The parametrized module