Skip to content

HeteroLayerNormalization

HeteroLayerNormalization(
    in_channels: int, num_types: int, eps: float = 1e-05, affine: bool = True
)

Bases: Module

Applies layer normalization over each individual example in a batch of heterogeneous features, as described in the "Layer Normalization" paper.

Compared to a standard layer norm, HeteroLayerNorm applies normalization individually for each node or edge type. Note that the normalization itself (per-node mean/variance across channels) does not depend on the type: only the learnable affine parameters do.

Parameters:

Name Type Description Default
in_channels int

Size of each input sample.

required
num_types int

The number of types.

required
eps float

A value added to the denominator for numerical stability. (default: 1e-5)

1e-05
affine bool

If set to True, this module has learnable affine parameters \(\gamma\) and \(\beta\). (default: True)

True

Methods:

Name Description
reset_parameters

Resets all learnable parameters of the module.

__call__

Forward pass.

Source code in mlx_graphs/nn/norm/layer_norm.py
def __init__(
    self,
    in_channels: int,
    num_types: int,
    eps: float = 1e-5,
    affine: bool = True,
):
    super().__init__()

    self.in_channels = in_channels
    self.num_types = num_types
    self.eps = eps
    self.affine = affine

    if self.affine:
        self.weight = mx.ones((num_types, in_channels))
        self.bias = mx.zeros((num_types, in_channels))

    self.reset_parameters()

reset_parameters

reset_parameters()

Resets all learnable parameters of the module.

Source code in mlx_graphs/nn/norm/layer_norm.py
def reset_parameters(self):
    r"""Resets all learnable parameters of the module."""
    if self.affine:
        self.weight = mx.ones_like(self.weight)
        self.bias = mx.zeros_like(self.bias)

__call__

__call__(x: array, type_vec: array) -> array

Forward pass.

Parameters:

Name Type Description Default
x array

The input features of shape [num_items, in_channels].

required
type_vec array

An integer vector of shape [num_items] that maps each entry to a type in [0, num_types).

required
Source code in mlx_graphs/nn/norm/layer_norm.py
def __call__(self, x: mx.array, type_vec: mx.array) -> mx.array:
    r"""Forward pass.

    Args:
        x (mx.array): The input features of shape
            :obj:`[num_items, in_channels]`.
        type_vec (mx.array): An integer vector of shape
            :obj:`[num_items]` that maps each entry to a type in
            :obj:`[0, num_types)`.
    """
    mean = x.mean(axis=-1, keepdims=True)
    var = x.var(axis=-1, keepdims=True)
    out = (x - mean) / mx.sqrt(var + self.eps)

    if self.affine:
        out = out * self.weight[type_vec] + self.bias[type_vec]

    return out

from mlx_graphs.nn import HeteroLayerNormalization · mlx_graphs/nn/norm/layer_norm.py