-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpe.py
More file actions
71 lines (57 loc) · 1.87 KB
/
Copy pathpe.py
File metadata and controls
71 lines (57 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from __future__ import annotations
import torch
import numpy as np
import torch_geometric
from scipy import sparse as sp
def random_walk(A, n_iter):
# Geometric diffusion features with Random Walk
Dinv = A.sum(dim=-1).clamp(min=1).pow(-1).unsqueeze(-1) # D^-1
RW = A * Dinv
M = RW
M_power = M
# Iterate
PE = [torch.diagonal(M)]
for _ in range(n_iter - 1):
M_power = torch.matmul(M_power, M)
PE.append(torch.diagonal(M_power))
PE = torch.stack(PE, dim=-1)
return PE
def RWSE(edge_index, pos_enc_dim, num_nodes):
"""
Initializing positional encoding with RWSE
"""
if edge_index.size(-1) == 0:
PE = torch.zeros(num_nodes, pos_enc_dim)
else:
A = torch_geometric.utils.to_dense_adj(edge_index, max_num_nodes=num_nodes)[0]
PE = random_walk(A, pos_enc_dim)
return PE
def LapPE(edge_index, pos_enc_dim, num_nodes):
"""
Graph positional encoding v/ Laplacian eigenvectors
"""
# Laplacian
degree = torch_geometric.utils.degree(edge_index[0], num_nodes)
A = torch_geometric.utils.to_scipy_sparse_matrix(edge_index, num_nodes=num_nodes)
N = sp.diags(np.array(degree.clip(1) ** -0.5, dtype=float))
L = sp.eye(num_nodes) - N * A * N
# Eigenvectors with numpy
EigVal, EigVec = np.linalg.eig(L.toarray())
idx = EigVal.argsort() # increasing order
EigVal, EigVec = EigVal[idx], np.real(EigVec[:, idx])
PE = torch.from_numpy(EigVec[:, 1 : pos_enc_dim + 1]).float()
if PE.size(1) < pos_enc_dim:
zeros = torch.zeros(num_nodes, pos_enc_dim)
zeros[:, : PE.size(1)] = PE
PE = zeros
return PE
class ROPE(torch.nn.Module):
def __init__(self):
return
def forward(self, x: torch.Tensor):
return
class FPE(torch.nn.Module):
def __init__(self):
return
def forward(self, x: torch.Tensor):
return