Skip to content

LapPEEncoder

LapPEEncoder(
    pe_dim: int,
    out_features_dim: int,
    norm: Optional[str] = None,
    random_sign_flip: bool = True,
)

Bases: RWSEEncoder

Projection of the Laplacian eigenvector positional encodings (LapPE) of the nodes, as the linear LapPE node encoder of GraphGPS.

Eigenvectors are only defined up to their sign, so during training (module.train()) the sign of every eigenvector of every graph is flipped at random, which makes the model learn sign-invariant functions of the encodings. The encodings (e.g. computed by AddLaplacianEigenvectorPE) are then normalized (optional) and linearly projected.

Parameters:

Name Type Description Default
pe_dim int

Number of eigenvectors \(k\) in the input encodings.

required
out_features_dim int

Size of the projected encodings.

required
norm Optional[str]

Normalization of the input encodings: "batch", "layer" or None. Default: None

None
random_sign_flip bool

Whether to flip signs at random during training. Default: True

True

Example:

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

encoder = LapPEEncoder(pe_dim=4, out_features_dim=8)
laplacian_pe = mx.random.normal((10, 4))
batch_indices = mx.array([0] * 6 + [1] * 4)
>>> encoder(laplacian_pe, batch_indices).shape
(10, 8)

Methods:

Name Description
__call__

Projects the eigenvector encodings pe.

Source code in mlx_graphs/nn/encoding.py
def __init__(
    self,
    pe_dim: int,
    out_features_dim: int,
    norm: Optional[str] = None,
    random_sign_flip: bool = True,
):
    super().__init__(pe_dim, out_features_dim, norm)
    self.random_sign_flip = random_sign_flip

__call__

__call__(pe: array, batch_indices: Optional[array] = None)

Projects the eigenvector encodings pe.

Parameters:

Name Type Description Default
pe array

Encodings of shape [num_nodes, pe_dim].

required
batch_indices Optional[array]

Graph of each node, so that each graph gets its own random signs during training. Default: None (one graph).

None
Source code in mlx_graphs/nn/encoding.py
def __call__(self, pe: mx.array, batch_indices: Optional[mx.array] = None):
    """Projects the eigenvector encodings ``pe``.

    Args:
        pe: Encodings of shape ``[num_nodes, pe_dim]``.
        batch_indices: Graph of each node, so that each graph gets its own
            random signs during training. Default: ``None`` (one graph).
    """
    if self.training and self.random_sign_flip:
        if batch_indices is None:
            flip = mx.random.bernoulli(0.5, (1, pe.shape[1]))
        else:
            # One row of signs per graph, without reading the number of
            # graphs back from the device (which breaks `mx.compile`):
            # there are at most as many non-empty graphs as nodes. Graph
            # ids beyond that (after empty graphs) wrap around, which
            # keeps one uniformly random sign per graph and eigenvector.
            num_rows = max(pe.shape[0], 1)
            flip = mx.random.bernoulli(0.5, (num_rows, pe.shape[1]))
            flip = flip[batch_indices % num_rows]
        pe = pe * mx.where(flip, -1.0, 1.0).astype(pe.dtype)
    return super().__call__(pe)

from mlx_graphs.nn import LapPEEncoder · mlx_graphs/nn/encoding.py