dgl.random_walk_pe(g, k, eweight_name=None)[source]

Random Walk Positional Encoding, as introduced in Graph Neural Networks with Learnable Structural and Positional Representations

This function computes the random walk positional encodings as landing probabilities from 1-step to k-step, starting from each node to itself.

  • g (DGLGraph) – The input graph. Must be homogeneous.

  • k (int) – The number of random walk steps. The paper found the best value to be 16 and 20 for two experiments.

  • eweight_name (str, optional) – The name to retrieve the edge weights. Default: None, not using the edge weights.


The random walk positional encodings of shape \((N, k)\), where \(N\) is the number of nodes in the input graph.

Return type:



>>> import dgl
>>> g = dgl.graph(([0,1,1], [1,1,0]))
>>> dgl.random_walk_pe(g, 2)
tensor([[0.0000, 0.5000],
        [0.5000, 0.7500]])