dgl.svd_pe(g, k, padding=False, random_flip=True)[source]

SVD-based Positional Encoding, as introduced in Global Self-Attention as a Replacement for Graph Convolution

This function computes the largest \(k\) singular values and corresponding left and right singular vectors to form positional encodings.

  • g (DGLGraph) – A DGLGraph to be encoded, which must be a homogeneous one.

  • k (int) – Number of largest singular values and corresponding singular vectors used for positional encoding.

  • padding (bool, optional) – If False, raise an error when \(k > N\), where \(N\) is the number of nodes in g. If True, add zero paddings in the end of encoding vectors when \(k > N\). Default : False.

  • random_flip (bool, optional) – If True, randomly flip the signs of encoding vectors. Proposed to be activated during training for better generalization. Default : True.


Return SVD-based positional encodings of shape \((N, 2k)\).

Return type



>>> import dgl
>>> g = dgl.graph(([0,1,2,3,4,2,3,1,4,0], [2,3,1,4,0,0,1,2,3,4]))
>>> dgl.svd_pe(g, k=2, padding=False, random_flip=True)
tensor([[-6.3246e-01, -1.1373e-07, -6.3246e-01,  0.0000e+00],
        [-6.3246e-01,  7.6512e-01, -6.3246e-01, -7.6512e-01],
        [ 6.3246e-01,  4.7287e-01,  6.3246e-01, -4.7287e-01],
        [-6.3246e-01, -7.6512e-01, -6.3246e-01,  7.6512e-01],
        [ 6.3246e-01, -4.7287e-01,  6.3246e-01,  4.7287e-01]])