-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
266 lines (224 loc) · 8.66 KB
/
utils.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import torch
from sklearn.metrics.pairwise import cosine_similarity
import scipy.sparse as sp
import numpy as np
import logging
def get_logger(filename, verbosity=1, name=None):
level_dict = {0: logging.DEBUG, 1: logging.INFO, 2: logging.WARNING}
formatter = logging.Formatter(
"[%(asctime)s][%(filename)s][line:%(lineno)d][%(levelname)s] %(message)s"
)
logger = logging.getLogger(name)
logger.setLevel(level_dict[verbosity])
fh = logging.FileHandler(filename, "w")
fh.setFormatter(formatter)
logger.addHandler(fh)
sh = logging.StreamHandler()
sh.setFormatter(formatter)
logger.addHandler(sh)
return logger
def adj_norm(adj, neighbor_only=False):
if not neighbor_only:
adj = torch.add(torch.eye(adj.shape[0]).cuda(), adj)
if adj.is_sparse:
degree = adj.to_dense().sum(dim=1)
else:
degree = adj.sum(dim=1)
in_degree_norm = torch.pow(degree.view(1, -1), -0.5).expand(adj.shape[0], adj.shape[0])
in_degree_norm = torch.where(torch.isinf(in_degree_norm), torch.full_like(in_degree_norm, 0), in_degree_norm)
out_degree_norm = torch.pow(degree.view(-1, 1), -0.5).expand(adj.shape[0], adj.shape[0])
out_degree_norm = torch.where(torch.isinf(out_degree_norm), torch.full_like(out_degree_norm, 0), out_degree_norm)
adj = sparse_dense_mul(adj, in_degree_norm)
adj = sparse_dense_mul(adj, out_degree_norm)
return adj
def sparse_dense_mul(s, d):
if not s.is_sparse:
return s * d
i = s._indices()
v = s._values()
dv = d[i[0, :], i[1, :]] # get values from relevant entries of dense matrix
return torch.sparse.FloatTensor(i, v * dv, s.size())
def evaluate(model, adj, features, labels, mask):
model.eval()
with torch.no_grad():
logits = model(adj, features)
logits = logits[mask]
test_labels = labels[mask]
_, indices = logits.max(dim=1)
correct = torch.sum(indices == test_labels)
return correct.item() * 1.0 / test_labels.shape[0]
def get_reliable_neighbors(adj, features, k, degree_threshold):
degree = adj.sum(dim=1)
degree_mask = degree > degree_threshold
assert degree_mask.sum().item() >= k
sim = cosine_similarity(features.to('cpu'))
sim = torch.FloatTensor(sim).to('cuda')
sim[:, degree_mask == False] = 0
_, top_k_indices = sim.topk(k=k, dim=1)
for i in range(adj.shape[0]):
adj[i][top_k_indices[i]] = 1
adj[i][i] = 0
return
def adj_new_norm(adj, alpha):
adj = torch.add(torch.eye(adj.shape[0]).cuda(), adj)
degree = adj.sum(dim=1)
in_degree_norm = torch.pow(degree.view(1, -1), alpha).expand(adj.shape[0], adj.shape[0])
out_degree_norm = torch.pow(degree.view(-1, 1), alpha).expand(adj.shape[0], adj.shape[0])
adj = sparse_dense_mul(adj, in_degree_norm)
adj = sparse_dense_mul(adj, out_degree_norm)
if alpha != -0.5:
return adj / (adj.sum(dim=1).reshape(adj.shape[0], -1))
else:
return adj
def preprocess_adj(features, adj, logger, metric='similarity', threshold=0.03, jaccard=True):
"""Drop dissimilar edges.(Faster version using numba)
"""
if not sp.issparse(adj):
adj = sp.csr_matrix(adj)
adj_triu = sp.triu(adj, format='csr')
if sp.issparse(features):
features = features.todense().A # make it easier for njit processing
if metric == 'distance':
removed_cnt = dropedge_dis(adj_triu.data, adj_triu.indptr, adj_triu.indices, features, threshold=threshold)
else:
if jaccard:
removed_cnt = dropedge_jaccard(adj_triu.data, adj_triu.indptr, adj_triu.indices, features,
threshold=threshold)
else:
removed_cnt = dropedge_cosine(adj_triu.data, adj_triu.indptr, adj_triu.indices, features,
threshold=threshold)
logger.info('removed %s edges in the original graph' % removed_cnt)
modified_adj = adj_triu + adj_triu.transpose()
return modified_adj
def dropedge_dis(A, iA, jA, features, threshold):
removed_cnt = 0
for row in range(len(iA)-1):
for i in range(iA[row], iA[row+1]):
# print(row, jA[i], A[i])
n1 = row
n2 = jA[i]
C = np.linalg.norm(features[n1] - features[n2])
if C > threshold:
A[i] = 0
# A[n2, n1] = 0
removed_cnt += 1
return removed_cnt
def dropedge_both(A, iA, jA, features, threshold1=2.5, threshold2=0.01):
removed_cnt = 0
for row in range(len(iA)-1):
for i in range(iA[row], iA[row+1]):
# print(row, jA[i], A[i])
n1 = row
n2 = jA[i]
C1 = np.linalg.norm(features[n1] - features[n2])
a, b = features[n1], features[n2]
inner_product = (a * b).sum()
C2 = inner_product / (np.sqrt(np.square(a).sum() + np.square(b).sum())+ 1e-6)
if C1 > threshold1 or threshold2 < 0:
A[i] = 0
# A[n2, n1] = 0
removed_cnt += 1
return removed_cnt
def dropedge_jaccard(A, iA, jA, features, threshold):
removed_cnt = 0
for row in range(len(iA)-1):
for i in range(iA[row], iA[row+1]):
# print(row, jA[i], A[i])
n1 = row
n2 = jA[i]
a, b = features[n1], features[n2]
intersection = np.count_nonzero(a*b)
J = intersection * 1.0 / (np.count_nonzero(a) + np.count_nonzero(b) - intersection)
if J < threshold:
A[i] = 0
# A[n2, n1] = 0
removed_cnt += 1
return removed_cnt
def dropedge_cosine(A, iA, jA, features, threshold):
removed_cnt = 0
for row in range(len(iA)-1):
for i in range(iA[row], iA[row+1]):
# print(row, jA[i], A[i])
n1 = row
n2 = jA[i]
a, b = features[n1], features[n2]
inner_product = (a * b).sum()
C = inner_product / (np.sqrt(np.square(a).sum()) * np.sqrt(np.square(b).sum()) + 1e-8)
if C <= threshold:
A[i] = 0
# A[n2, n1] = 0
removed_cnt += 1
return removed_cnt
def sparse_mx_to_sparse_tensor(sparse_mx):
"""sparse matrix to sparse tensor matrix(torch)
Args:
sparse_mx : scipy.sparse.csr_matrix
sparse matrix
"""
sparse_mx_coo = sparse_mx.tocoo().astype(np.float32)
sparse_row = torch.LongTensor(sparse_mx_coo.row).unsqueeze(1)
sparse_col = torch.LongTensor(sparse_mx_coo.col).unsqueeze(1)
sparse_indices = torch.cat((sparse_row, sparse_col), 1)
sparse_data = torch.FloatTensor(sparse_mx.data)
return torch.sparse.FloatTensor(sparse_indices.t(), sparse_data, torch.Size(sparse_mx.shape))
def to_tensor(adj, features, labels=None, device='cpu'):
"""Convert adj, features, labels from array or sparse matrix to
torch Tensor on target device.
Args:
adj : scipy.sparse.csr_matrix
the adjacency matrix.
features : scipy.sparse.csr_matrix
node features
labels : numpy.array
node labels
device : str
'cpu' or 'cuda'
"""
if sp.issparse(adj):
adj = sparse_mx_to_sparse_tensor(adj)
else:
adj = torch.FloatTensor(adj)
if sp.issparse(features):
features = sparse_mx_to_sparse_tensor(features)
else:
features = torch.FloatTensor(np.array(features))
if labels is None:
return adj.to(device), features.to(device)
else:
labels = torch.LongTensor(labels)
return adj.to(device), features.to(device), labels.to(device)
def idx_to_mask(idx, nodes_num):
"""Convert a indices array to a tensor mask matrix
Args:
idx : numpy.array
indices of nodes set
nodes_num: int
number of nodes
"""
mask = torch.zeros(nodes_num)
mask[idx] = 1
return mask.bool()
def is_sparse_tensor(tensor):
"""Check if a tensor is sparse tensor.
Args:
tensor : torch.Tensor
given tensor
Returns:
bool
whether a tensor is sparse tensor
"""
# if hasattr(tensor, 'nnz'):
if tensor.layout == torch.sparse_coo:
return True
else:
return False
def to_scipy(tensor):
"""Convert a dense/sparse tensor to scipy matrix"""
if is_sparse_tensor(tensor):
values = tensor._values()
indices = tensor._indices()
return sp.csr_matrix((values.cpu().numpy(), indices.cpu().numpy()), shape=tensor.shape)
else:
indices = tensor.nonzero().t()
values = tensor[indices[0], indices[1]]
return sp.csr_matrix((values.cpu().numpy(), indices.cpu().numpy()), shape=tensor.shape)