forked from yueliu1999/HSAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
231 lines (199 loc) · 7.49 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
import torch
import random
import numpy as np
from opt import args
from sklearn import metrics
from munkres import Munkres
from kmeans_gpu import kmeans
import torch.nn.functional as F
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 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
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, cluster number
"""
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)
cluster_num = len(np.unique(label))
node_num = feat.shape[0]
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("++++++++++++++++++++++++++++++")
if args.n_input != -1:
pca = PCA(n_components=args.n_input)
feat = pca.fit_transform(feat)
return feat, label, torch.tensor(adj).float(), node_num, cluster_num
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), sqrt_d_inv)
# non-symmetry normalize: D^{-1} A
else:
norm_adj = np.matmul(d_inv, adj_tmp)
return norm_adj
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 phi(feature, true_labels, cluster_num):
predict_labels, centers = kmeans(X=feature, num_clusters=cluster_num, distance="euclidean", device="cuda")
acc, nmi, ari, f1 = eva(true_labels, predict_labels.numpy(), show_details=False)
return 100 * acc, 100 * nmi, 100 * ari, 100 * f1, predict_labels.numpy(), centers
def laplacian_filtering(A, X, t):
A_tmp = A - torch.diag_embed(torch.diag(A))
A_norm = normalize_adj(A_tmp, self_loop=True, symmetry=True)
I = torch.eye(A.shape[0])
L = I - A_norm
for i in range(t):
X = (I - L) @ X
return X.float()
def comprehensive_similarity(Z1, Z2, E1, E2, alpha):
Z1_Z2 = torch.cat([torch.cat([Z1 @ Z1.T, Z1 @ Z2.T], dim=1),
torch.cat([Z2 @ Z1.T, Z2 @ Z2.T], dim=1)], dim=0)
E1_E2 = torch.cat([torch.cat([E1 @ E1.T, E1 @ E2.T], dim=1),
torch.cat([E2 @ E1.T, E2 @ E2.T], dim=1)], dim=0)
S = alpha * Z1_Z2 + (1 - alpha) * E1_E2
return S
def hard_sample_aware_infoNCE(S, M, pos_neg_weight, pos_weight, node_num):
pos_neg = M * torch.exp(S * pos_neg_weight)
pos = torch.cat([torch.diag(S, node_num), torch.diag(S, -node_num)], dim=0)
pos = torch.exp(pos * pos_weight)
neg = (torch.sum(pos_neg, dim=1) - pos)
infoNEC = (-torch.log(pos / (pos + neg))).sum() / (2 * node_num)
return infoNEC
def square_euclid_distance(Z, center):
ZZ = (Z * Z).sum(-1).reshape(-1, 1).repeat(1, center.shape[0])
CC = (center * center).sum(-1).reshape(1, -1).repeat(Z.shape[0], 1)
ZZ_CC = ZZ + CC
ZC = Z @ center.T
distance = ZZ_CC - 2 * ZC
return distance
def high_confidence(Z, center):
distance_norm = torch.min(F.softmax(square_euclid_distance(Z, center), dim=1), dim=1).values
value, _ = torch.topk(distance_norm, int(Z.shape[0] * (1 - args.tao)))
index = torch.where(distance_norm <= value[-1],
torch.ones_like(distance_norm), torch.zeros_like(distance_norm))
high_conf_index_v1 = torch.nonzero(index).reshape(-1, )
high_conf_index_v2 = high_conf_index_v1 + Z.shape[0]
H = torch.cat([high_conf_index_v1, high_conf_index_v2], dim=0)
H_mat = np.ix_(H.cpu(), H.cpu())
return H, H_mat
def pseudo_matrix(P, S, node_num):
P = torch.tensor(P)
P = torch.cat([P, P], dim=0)
Q = (P == P.unsqueeze(1)).float().to(args.device)
S_norm = (S - S.min()) / (S.max() - S.min())
M_mat = torch.abs(Q - S_norm) ** args.beta
M = torch.cat([torch.diag(M_mat, node_num), torch.diag(M_mat, -node_num)], dim=0)
return M, M_mat