-
Notifications
You must be signed in to change notification settings - Fork 32
/
utils.py
520 lines (449 loc) · 15.1 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import opt
import torch
import random
import numpy as np
from sklearn import metrics
from munkres import Munkres
import torch.nn.functional as F
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.metrics import adjusted_rand_score as ari_score
from sklearn.metrics.cluster import normalized_mutual_info_score as nmi_score
def setup():
"""
setup
- name: the name of dataset
- device: CPU / GPU
- seed: random seed
- n_clusters: num of cluster
- n_input: dimension of feature
- alpha_value: alpha value for graph diffusion
- lambda_value: lambda value for clustering guidance
- gamma_value: gamma value for propagation regularization
- lr: learning rate
Return: None
"""
print("setting:")
setup_seed(opt.args.seed)
if opt.args.name == 'acm':
opt.args.n_clusters = 3
opt.args.n_input = 100
opt.args.alpha_value = 0.2
opt.args.lambda_value = 10
opt.args.gamma_value = 1e3
opt.args.lr = 5e-5
elif opt.args.name == 'dblp':
opt.args.n_clusters = 4
opt.args.n_input = 50
opt.args.alpha_value = 0.2
opt.args.lambda_value = 10
opt.args.gamma_value = 1e3
opt.args.lr = 1e-4
elif opt.args.name == 'cite':
opt.args.n_clusters = 6
opt.args.n_input = 100
opt.args.alpha_value = 0.2
opt.args.lambda_value = 10
opt.args.gamma_value = 1e3
opt.args.lr = 1e-5
elif opt.args.name == 'amap':
opt.args.n_clusters = 8
opt.args.n_input = 100
opt.args.alpha_value = 0.2
opt.args.lambda_value = 10
opt.args.gamma_value = 1e3
opt.args.lr = 1e-3
else:
print("error!")
print("please add the new dataset's parameters")
print("------------------------------")
print("dataset : ")
print("device : ")
print("random seed : ")
print("clusters : ")
print("alpha value : ")
print("lambda value : ")
print("gamma value : ")
print("learning rate : ")
print("------------------------------")
exit(0)
opt.args.device = torch.device("cuda" if opt.args.cuda else "cpu")
print("------------------------------")
print("dataset : {}".format(opt.args.name))
print("device : {}".format(opt.args.device))
print("random seed : {}".format(opt.args.seed))
print("clusters : {}".format(opt.args.n_clusters))
print("alpha value : {}".format(opt.args.alpha_value))
print("lambda value : {}".format(opt.args.lambda_value))
print("gamma value : {:.0e}".format(opt.args.gamma_value))
print("learning rate : {:.0e}".format(opt.args.lr))
print("------------------------------")
def setup_seed(seed):
"""
setup random seed to fix the result
Args:
seed: random seed
Returns: None
"""
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def numpy_to_torch(a, sparse=False):
"""
numpy array to torch tensor
:param a: the numpy array
:param sparse: is sparse tensor or not
:return: torch tensor
"""
if sparse:
a = torch.sparse.Tensor(a)
a = a.to_sparse()
else:
a = torch.FloatTensor(a)
return a
def torch_to_numpy(t):
"""
torch tensor to numpy array
:param t: the torch tensor
:return: numpy array
"""
return t.numpy()
def load_graph_data(dataset_name, show_details=False):
"""
load graph data
:param dataset_name: the name of the dataset
:param show_details: if show the details of dataset
- dataset name
- features' shape
- labels' shape
- adj shape
- edge num
- category num
- category distribution
:return: the features, labels and adj
"""
load_path = "dataset/" + dataset_name + "/" + dataset_name
feat = np.load(load_path+"_feat.npy", allow_pickle=True)
label = np.load(load_path+"_label.npy", allow_pickle=True)
adj = np.load(load_path+"_adj.npy", allow_pickle=True)
if show_details:
print("++++++++++++++++++++++++++++++")
print("---details of graph dataset---")
print("++++++++++++++++++++++++++++++")
print("dataset name: ", dataset_name)
print("feature shape: ", feat.shape)
print("label shape: ", label.shape)
print("adj shape: ", adj.shape)
print("undirected edge num: ", int(np.nonzero(adj)[0].shape[0]/2))
print("category num: ", max(label)-min(label)+1)
print("category distribution: ")
for i in range(max(label)+1):
print("label", i, end=":")
print(len(label[np.where(label == i)]))
print("++++++++++++++++++++++++++++++")
# X pre-processing
pca = PCA(n_components=opt.args.n_input)
feat = pca.fit_transform(feat)
return feat, label, adj
def normalize_adj(adj, self_loop=True, symmetry=False):
"""
normalize the adj matrix
:param adj: input adj matrix
:param self_loop: if add the self loop or not
:param symmetry: symmetry normalize or not
:return: the normalized adj matrix
"""
# add the self_loop
if self_loop:
adj_tmp = adj + np.eye(adj.shape[0])
else:
adj_tmp = adj
# calculate degree matrix and it's inverse matrix
d = np.diag(adj_tmp.sum(0))
d_inv = np.linalg.inv(d)
# symmetry normalize: D^{-0.5} A D^{-0.5}
if symmetry:
sqrt_d_inv = np.sqrt(d_inv)
norm_adj = np.matmul(np.matmul(sqrt_d_inv, adj_tmp), adj_tmp)
# non-symmetry normalize: D^{-1} A
else:
norm_adj = np.matmul(d_inv, adj_tmp)
return norm_adj
def gaussian_noised_feature(X):
"""
add gaussian noise to the attribute matrix X
Args:
X: the attribute matrix
Returns: the noised attribute matrix X_tilde
"""
N_1 = torch.Tensor(np.random.normal(1, 0.1, X.shape)).to(opt.args.device)
N_2 = torch.Tensor(np.random.normal(1, 0.1, X.shape)).to(opt.args.device)
X_tilde1 = X * N_1
X_tilde2 = X * N_2
return X_tilde1, X_tilde2
def diffusion_adj(adj, mode="ppr", transport_rate=0.2):
"""
graph diffusion
:param adj: input adj matrix
:param mode: the mode of graph diffusion
:param transport_rate: the transport rate
- personalized page rank
-
:return: the graph diffusion
"""
# add the self_loop
adj_tmp = adj + np.eye(adj.shape[0])
# calculate degree matrix and it's inverse matrix
d = np.diag(adj_tmp.sum(0))
d_inv = np.linalg.inv(d)
sqrt_d_inv = np.sqrt(d_inv)
# calculate norm adj
norm_adj = np.matmul(np.matmul(sqrt_d_inv, adj_tmp), sqrt_d_inv)
# calculate graph diffusion
if mode == "ppr":
diff_adj = transport_rate * np.linalg.inv((np.eye(d.shape[0]) - (1 - transport_rate) * norm_adj))
return diff_adj
def remove_edge(A, similarity, remove_rate=0.1):
"""
remove edge based on embedding similarity
Args:
A: the origin adjacency matrix
similarity: cosine similarity matrix of embedding
remove_rate: the rate of removing linkage relation
Returns:
Am: edge-masked adjacency matrix
"""
# remove edges based on cosine similarity of embedding
n_node = A.shape[0]
for i in range(n_node):
A[i, torch.argsort(similarity[i].cpu())[:int(round(remove_rate * n_node))]] = 0
# normalize adj
Am = normalize_adj(A, self_loop=True, symmetry=False)
Am = numpy_to_torch(Am).to(opt.args.device)
return Am
def load_pretrain_parameter(model):
"""
load pretrained parameters
Args:
model: Dual Correlation Reduction Network
Returns: model
"""
pretrained_dict = torch.load('model_pretrain/{}_pretrain.pkl'.format(opt.args.name), map_location='cpu')
model_dict = model.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
return model
def model_init(model, X, y, A_norm):
"""
load the pre-train model and calculate similarity and cluster centers
Args:
model: Dual Correlation Reduction Network
X: input feature matrix
y: input label
A_norm: normalized adj
Returns: embedding similarity matrix
"""
# load pre-train model
model = load_pretrain_parameter(model)
# calculate embedding similarity
with torch.no_grad():
_, _, _, sim, _, _, _, Z, _, _ = model(X, A_norm, X, A_norm)
# calculate cluster centers
acc, nmi, ari, f1, centers = clustering(Z, y)
return sim, centers
# the reconstruction function from DFCN
def reconstruction_loss(X, A_norm, X_hat, Z_hat, A_hat):
"""
reconstruction loss L_{}
Args:
X: the origin feature matrix
A_norm: the normalized adj
X_hat: the reconstructed X
Z_hat: the reconstructed Z
A_hat: the reconstructed A
Returns: the reconstruction loss
"""
loss_ae = F.mse_loss(X_hat, X)
loss_w = F.mse_loss(Z_hat, torch.spmm(A_norm, X))
loss_a = F.mse_loss(A_hat, A_norm.to_dense())
loss_igae = loss_w + 0.1 * loss_a
loss_rec = loss_ae + loss_igae
return loss_rec
def target_distribution(Q):
"""
calculate the target distribution (student-t distribution)
Args:
Q: the soft assignment distribution
Returns: target distribution P
"""
weight = Q ** 2 / Q.sum(0)
P = (weight.t() / weight.sum(1)).t()
return P
# clustering guidance from DFCN
def distribution_loss(Q, P):
"""
calculate the clustering guidance loss L_{KL}
Args:
Q: the soft assignment distribution
P: the target distribution
Returns: L_{KL}
"""
loss = F.kl_div((Q[0].log() + Q[1].log() + Q[2].log()) / 3, P, reduction='batchmean')
return loss
def r_loss(AZ, Z):
"""
the loss of propagated regularization (L_R)
Args:
AZ: the propagated embedding
Z: embedding
Returns: L_R
"""
loss = 0
for i in range(2):
for j in range(3):
p_output = F.softmax(AZ[i][j], dim=1)
q_output = F.softmax(Z[i][j], dim=1)
log_mean_output = ((p_output + q_output) / 2).log()
loss += (F.kl_div(log_mean_output, p_output, reduction='batchmean') +
F.kl_div(log_mean_output, p_output, reduction='batchmean')) / 2
return loss
def off_diagonal(x):
"""
off-diagonal elements of x
Args:
x: the input matrix
Returns: the off-diagonal elements of x
"""
n, m = x.shape
assert n == m
return x.flatten()[:-1].view(n - 1, n + 1)[:, 1:].flatten()
def cross_correlation(Z_v1, Z_v2):
"""
calculate the cross-view correlation matrix S
Args:
Z_v1: the first view embedding
Z_v2: the second view embedding
Returns: S
"""
return torch.mm(F.normalize(Z_v1, dim=1), F.normalize(Z_v2, dim=1).t())
def correlation_reduction_loss(S):
"""
the correlation reduction loss L: MSE for S and I (identical matrix)
Args:
S: the cross-view correlation matrix S
Returns: L
"""
return torch.diagonal(S).add(-1).pow(2).mean() + off_diagonal(S).pow(2).mean()
def dicr_loss(Z_ae, Z_igae, AZ, Z):
"""
Dual Information Correlation Reduction loss L_{DICR}
Args:
Z_ae: AE embedding including two-view node embedding [0, 1] and two-view cluster-level embedding [2, 3]
Z_igae: IGAE embedding including two-view node embedding [0, 1] and two-view cluster-level embedding [2, 3]
AZ: the propagated fusion embedding AZ
Z: the fusion embedding Z
Returns:
L_{DICR}
"""
# Sample-level Correlation Reduction (SCR)
# cross-view sample correlation matrix
S_N_ae = cross_correlation(Z_ae[0], Z_ae[1])
S_N_igae = cross_correlation(Z_igae[0], Z_igae[1])
# loss of SCR
L_N_ae = correlation_reduction_loss(S_N_ae)
L_N_igae = correlation_reduction_loss(S_N_igae)
# Feature-level Correlation Reduction (FCR)
# cross-view feature correlation matrix
S_F_ae = cross_correlation(Z_ae[2].t(), Z_ae[3].t())
S_F_igae = cross_correlation(Z_igae[2].t(), Z_igae[3].t())
# loss of FCR
L_F_ae = correlation_reduction_loss(S_F_ae)
L_F_igae = correlation_reduction_loss(S_F_igae)
if opt.args.name == "dblp" or opt.args.name == "acm":
L_N = 0.01 * L_N_ae + 10 * L_N_igae
L_F = 0.5 * L_F_ae + 0.5 * L_F_igae
else:
L_N = 0.1 * L_N_ae + 5 * L_N_igae
L_F = L_F_ae + L_F_igae
# propagated regularization
L_R = r_loss(AZ, Z)
# loss of DICR
loss_dicr = L_N + L_F + opt.args.gamma_value * L_R
return loss_dicr
def clustering(Z, y):
"""
clustering based on embedding
Args:
Z: the input embedding
y: the ground truth
Returns: acc, nmi, ari, f1, clustering centers
"""
model = KMeans(n_clusters=opt.args.n_clusters, n_init=20)
cluster_id = model.fit_predict(Z.data.cpu().numpy())
acc, nmi, ari, f1 = eva(y, cluster_id, show_details=opt.args.show_training_details)
return acc, nmi, ari, f1, model.cluster_centers_
def cluster_acc(y_true, y_pred):
"""
calculate clustering acc and f1-score
Args:
y_true: the ground truth
y_pred: the clustering id
Returns: acc and f1-score
"""
y_true = y_true - np.min(y_true)
l1 = list(set(y_true))
num_class1 = len(l1)
l2 = list(set(y_pred))
num_class2 = len(l2)
ind = 0
if num_class1 != num_class2:
for i in l1:
if i in l2:
pass
else:
y_pred[ind] = i
ind += 1
l2 = list(set(y_pred))
numclass2 = len(l2)
if num_class1 != numclass2:
print('error')
return
cost = np.zeros((num_class1, numclass2), dtype=int)
for i, c1 in enumerate(l1):
mps = [i1 for i1, e1 in enumerate(y_true) if e1 == c1]
for j, c2 in enumerate(l2):
mps_d = [i1 for i1 in mps if y_pred[i1] == c2]
cost[i][j] = len(mps_d)
m = Munkres()
cost = cost.__neg__().tolist()
indexes = m.compute(cost)
new_predict = np.zeros(len(y_pred))
for i, c in enumerate(l1):
c2 = l2[indexes[i][1]]
ai = [ind for ind, elm in enumerate(y_pred) if elm == c2]
new_predict[ai] = c
acc = metrics.accuracy_score(y_true, new_predict)
f1_macro = metrics.f1_score(y_true, new_predict, average='macro')
return acc, f1_macro
def eva(y_true, y_pred, show_details=True):
"""
evaluate the clustering performance
Args:
y_true: the ground truth
y_pred: the predicted label
show_details: if print the details
Returns: None
"""
acc, f1 = cluster_acc(y_true, y_pred)
nmi = nmi_score(y_true, y_pred, average_method='arithmetic')
ari = ari_score(y_true, y_pred)
if show_details:
print(':acc {:.4f}'.format(acc), ', nmi {:.4f}'.format(nmi), ', ari {:.4f}'.format(ari),
', f1 {:.4f}'.format(f1))
return acc, nmi, ari, f1