-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_adj.py
198 lines (156 loc) · 6.85 KB
/
get_adj.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Adapted/copied from:
# https://github.com/flyingtango/DiGCN
import os.path as osp
import numpy as np
import scipy.sparse as sp
import networkx as nx
import pandas as pd
import os
import torch
import torch_geometric.transforms as T
from torch_geometric.data import Data
from torch_geometric.utils import to_undirected, is_undirected, to_networkx
from networkx.algorithms.components import is_weakly_connected
from torch_geometric.utils import add_remaining_self_loops, add_self_loops, remove_self_loops
from torch_scatter import scatter_add
import scipy
def get_undirected_adj(edge_index, num_nodes, dtype):
edge_weight = torch.ones((edge_index.size(1), ), dtype=dtype,
device=edge_index.device)
fill_value = 1
edge_index, edge_weight = add_self_loops(
edge_index, edge_weight, fill_value, num_nodes)
row, col = edge_index
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
deg_inv_sqrt = deg.pow(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
return edge_index, deg_inv_sqrt[row] * edge_weight * deg_inv_sqrt[col]
def get_pr_directed_adj(alpha, edge_index, num_nodes, dtype):
edge_weight = torch.ones((edge_index.size(1), ), dtype=dtype,
device=edge_index.device)
fill_value = 1
edge_index, edge_weight = add_self_loops(
edge_index, edge_weight, fill_value, num_nodes)
row, col = edge_index
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
deg_inv = deg.pow(-1)
deg_inv[deg_inv == float('inf')] = 0
p = deg_inv[row] * edge_weight
p_dense = torch.sparse.FloatTensor(edge_index, p, torch.Size([num_nodes,num_nodes])).to_dense()
# pagerank p
p_pr = (1.0-alpha) * p_dense + alpha / num_nodes * torch.ones((num_nodes,num_nodes), dtype=dtype, device=p.device)
eig_value, left_vector = scipy.linalg.eig(p_pr.numpy(),left=True,right=False)
eig_value = torch.from_numpy(eig_value.real)
left_vector = torch.from_numpy(left_vector.real)
val, ind = eig_value.sort(descending=True)
# assert val[0] == 1.0
pi = left_vector[:,ind[0]] # choose the largest eig vector
pi = pi/pi.sum() # norm pi
# Note that by scaling the vectors, even the sign can change. That's why positive and negative elements might get flipped.
assert len(pi[pi<0]) == 0
pi_inv_sqrt = pi.pow(-0.5)
pi_inv_sqrt[pi_inv_sqrt == float('inf')] = 0
pi_inv_sqrt = pi_inv_sqrt.diag()
pi_sqrt = pi.pow(0.5)
pi_sqrt[pi_sqrt == float('inf')] = 0
pi_sqrt = pi_sqrt.diag()
# L_pr
L = (torch.mm(torch.mm(pi_sqrt, p_pr), pi_inv_sqrt) + torch.mm(torch.mm(pi_inv_sqrt, p_pr.t()), pi_sqrt)) / 2.0
# make nan to 0
L[torch.isnan(L)] = 0
# # let little possbility connection to 0, make L sparse
# L[ L < (1/num_nodes)] = 0
# L[ L < 5e-4] = 0
# transfer dense L to sparse
L_indices = torch.nonzero(L,as_tuple=False).t()
L_values = L[L_indices[0], L_indices[1]]
edge_index = L_indices
edge_weight = L_values
# row normalization
row, col = edge_index
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
deg_inv_sqrt = deg.pow(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
return edge_index, deg_inv_sqrt[row] * edge_weight * deg_inv_sqrt[col]
def get_appr_directed_adj(alpha, edge_index, num_nodes, dtype, edge_weight=None):
if edge_weight ==None:
edge_weight = torch.ones((edge_index.size(1), ), dtype=dtype,
device=edge_index.device)
fill_value = 1
edge_index, edge_weight = add_self_loops(
edge_index, edge_weight, fill_value, num_nodes)
row, col = edge_index
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
deg_inv = deg.pow(-1)
deg_inv[deg_inv == float('inf')] = 0
p = deg_inv[row] * edge_weight
# personalized pagerank p
p_dense = torch.sparse.FloatTensor(edge_index, p, torch.Size([num_nodes,num_nodes])).to_dense()
p_v = torch.zeros(torch.Size([num_nodes+1,num_nodes+1]))
p_v[0:num_nodes,0:num_nodes] = (1-alpha) * p_dense
p_v[num_nodes,0:num_nodes] = 1.0 / num_nodes
p_v[0:num_nodes,num_nodes] = alpha
p_v[num_nodes,num_nodes] = 0.0
p_ppr = p_v
eig_value, left_vector = scipy.linalg.eig(p_ppr.numpy(),left=True,right=False)
eig_value = torch.from_numpy(eig_value.real)
left_vector = torch.from_numpy(left_vector.real)
val, ind = eig_value.sort(descending=True)
pi = left_vector[:,ind[0]] # choose the largest eig vector
pi = pi[0:num_nodes]
p_ppr = p_dense
pi = pi/pi.sum() # norm pi
# Note that by scaling the vectors, even the sign can change. That's why positive and negative elements might get flipped.
assert len(pi[pi<0]) == 0
pi_inv_sqrt = pi.pow(-0.5)
pi_inv_sqrt[pi_inv_sqrt == float('inf')] = 0
pi_inv_sqrt = pi_inv_sqrt.diag()
pi_sqrt = pi.pow(0.5)
pi_sqrt[pi_sqrt == float('inf')] = 0
pi_sqrt = pi_sqrt.diag()
# L_appr
L = (torch.mm(torch.mm(pi_sqrt, p_ppr), pi_inv_sqrt) + torch.mm(torch.mm(pi_inv_sqrt, p_ppr.t()), pi_sqrt)) / 2.0
# make nan to 0
L[torch.isnan(L)] = 0
# transfer dense L to sparse
L_indices = torch.nonzero(L,as_tuple=False).t()
L_values = L[L_indices[0], L_indices[1]]
edge_index = L_indices
edge_weight = L_values
# row normalization
row, col = edge_index
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
deg_inv_sqrt = deg.pow(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
return edge_index, deg_inv_sqrt[row] * edge_weight * deg_inv_sqrt[col]
def get_second_directed_adj(edge_index, num_nodes, dtype):
edge_weight = torch.ones((edge_index.size(1), ), dtype=dtype,
device=edge_index.device)
fill_value = 1
edge_index, edge_weight = add_self_loops(
edge_index, edge_weight, fill_value, num_nodes)
row, col = edge_index
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
deg_inv = deg.pow(-1)
deg_inv[deg_inv == float('inf')] = 0
p = deg_inv[row] * edge_weight
p_dense = torch.sparse.FloatTensor(edge_index, p, torch.Size([num_nodes,num_nodes])).to_dense()
L_in = torch.mm(p_dense.t(), p_dense)
L_out = torch.mm(p_dense, p_dense.t())
L_in_hat = L_in
L_out_hat = L_out
L_in_hat[L_out == 0] = 0
L_out_hat[L_in == 0] = 0
# L^{(2)}
L = (L_in_hat + L_out_hat) / 2.0
L[torch.isnan(L)] = 0
L_indices = torch.nonzero(L,as_tuple=False).t()
L_values = L[L_indices[0], L_indices[1]]
edge_index = L_indices
edge_weight = L_values
# row normalization
row, col = edge_index
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
deg_inv_sqrt = deg.pow(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
return edge_index, deg_inv_sqrt[row] * edge_weight * deg_inv_sqrt[col]