Layer Normalization (LayerNorm)#
API#
- class warp_nn.modules.layers.LayerNorm(
- normalized_shape: int | tuple[int, ...],
- *,
- eps: float = 1e-05,
- elementwise_affine: bool = True,
- bias: bool = True,
- initialize_parameters: bool = True,
- requires_grad: bool = True,
Bases:
ModuleApply Layer Normalization over the trailing dimensions of the input.
\[\text{LayerNorm}(x) = \frac{x - \text{E}[x]}{\sqrt{\text{Var}[x] + \epsilon}} \, \gamma + \beta\]The mean and the (biased) variance are computed over the last
len(normalized_shape)dimensions.
Learnable parameters:
Name
Shape
Description
\(\gamma\)
weightnormalized_shapeWeights. Only if
elementwise_affineis true\(\beta\)
biasnormalized_shapeBias. Only if
elementwise_affineandbiasare trueThe weights are initialized to ones and the bias to zeros.
- Parameters:
normalized_shape – The shape of the trailing dimensions to normalize over.
eps – The value added to the variance for numerical stability.
elementwise_affine – Whether to include learnable per-element affine parameters.
bias – Whether to include a bias term (only if
elementwise_affineis true).initialize_parameters – Whether to initialize the parameters with their default/initial values. If false, the parameters are left as uninitialized memory, and reading them (e.g. during a forward pass) before loading their values (e.g. from a state dictionary) is undefined behavior.
requires_grad – Whether the parameters and the cached output arrays of the module require gradients.
- __call__(
- input: array,
Forward pass of the module.
- Parameters:
input – The input array, with shape
(*, *normalized_shape).- Returns:
The output array, with same shape as the input array.
- Raises:
ValueError – If the trailing dimensions of the input array do not match the normalized shape.