Skip to content

GraphNormalization

GraphNormalization(node_features_dim: int, eps: float = 1e-05)

Bases: Module

Applies graph normalization over individual graphs as described in the "GraphNorm: A Principled Approach to Accelerating Graph Neural Network Training" paper.

\[ \mathbf{x}^{\prime}_i = \frac{\mathbf{x} - \alpha \odot \textrm{E}[\mathbf{x}]} {\sqrt{\textrm{Var}[\mathbf{x} - \alpha \odot \textrm{E}[\mathbf{x}]] + \epsilon}} \odot \gamma + \beta \]

where \(\alpha\) denotes parameters that learn how much information to keep in the mean. The statistics are computed per graph and per feature. Also available as GraphNorm, its PyG name.

Parameters:

Name Type Description Default
node_features_dim int

Size of the input node features.

required
eps float

A value added to the denominator for numerical stability. Default: 1e-5

1e-05

Example:

import mlx.core as mx
from mlx_graphs.nn import GraphNormalization

norm = GraphNormalization(16)
node_features = mx.random.normal((5, 16))
batch_indices = mx.array([0, 0, 0, 1, 1])

>>> norm(node_features, batch_indices).shape
(5, 16)

Methods:

Name Description
__call__

Normalizes the node features of each graph.

Source code in mlx_graphs/nn/norm/graph_norm.py
def __init__(self, node_features_dim: int, eps: float = 1e-5):
    super().__init__()
    self.node_features_dim = node_features_dim
    self.eps = eps
    self.weight = mx.ones((node_features_dim,))
    self.bias = mx.zeros((node_features_dim,))
    self.mean_scale = mx.ones((node_features_dim,))

__call__

__call__(
    node_features: array,
    batch_indices: Optional[array] = None,
    num_graphs: Optional[int] = None,
) -> array

Normalizes the node features of each graph.

Parameters:

Name Type Description Default
node_features array

Node features of shape [num_nodes, node_features_dim].

required
batch_indices Optional[array]

Graph of each node, of shape [num_nodes]. Default: None (all the nodes belong to a single graph).

None
num_graphs Optional[int]

Number of graphs. Default: None (inferred from batch_indices).

None

Returns:

Type Description
array

The normalized node features.

Source code in mlx_graphs/nn/norm/graph_norm.py
def __call__(
    self,
    node_features: mx.array,
    batch_indices: Optional[mx.array] = None,
    num_graphs: Optional[int] = None,
) -> mx.array:
    """Normalizes the node features of each graph.

    Args:
        node_features: Node features of shape ``[num_nodes, node_features_dim]``.
        batch_indices: Graph of each node, of shape ``[num_nodes]``. Default:
            ``None`` (all the nodes belong to a single graph).
        num_graphs: Number of graphs. Default: ``None`` (inferred from
            ``batch_indices``).

    Returns:
        The normalized node features.
    """
    batch_indices, num_graphs = _batch_or_single(
        node_features, batch_indices, num_graphs
    )
    mean = scatter(node_features, batch_indices, num_graphs, aggr="mean")
    out = node_features - mean[batch_indices] * self.mean_scale
    var = scatter(out * out, batch_indices, num_graphs, aggr="mean")
    std = mx.sqrt(var + self.eps)[batch_indices]
    return self.weight * out / std + self.bias

from mlx_graphs.nn import GraphNormalization · mlx_graphs/nn/norm/graph_norm.py