Skip to content

RWSEEncoder

RWSEEncoder(pe_dim: int, out_features_dim: int, norm='batch')

Bases: Module

Projection of the random-walk structural encodings (RWSE) of the nodes, as in the GPS example of PyG (examples/graph_gps.py) and the RWSE node encoder of GraphGPS.

The encodings (e.g. computed by AddRandomWalkPE) are normalized and linearly projected:

\[ \mathbf{p}_i' = \mathbf{W} \, \mathrm{Norm}(\mathbf{p}_i) + \mathbf{b} \]

The result is typically concatenated with an embedding of the node features before a GPSConv.

Parameters:

Name Type Description Default
pe_dim int

Size of the input encodings (the walk length).

required
out_features_dim int

Size of the projected encodings.

required
norm

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

'batch'

Example:

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

encoder = RWSEEncoder(pe_dim=20, out_features_dim=8)
random_walk_pe = mx.random.uniform(shape=(10, 20))
>>> encoder(random_walk_pe).shape
(10, 8)

Methods:

Name Description
__call__

Projects the encodings pe of shape [num_nodes, pe_dim].

Source code in mlx_graphs/nn/encoding.py
def __init__(self, pe_dim: int, out_features_dim: int, norm="batch"):
    super().__init__()
    if norm == "batch":
        self.norm = nn.BatchNorm(pe_dim)
    elif norm == "layer":
        self.norm = nn.LayerNorm(pe_dim)
    elif norm is not None:
        raise ValueError(f"Unknown norm '{norm}'. Available: 'batch', 'layer'")
    self.lin = nn.Linear(pe_dim, out_features_dim)

__call__

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

Projects the encodings pe of shape [num_nodes, pe_dim].

Parameters:

Name Type Description Default
pe array

Encodings of shape [num_nodes, pe_dim].

required
batch_indices Optional[array]

Unused, for compatibility with LapPEEncoder.

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

    Args:
        pe: Encodings of shape ``[num_nodes, pe_dim]``.
        batch_indices: Unused, for compatibility with
            :class:`LapPEEncoder`.
    """
    if "norm" in self:
        pe = self.norm(pe)
    return self.lin(pe)

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