dgl.svd_pe

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

此函数计算最大的\(k\)奇异值以及相应的左右奇异向量,以形成位置编码。

Parameters:
  • g (DGLGraph) – 一个需要编码的DGLGraph,必须是一个同质图。

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

  • padding (bool, optional) – 如果为False,当\(k > N\)时,会引发错误, 其中\(N\)g中的节点数。 如果为True,当\(k > N\)时,在编码向量的末尾添加零填充。 默认值: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.

Returns:

返回基于SVD的位置编码,形状为 \((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]])