Skip to content

PairNormalization

PairNormalization(
    scale: float = 1.0, scale_individually: bool = False, eps: float = 1e-05
)

Bases: Module

Applies pair normalization over node features as described in the "PairNorm: Tackling Oversmoothing in GNNs" paper.

\[ \mathbf{x}_i^c &= \mathbf{x}_i - \frac{1}{n} \sum_{i=1}^n \mathbf{x}_i \\ \mathbf{x}_i^{\prime} &= s \cdot \frac{\mathbf{x}_i^c}{\sqrt{\frac{1}{n} \sum_{i=1}^n {\| \mathbf{x}_i^c \|}^2_2}} \]

The statistics are computed separately for each graph of a batch. Also available as PairNorm, its PyG name.

Parameters:

Name Type Description Default
scale float

Scaling factor \(s\) of the normalization. Default: 1.0

1.0
scale_individually bool

If set to True, computes the scaling step as \(\mathbf{x}^{\prime}_i = s \cdot \frac{\mathbf{x}_i^c} {{\| \mathbf{x}_i^c \|}_2}\). Default: False

False
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 PairNormalization

norm = PairNormalization(scale=2.0)
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/pair_norm.py
def __init__(
    self, scale: float = 1.0, scale_individually: bool = False, eps: float = 1e-5
):
    super().__init__()
    self.scale = scale
    self.scale_individually = scale_individually
    self.eps = eps

__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, num_features].

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/pair_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, num_features]``.
        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")
    x = node_features - mean[batch_indices]

    if self.scale_individually:
        norm = p_norm(x, axis=-1, keepdims=True)
        return self.scale * x / (self.eps + norm)

    mean_sq_norm = scatter(
        (x * x).sum(axis=-1, keepdims=True), batch_indices, num_graphs, aggr="mean"
    )
    return self.scale * x / mx.sqrt(self.eps + mean_sq_norm[batch_indices])

from mlx_graphs.nn import PairNormalization · mlx_graphs/nn/norm/pair_norm.py