-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
270 lines (215 loc) · 9.81 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
267
268
269
270
import numpy as np
import scipy.sparse as sp
import torch
import torch.nn.functional as F
from sklearn.neighbors import kneighbors_graph
import dgl
from sklearn import metrics
from munkres import Munkres
EOS = 1e-10
def apply_non_linearity(tensor, non_linearity, i):
if non_linearity == 'elu':
return F.elu(tensor * i - i) + 1
elif non_linearity == 'relu':
return F.relu(tensor)
elif non_linearity == 'none':
return tensor
else:
raise NameError('We dont support the non-linearity yet')
def split_batch(init_list, batch_size):
groups = zip(*(iter(init_list),) * batch_size)
end_list = [list(i) for i in groups]
count = len(init_list) % batch_size
end_list.append(init_list[-count:]) if count != 0 else end_list
return end_list
def edge_deletion(adj, drop_r):
edge_index = np.array(np.nonzero(adj))
half_edge_index = edge_index[:, edge_index[0,:] < edge_index[1,:]]
num_edge = half_edge_index.shape[1]
samples = np.random.choice(num_edge, size=int(drop_r * num_edge), replace=False)
dropped_edge_index = half_edge_index[:, samples].T
adj[dropped_edge_index[:,0],dropped_edge_index[:,1]] = 0.
adj[dropped_edge_index[:,1],dropped_edge_index[:,0]] = 0.
return adj
def edge_addition(adj, add_r):
edge_index = np.array(np.nonzero(adj))
half_edge_index = edge_index[:, edge_index[0,:] < edge_index[1,:]]
num_edge = half_edge_index.shape[1]
num_node = adj.shape[0]
added_edge_index_in = np.random.choice(num_node, size=int(add_r * num_edge), replace=True)
added_edge_index_out = np.random.choice(num_node, size=int(add_r * num_edge), replace=True)
adj[added_edge_index_in,added_edge_index_out] = 1.
adj[added_edge_index_out,added_edge_index_in] = 1.
return adj
def get_feat_mask(features, mask_rate):
feat_node = features.shape[1]
mask = torch.zeros(features.shape)
samples = np.random.choice(feat_node, size=int(feat_node * mask_rate), replace=False)
mask[:, samples] = 1
return mask.cuda(), samples
def accuracy(preds, labels):
pred_class = torch.max(preds, 1)[1]
return torch.sum(torch.eq(pred_class, labels)).float() / labels.shape[0]
def nearest_neighbors(X, k, metric):
adj = kneighbors_graph(X, k, metric=metric)
adj = np.array(adj.todense(), dtype=np.float32)
adj += np.eye(adj.shape[0])
return adj
def nearest_neighbors_sparse(X, k, metric):
adj = kneighbors_graph(X, k, metric=metric)
loop = np.arange(X.shape[0])
[s_, d_, val] = sp.find(adj)
s = np.concatenate((s_, loop))
d = np.concatenate((d_, loop))
return s, d
def nearest_neighbors_pre_exp(X, k, metric, i):
adj = kneighbors_graph(X, k, metric=metric)
adj = np.array(adj.todense(), dtype=np.float32)
adj += np.eye(adj.shape[0])
adj = adj * i - i
return adj
def nearest_neighbors_pre_elu(X, k, metric, i):
adj = kneighbors_graph(X, k, metric=metric)
adj = np.array(adj.todense(), dtype=np.float32)
adj += np.eye(adj.shape[0])
adj = adj * i - i
return adj
def normalize(adj, mode, sparse=False):
if not sparse:
if mode == "sym":
inv_sqrt_degree = 1. / (torch.sqrt(adj.sum(dim=1, keepdim=False)) + EOS)
return inv_sqrt_degree[:, None] * adj * inv_sqrt_degree[None, :]
elif mode == "row":
inv_degree = 1. / (adj.sum(dim=1, keepdim=False) + EOS)
return inv_degree[:, None] * adj
else:
exit("wrong norm mode")
else:
adj = adj.coalesce()
if mode == "sym":
inv_sqrt_degree = 1. / (torch.sqrt(torch.sparse.sum(adj, dim=1).values()))
D_value = inv_sqrt_degree[adj.indices()[0]] * inv_sqrt_degree[adj.indices()[1]]
elif mode == "row":
aa = torch.sparse.sum(adj, dim=1)
bb = aa.values()
inv_degree = 1. / (torch.sparse.sum(adj, dim=1).values() + EOS)
D_value = inv_degree[adj.indices()[0]]
else:
exit("wrong norm mode")
new_values = adj.values() * D_value
return torch.sparse.FloatTensor(adj.indices(), new_values, adj.size())
def symmetrize(adj): # only for non-sparse
return (adj + adj.T) / 2
def cal_similarity_graph(node_embeddings):
similarity_graph = torch.mm(node_embeddings, node_embeddings.t())
return similarity_graph
def top_k(raw_graph, K):
values, indices = raw_graph.topk(k=int(K), dim=-1)
assert torch.max(indices) < raw_graph.shape[1]
mask = torch.zeros(raw_graph.shape).cuda()
mask[torch.arange(raw_graph.shape[0]).view(-1, 1), indices] = 1.
mask.requires_grad = False
sparse_graph = raw_graph * mask
return sparse_graph
def knn_fast(X, k, b):
X = F.normalize(X, dim=1, p=2)
index = 0
values = torch.zeros(X.shape[0] * (k + 1)).cuda()
rows = torch.zeros(X.shape[0] * (k + 1)).cuda()
cols = torch.zeros(X.shape[0] * (k + 1)).cuda()
norm_row = torch.zeros(X.shape[0]).cuda()
norm_col = torch.zeros(X.shape[0]).cuda()
while index < X.shape[0]:
if (index + b) > (X.shape[0]):
end = X.shape[0]
else:
end = index + b
sub_tensor = X[index:index + b]
similarities = torch.mm(sub_tensor, X.t())
vals, inds = similarities.topk(k=k + 1, dim=-1)
values[index * (k + 1):(end) * (k + 1)] = vals.view(-1)
cols[index * (k + 1):(end) * (k + 1)] = inds.view(-1)
rows[index * (k + 1):(end) * (k + 1)] = torch.arange(index, end).view(-1, 1).repeat(1, k + 1).view(-1)
norm_row[index: end] = torch.sum(vals, dim=1)
norm_col.index_add_(-1, inds.view(-1), vals.view(-1))
index += b
norm = norm_row + norm_col
rows = rows.long()
cols = cols.long()
values *= (torch.pow(norm[rows], -0.5) * torch.pow(norm[cols], -0.5))
return rows, cols, values
def sparse_mx_to_torch_sparse_tensor(sparse_mx):
"""Convert a scipy sparse matrix to a torch sparse tensor."""
sparse_mx = sparse_mx.tocoo().astype(np.float32)
indices = torch.from_numpy(
np.vstack((sparse_mx.row, sparse_mx.col)).astype(np.int64))
values = torch.from_numpy(sparse_mx.data)
shape = torch.Size(sparse_mx.shape)
return torch.sparse.FloatTensor(indices, values, shape)
def torch_sparse_to_dgl_graph(torch_sparse_mx):
torch_sparse_mx = torch_sparse_mx.coalesce()
indices = torch_sparse_mx.indices()
values = torch_sparse_mx.values()
rows_, cols_ = indices[0,:], indices[1,:]
dgl_graph = dgl.graph((rows_, cols_), num_nodes=torch_sparse_mx.shape[0], device='cuda')
dgl_graph.edata['w'] = values.detach().cuda()
return dgl_graph
def dgl_graph_to_torch_sparse(dgl_graph):
values = dgl_graph.edata['w'].cpu().detach()
rows_, cols_ = dgl_graph.edges()
indices = torch.cat((torch.unsqueeze(rows_, 0), torch.unsqueeze(cols_, 0)), 0).cpu()
torch_sparse_mx = torch.sparse.FloatTensor(indices, values)
return torch_sparse_mx
def torch_sparse_eye(num_nodes):
indices = torch.arange(num_nodes).repeat(2, 1)
values = torch.ones(num_nodes)
return torch.sparse.FloatTensor(indices, values)
class clustering_metrics():
def __init__(self, true_label, predict_label):
self.true_label = true_label
self.pred_label = predict_label
def clusteringAcc(self):
# best mapping between true_label and predict label
l1 = list(set(self.true_label))
numclass1 = len(l1)
l2 = list(set(self.pred_label))
numclass2 = len(l2)
if numclass1 != numclass2:
print('Class Not equal, Error!!!!')
return 0, 0, 0, 0, 0, 0, 0
cost = np.zeros((numclass1, numclass2), dtype=int)
for i, c1 in enumerate(l1):
mps = [i1 for i1, e1 in enumerate(self.true_label) if e1 == c1]
for j, c2 in enumerate(l2):
mps_d = [i1 for i1 in mps if self.pred_label[i1] == c2]
cost[i][j] = len(mps_d)
# match two clustering results by Munkres algorithm
m = Munkres()
cost = cost.__neg__().tolist()
indexes = m.compute(cost)
# get the match results
new_predict = np.zeros(len(self.pred_label))
for i, c in enumerate(l1):
# correponding label in l2:
c2 = l2[indexes[i][1]]
# ai is the index with label==c2 in the pred_label list
ai = [ind for ind, elm in enumerate(self.pred_label) if elm == c2]
new_predict[ai] = c
acc = metrics.accuracy_score(self.true_label, new_predict)
f1_macro = metrics.f1_score(self.true_label, new_predict, average='macro')
precision_macro = metrics.precision_score(self.true_label, new_predict, average='macro')
recall_macro = metrics.recall_score(self.true_label, new_predict, average='macro')
f1_micro = metrics.f1_score(self.true_label, new_predict, average='micro')
precision_micro = metrics.precision_score(self.true_label, new_predict, average='micro')
recall_micro = metrics.recall_score(self.true_label, new_predict, average='micro')
return acc, f1_macro, precision_macro, recall_macro, f1_micro, precision_micro, recall_micro
def evaluationClusterModelFromLabel(self, print_results=True):
nmi = metrics.normalized_mutual_info_score(self.true_label, self.pred_label)
adjscore = metrics.adjusted_rand_score(self.true_label, self.pred_label)
acc, f1_macro, precision_macro, recall_macro, f1_micro, precision_micro, recall_micro = self.clusteringAcc()
if print_results:
print('ACC={:.4f}, f1_macro={:.4f}, precision_macro={:.4f}, recall_macro={:.4f}, f1_micro={:.4f}, '
.format(acc, f1_macro, precision_macro, recall_macro, f1_micro) +
'precision_micro={:.4f}, recall_micro={:.4f}, NMI={:.4f}, ADJ_RAND_SCORE={:.4f}'
.format(precision_micro, recall_micro, nmi, adjscore))
return acc, nmi, f1_macro, adjscore