LapPosEncoderยถ
-
class
dgl.nn.pytorch.gt.
LapPosEncoder
(model_type, num_layer, k, dim, n_head=1, batch_norm=False, num_post_layer=0)[source]ยถ Bases:
torch.nn.modules.module.Module
Laplacian Positional Encoder (LPE), as introduced in GraphGPS: General Powerful Scalable Graph Transformers
This module is a learned laplacian positional encoding module using Transformer or DeepSet.
- Parameters
model_type (str) โ Encoder model type for LPE, can only be โTransformerโ or โDeepSetโ.
num_layer (int) โ Number of layers in Transformer/DeepSet Encoder.
k (int) โ Number of smallest non-trivial eigenvectors.
dim (int) โ Output size of final laplacian encoding.
n_head (int, optional) โ Number of heads in Transformer Encoder. Default : 1.
batch_norm (bool, optional) โ If True, apply batch normalization on raw laplacian positional encoding. Default : False.
num_post_layer (int, optional) โ If num_post_layer > 0, apply an MLP of
num_post_layer
layers after pooling. Default : 0.
Example
>>> import dgl >>> from dgl import LapPE >>> from dgl.nn import LapPosEncoder
>>> transform = LapPE(k=5, feat_name='eigvec', eigval_name='eigval', padding=True) >>> 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) >>> eigvals, eigvecs = g.ndata['eigval'], g.ndata['eigvec'] >>> transformer_encoder = LapPosEncoder( model_type="Transformer", num_layer=3, k=5, dim=16, n_head=4 ) >>> pos_encoding = transformer_encoder(eigvals, eigvecs) >>> deepset_encoder = LapPosEncoder( model_type="DeepSet", num_layer=3, k=5, dim=16, num_post_layer=2 ) >>> pos_encoding = deepset_encoder(eigvals, eigvecs)
-
forward
(eigvals, eigvecs)[source]ยถ - Parameters
eigvals (Tensor) โ Laplacian Eigenvalues of shape \((N, k)\), k different eigenvalues repeat N times, can be obtained by using LaplacianPE.
eigvecs (Tensor) โ Laplacian Eigenvectors of shape \((N, k)\), can be obtained by using LaplacianPE.
- Returns
Return the laplacian positional encodings of shape \((N, d)\), where \(N\) is the number of nodes in the input graph, \(d\) is
dim
.- Return type
Tensor