r"""Laplacian Positional Encoding, as introduced in `Benchmarking Graph Neural Networks `__ This function computes the laplacian positional encodings as the k smallest non-trivial eigenvectors. Parameters ---------- g : DGLGraph
(g, k, padding=False, return_eigval=False)
| 3612 | |
| 3613 | |
| 3614 | def lap_pe(g, k, padding=False, return_eigval=False): |
| 3615 | r"""Laplacian Positional Encoding, as introduced in |
| 3616 | `Benchmarking Graph Neural Networks |
| 3617 | <https://arxiv.org/abs/2003.00982>`__ |
| 3618 | |
| 3619 | This function computes the laplacian positional encodings as the |
| 3620 | k smallest non-trivial eigenvectors. |
| 3621 | |
| 3622 | Parameters |
| 3623 | ---------- |
| 3624 | g : DGLGraph |
| 3625 | The input graph. Must be homogeneous and bidirected. |
| 3626 | k : int |
| 3627 | Number of smallest non-trivial eigenvectors to use for positional |
| 3628 | encoding. |
| 3629 | padding : bool, optional |
| 3630 | If False, raise an exception when k>=n. Otherwise, add zero paddings |
| 3631 | in the end of eigenvectors and 'nan' paddings in the end of eigenvalues |
| 3632 | when k>=n. Default: False. n is the number of nodes in the given graph. |
| 3633 | return_eigval : bool, optional |
| 3634 | If True, return laplacian eigenvalues together with eigenvectors. |
| 3635 | Otherwise, return laplacian eigenvectors only. |
| 3636 | Default: False. |
| 3637 | |
| 3638 | Returns |
| 3639 | ------- |
| 3640 | Tensor or (Tensor, Tensor) |
| 3641 | Return the laplacian positional encodings of shape :math:`(N, k)`, |
| 3642 | where :math:`N` is the number of nodes in the input graph, when |
| 3643 | :attr:`return_eigval` is False. The eigenvalues of shape :math:`N` is |
| 3644 | additionally returned as the second element when :attr:`return_eigval` |
| 3645 | is True. |
| 3646 | |
| 3647 | Example |
| 3648 | ------- |
| 3649 | >>> import dgl |
| 3650 | >>> g = dgl.graph(([0,1,2,3,1,2,3,0], [1,2,3,0,0,1,2,3])) |
| 3651 | >>> dgl.lap_pe(g, 2) |
| 3652 | tensor([[ 7.0711e-01, -6.4921e-17], |
| 3653 | [ 3.0483e-16, -7.0711e-01], |
| 3654 | [-7.0711e-01, -2.4910e-16], |
| 3655 | [ 9.9288e-17, 7.0711e-01]]) |
| 3656 | >>> dgl.lap_pe(g, 5, padding=True) |
| 3657 | tensor([[ 7.0711e-01, -6.4921e-17, 5.0000e-01, 0.0000e+00, 0.0000e+00], |
| 3658 | [ 3.0483e-16, -7.0711e-01, -5.0000e-01, 0.0000e+00, 0.0000e+00], |
| 3659 | [-7.0711e-01, -2.4910e-16, 5.0000e-01, 0.0000e+00, 0.0000e+00], |
| 3660 | [ 9.9288e-17, 7.0711e-01, -5.0000e-01, 0.0000e+00, 0.0000e+00]]) |
| 3661 | >>> dgl.lap_pe(g, 5, padding=True, return_eigval=True) |
| 3662 | (tensor([[-7.0711e-01, 6.4921e-17, -5.0000e-01, 0.0000e+00, 0.0000e+00], |
| 3663 | [-3.0483e-16, 7.0711e-01, 5.0000e-01, 0.0000e+00, 0.0000e+00], |
| 3664 | [ 7.0711e-01, 2.4910e-16, -5.0000e-01, 0.0000e+00, 0.0000e+00], |
| 3665 | [-9.9288e-17, -7.0711e-01, 5.0000e-01, 0.0000e+00, 0.0000e+00]]), |
| 3666 | tensor([1., 1., 2., nan, nan])) |
| 3667 | """ |
| 3668 | # check for the "k < n" constraint |
| 3669 | n = g.num_nodes() |
| 3670 | if not padding and n <= k: |
| 3671 | assert ( |
no test coverage detected