dgl.laplacian_pe(g, k, padding=False, return_eigval=False)[source]

Laplacian Positional Encoding, as introduced in Benchmarking Graph Neural Networks

This function computes the laplacian positional encodings as the k smallest non-trivial eigenvectors.

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

  • k (int) – Number of smallest non-trivial eigenvectors to use for positional encoding.

  • padding (bool, optional) – If False, raise an exception when k>=n. Otherwise, add zero paddings in the end of eigenvectors and ‘nan’ paddings in the end of eigenvalues when k>=n. Default: False. n is the number of nodes in the given graph.

  • return_eigval (bool, optional) – If True, return laplacian eigenvalues together with eigenvectors. Otherwise, return laplacian eigenvectors only. Default: False.


Return the laplacian positional encodings of shape \((N, k)\), where \(N\) is the number of nodes in the input graph, when return_eigval is False. The eigenvalues of shape \(N\) is additionally returned as the second element when return_eigval is True.

Return type

Tensor or (Tensor, Tensor)


>>> import dgl
>>> g = dgl.graph(([0,1,2,3,1,2,3,0], [1,2,3,0,0,1,2,3]))
>>> dgl.laplacian_pe(g, 2)
tensor([[ 7.0711e-01, -6.4921e-17],
        [ 3.0483e-16, -7.0711e-01],
        [-7.0711e-01, -2.4910e-16],
        [ 9.9288e-17,  7.0711e-01]])
>>> dgl.laplacian_pe(g, 5, padding=True)
tensor([[ 7.0711e-01, -6.4921e-17,  5.0000e-01,  0.0000e+00,  0.0000e+00],
        [ 3.0483e-16, -7.0711e-01, -5.0000e-01,  0.0000e+00,  0.0000e+00],
        [-7.0711e-01, -2.4910e-16,  5.0000e-01,  0.0000e+00,  0.0000e+00],
        [ 9.9288e-17,  7.0711e-01, -5.0000e-01,  0.0000e+00,  0.0000e+00]])
>>> dgl.laplacian_pe(g, 5, padding=True, return_eigval=True)
(tensor([[-7.0711e-01,  6.4921e-17, -5.0000e-01,  0.0000e+00,  0.0000e+00],
         [-3.0483e-16,  7.0711e-01,  5.0000e-01,  0.0000e+00,  0.0000e+00],
         [ 7.0711e-01,  2.4910e-16, -5.0000e-01,  0.0000e+00,  0.0000e+00],
         [-9.9288e-17, -7.0711e-01,  5.0000e-01,  0.0000e+00,  0.0000e+00]]),
 tensor([1., 1., 2., nan, nan]))