Skip to content

AddRandomWalkPE

AddRandomWalkPE(walk_length: int, attr_name: Optional[str] = 'random_walk_pe')

Bases: BaseTransform

Adds the random walk positional encoding of each node, from "Graph Neural Networks with Learnable Structural and Positional Representations".

The encoding of a node holds its probabilities of being back at its starting point after \(1, 2, \ldots,\) walk_length steps of a random walk, i.e. the diagonals of the powers of the transition matrix \(\mathbf{D}^{-1} \mathbf{A}\), where \(\mathbf{D}\) is the out-degree. The edge_weights attribute of the graph is used as \(\mathbf{A}\) if it exists: the walk then follows an edge with a probability proportional to its weight. Duplicate edges are summed.

The encoding is computed on the host with NumPy: with dense matrices for graphs of up to DENSE_THRESHOLD nodes, and otherwise with scipy sparse products on blocks of nodes, which keeps the memory bounded.

Parameters:

Name Type Description Default
walk_length int

Number of random walk steps, which is the size of the encoding.

required
attr_name Optional[str]

Name of the attribute in which the [num_nodes, walk_length] float32 encoding is stored. If None, the encoding is appended to the node features instead. Default: "random_walk_pe"

'random_walk_pe'

Example:

graph = GraphData(edge_index=mx.array([[0, 1, 1, 2], [1, 0, 2, 1]]))
graph = AddRandomWalkPE(walk_length=2)(graph)
graph.random_walk_pe
# mx.array([[0, 0.5], [0, 1], [0, 0.5]])

Methods:

Name Description
process

Adds the random walk positional encoding to data.

Source code in mlx_graphs/transforms/features.py
def __init__(self, walk_length: int, attr_name: Optional[str] = "random_walk_pe"):
    self.walk_length = walk_length
    self.attr_name = attr_name

process

process(data: GraphData) -> GraphData

Adds the random walk positional encoding to data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph with the encoding.

Source code in mlx_graphs/transforms/features.py
def process(self, data: GraphData) -> GraphData:
    """Adds the random walk positional encoding to ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph with the encoding.
    """
    num_nodes = _num_nodes(data)
    src, dst = _host_edges(data)
    weights = _edge_weights(data)
    out_degree = _weighted_bincount(src, weights, num_nodes)
    transition = np.zeros_like(weights)
    np.divide(weights, out_degree[src], out=transition, where=out_degree[src] != 0)

    if num_nodes <= self.DENSE_THRESHOLD:
        encoding = self._dense(src, dst, transition, num_nodes)
    else:
        encoding = self._sparse(src, dst, transition, num_nodes)
    encoding = mx.array(encoding.astype(np.float32))

    if self.attr_name is None:
        return _add_node_features(data, encoding, cat=True)
    setattr(data, self.attr_name, encoding)
    return data

from mlx_graphs.transforms import AddRandomWalkPE · mlx_graphs/transforms/features.py