SVDPE

class dgl.transforms.SVDPE(k, feat_name='svd_pe', padding=False, random_flip=True)[source]

Bases: dgl.transforms.module.BaseTransform

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, which could be stored in ndata.

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

  • feat_name (str, optional) – Name to store the computed positional encodings in ndata. Default : svd_pe

  • 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 encodings 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.

Example

>>> import dgl
>>> from dgl import SVDPE
>>> transform = SVDPE(k=2, feat_name="svd_pe")
>>> g = dgl.graph(([0,1,2,3,4,2,3,1,4,0], [2,3,1,4,0,0,1,2,3,4]))
>>> g_ = transform(g)
>>> print(g_.ndata['svd_pe'])
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]])