-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
461 lines (380 loc) · 15.9 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
import pickle as pkl
import networkx as nx
import scipy.io as sio
import numpy as np
import scipy.sparse as sp
import torch
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.cluster import SpectralClustering
from sklearn.metrics import roc_auc_score, average_precision_score, euclidean_distances
import sklearn.preprocessing as preprocess
from clustering_metric import clustering_metrics
def sample_mask(idx, l):
"""Create mask."""
mask = np.zeros(l)
mask[idx] = 1
return np.array(mask, dtype=np.bool_)
def load_data(dataset):
# load the data: x, tx, allx, graph
names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
objects = []
if dataset == 'wiki':
adj, features, label = loadwiki()
return adj, features, label, 0, 0, 0
for i in range(len(names)):
'''
fix Pickle incompatibility of numpy arrays between Python 2 and 3
https://stackoverflow.com/questions/11305790/pickle-incompatibility-of-numpy-arrays-between-python-2-and-3
'''
with open("data/ind.{}.{}".format(dataset, names[i]), 'rb') as rf:
u = pkl._Unpickler(rf)
u.encoding = 'latin1'
cur_data = u.load()
objects.append(cur_data)
# objects.append(
# pkl.load(open("data/ind.{}.{}".format(dataset, names[i]), 'rb')))
x, y, tx, ty, allx, ally, graph = tuple(objects)
test_idx_reorder = parse_index_file(
"data/ind.{}.test.index".format(dataset))
test_idx_range = np.sort(test_idx_reorder)
if dataset == 'citeseer':
# Fix citeseer dataset (there are some isolated nodes in the graph)
# Find isolated nodes, add them as zero-vecs into the right position
test_idx_range_full = range(
min(test_idx_reorder), max(test_idx_reorder) + 1)
tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1]))
tx_extended[test_idx_range - min(test_idx_range), :] = tx
tx = tx_extended
ty_extended = np.zeros((len(test_idx_range_full), y.shape[1]))
ty_extended[test_idx_range - min(test_idx_range), :] = ty
ty = ty_extended
features = sp.vstack((allx, tx)).tolil()
features[test_idx_reorder, :] = features[test_idx_range, :]
features = torch.FloatTensor(np.array(features.todense()))
adj = nx.adjacency_matrix(nx.from_dict_of_lists(graph))
labels = np.vstack((ally, ty))
labels[test_idx_reorder, :] = labels[test_idx_range, :]
idx_test = test_idx_range.tolist()
idx_train = range(len(y))
idx_val = range(len(y), len(y) + 500)
train_mask = sample_mask(idx_train, labels.shape[0])
val_mask = sample_mask(idx_val, labels.shape[0])
test_mask = sample_mask(idx_test, labels.shape[0])
y_train = np.zeros(labels.shape)
y_val = np.zeros(labels.shape)
y_test = np.zeros(labels.shape)
y_train[train_mask, :] = labels[train_mask, :]
y_val[val_mask, :] = labels[val_mask, :]
y_test[test_mask, :] = labels[test_mask, :]
return adj, features, np.argmax(labels, 1), idx_train, idx_val, idx_test
def load_wiki():
f = open('data/graph.txt','r')
adj, xind, yind = [], [], []
for line in f.readlines():
line = line.split()
xind.append(int(line[0]))
yind.append(int(line[1]))
adj.append([int(line[0]), int(line[1])])
f.close()
##print(len(adj))
f = open('data/group.txt','r')
label = []
for line in f.readlines():
line = line.split()
label.append(int(line[1]))
f.close()
f = open('data/tfidf.txt','r')
fea_idx = []
fea = []
adj = np.array(adj)
adj = np.vstack((adj, adj[:,[1,0]]))
adj = np.unique(adj, axis=0)
labelset = np.unique(label)
labeldict = dict(zip(labelset, range(len(labelset))))
label = np.array([labeldict[x] for x in label])
adj = sp.csr_matrix((np.ones(len(adj)), (adj[:,0], adj[:,1])), shape=(len(label), len(label)))
for line in f.readlines():
line = line.split()
fea_idx.append([int(line[0]), int(line[1])])
fea.append(float(line[2]))
f.close()
fea_idx = np.array(fea_idx)
features = sp.csr_matrix((fea, (fea_idx[:,0], fea_idx[:,1])), shape=(len(label), 4973)).toarray()
scaler = preprocess.MinMaxScaler()
#features = preprocess.normalize(features, norm='l2')
features = scaler.fit_transform(features)
features = torch.FloatTensor(features)
return adj, features, label
def loadwiki():
dataset = 'wiki'
data = sio.loadmat('data/{}.mat'.format(dataset))
features = data['fea'] # 每个结点的特征是1433维,2708*14336
if sp.issparse(features): # 检查是否是稀疏矩阵
feature = features.todense() # 转换成密集对象
adj = data['W'] # 2708*2708 两个结点之间是否有边
label = data['gnd'] # 标签2708*1,结点属于哪个类别
label = label.T
label = label - 1 # 原来的gnd类别是1-7 变成0-6
label = label[0, :]
adj = sp.coo_matrix(adj) # coo_matrix是最简单的稀疏矩阵存储方式,采用三元组(row, col, data)
return adj,features,label
def parse_index_file(filename):
index = []
for line in open(filename):
index.append(int(line.strip()))
return index
def sparse_to_tuple(sparse_mx):
if not sp.isspmatrix_coo(sparse_mx):
sparse_mx = sparse_mx.tocoo()
coords = np.vstack((sparse_mx.row, sparse_mx.col)).transpose()
values = sparse_mx.data
shape = sparse_mx.shape
return coords, values, shape
def mask_test_edges(adj):
# Function to build test set with 10% positive links
# NOTE: Splits are randomized and results might slightly deviate from reported numbers in the paper.
# TODO: Clean up.
# Remove diagonal elements
adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)
adj.eliminate_zeros()
# Check that diag is zero:
assert np.diag(adj.todense()).sum() == 0
adj_triu = sp.triu(adj)
adj_tuple = sparse_to_tuple(adj_triu)
edges = adj_tuple[0]
edges_all = sparse_to_tuple(adj)[0]
num_test = int(np.floor(edges.shape[0] / 10.))
num_val = int(np.floor(edges.shape[0] / 20.))
all_edge_idx = list(range(edges.shape[0]))
np.random.shuffle(all_edge_idx)
val_edge_idx = all_edge_idx[:num_val]
test_edge_idx = all_edge_idx[num_val:(num_val + num_test)]
test_edges = edges[test_edge_idx]
val_edges = edges[val_edge_idx]
train_edges = np.delete(edges, np.hstack([test_edge_idx, val_edge_idx]), axis=0)
def ismember(a, b, tol=5):
rows_close = np.all(np.round(a - b[:, None], tol) == 0, axis=-1)
return np.any(rows_close)
test_edges_false = []
while len(test_edges_false) < len(test_edges):
idx_i = np.random.randint(0, adj.shape[0])
idx_j = np.random.randint(0, adj.shape[0])
if idx_i == idx_j:
continue
if ismember([idx_i, idx_j], edges_all):
continue
if test_edges_false:
if ismember([idx_j, idx_i], np.array(test_edges_false)):
continue
if ismember([idx_i, idx_j], np.array(test_edges_false)):
continue
test_edges_false.append([idx_i, idx_j])
val_edges_false = []
while len(val_edges_false) < len(val_edges):
idx_i = np.random.randint(0, adj.shape[0])
idx_j = np.random.randint(0, adj.shape[0])
if idx_i == idx_j:
continue
if ismember([idx_i, idx_j], train_edges):
continue
if ismember([idx_j, idx_i], train_edges):
continue
if ismember([idx_i, idx_j], val_edges):
continue
if ismember([idx_j, idx_i], val_edges):
continue
if val_edges_false:
if ismember([idx_j, idx_i], np.array(val_edges_false)):
continue
if ismember([idx_i, idx_j], np.array(val_edges_false)):
continue
val_edges_false.append([idx_i, idx_j])
#assert ~ismember(test_edges_false, edges_all)
#assert ~ismember(val_edges_false, edges_all)
#assert ~ismember(val_edges, train_edges)
#assert ~ismember(test_edges, train_edges)
#assert ~ismember(val_edges, test_edges)
data = np.ones(train_edges.shape[0])
# Re-build adj matrix
adj_train = sp.csr_matrix((data, (train_edges[:, 0], train_edges[:, 1])), shape=adj.shape)
adj_train = adj_train + adj_train.T
# NOTE: these edge lists only contain single direction of edge!
return adj_train, train_edges, val_edges, val_edges_false, test_edges, test_edges_false
#数据集的特征值-频率图
def decompose(adj, dataset, norm='sym', renorm=True):
adj = sp.coo_matrix(adj)
ident = sp.eye(adj.shape[0])
if renorm:
adj_ = adj + ident
else:
adj_ = adj
rowsum = np.array(adj_.sum(1))
if norm == 'sym':
degree_mat_inv_sqrt = sp.diags(np.power(rowsum, -0.5).flatten())
adj_normalized = adj_.dot(degree_mat_inv_sqrt).transpose().dot(degree_mat_inv_sqrt).tocoo()
laplacian = ident - adj_normalized
evalue, evector = np.linalg.eig(laplacian.toarray())
np.save(dataset + ".npy", evalue)
#print(max(evalue))
exit(1)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
n, bins, patches = ax.hist(evalue, 50, facecolor='g')
plt.xlabel('Eigenvalues')
plt.ylabel('Frequncy')
fig.savefig("eig_renorm_" + dataset + ".png")
def preprocess_graph(adj, layer, norm='sym', renorm=True):
adj = sp.coo_matrix(adj) #sp.coo_matrix() 的作用是生成矩阵
ident = sp.eye(adj.shape[0]) #sp.eye 创建单位矩阵
if renorm:
adj_ = adj + ident #考虑自身后的邻接矩阵 A~ = A + E
else:
adj_ = adj
rowsum = np.array(adj_.sum(1)) #求出来每一行的度
if norm == 'sym':
degree_mat_inv_sqrt = sp.diags(np.power(rowsum, -0.5).flatten()) #
adj_normalized = adj_.dot(degree_mat_inv_sqrt).transpose().dot(degree_mat_inv_sqrt).tocoo()
laplacian = ident - adj_normalized #标准化后的拉普拉斯
elif norm == 'left':
degree_mat_inv_sqrt = sp.diags(np.power(rowsum, -1.).flatten())
adj_normalized = degree_mat_inv_sqrt.dot(adj_).tocoo()
laplacian = ident - adj_normalized
reg = [2/3] * (layer) #[0.6666666666666666, 0.6666666666666666, 0.6666666666666666]
adjs = []
for i in range(len(reg)):
adjs.append(ident-(reg[i] * laplacian))
return adjs
#普通laplacian:D-A
def laplacian(adj):
rowsum = np.array(adj.sum(1))
degree_mat = sp.diags(rowsum.flatten())
lap = degree_mat - adj
return torch.FloatTensor(lap.toarray())
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)
#AUC AP 值
def get_roc_score(emb, adj_orig, edges_pos, edges_neg):
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Predict on test set of edges
adj_rec = np.dot(emb, emb.T)
preds = []
pos = []
for e in edges_pos:
preds.append(sigmoid(adj_rec[e[0], e[1]]))
pos.append(adj_orig[e[0], e[1]])
preds_neg = []
neg = []
for e in edges_neg:
preds_neg.append(sigmoid(adj_rec[e[0], e[1]]))
neg.append(adj_orig[e[0], e[1]])
preds_all = np.hstack([preds, preds_neg])
labels_all = np.hstack([np.ones(len(preds)), np.zeros(len(preds))])
roc_score = roc_auc_score(labels_all, preds_all)
ap_score = average_precision_score(labels_all, preds_all)
return roc_score, ap_score
#-----------------------------------------------
def normalize_adj(adj, type='sym'):
"""Symmetrically normalize adjacency matrix.对称归一化邻接矩阵"""
if type == 'sym':
adj = sp.coo_matrix(adj)
rowsum = np.array(adj.sum(1)) #求每一行的和
# d_inv_sqrt = np.power(rowsum, -0.5)
# d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
# return adj*d_inv_sqrt*d_inv_sqrt.flatten()
d_inv_sqrt = np.power(rowsum, -0.5).flatten() #开方 flatten()是对多维数据的降维函数
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0. # isinf 确定数组元素是否为无限值
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return d_mat_inv_sqrt.dot(adj).dot(d_mat_inv_sqrt).tocoo() # .tocoo()将稠密矩阵转为稀疏矩阵
elif type == 'rw':
rowsum = np.array(adj.sum(1))
d_inv = np.power(rowsum, -1.0).flatten()
d_inv[np.isinf(d_inv)] = 0.
d_mat_inv = sp.diags(d_inv)
adj_normalized = d_mat_inv.dot(adj)
return adj_normalized
def preprocess_adj(adj, type='sym', loop=True):
"""Preprocessing of adjacency matrix for simple GCN model and conversion to tuple representation."""
# print(adj)
# print(adj.shape[0]) #2708
if loop:
adj = adj + sp.eye(adj.shape[0])
adj_normalized = normalize_adj(adj, type=type)
return adj_normalized
def to_onehot(prelabel):
k = len(np.unique(prelabel))
label = np.zeros([prelabel.shape[0], k])
label[range(prelabel.shape[0]), prelabel] = 1
label = label.T
return label
def square_dist(prelabel, feature):
if sp.issparse(feature):
feature = feature.todense()
feature = np.array(feature)
onehot = to_onehot(prelabel)
m, n = onehot.shape
count = onehot.sum(1).reshape(m, 1)
count[count==0] = 1
mean = onehot.dot(feature)/count
a2 = (onehot.dot(feature*feature)/count).sum(1)
pdist2 = np.array(a2 + a2.T - 2*mean.dot(mean.T))
intra_dist = pdist2.trace()
inter_dist = pdist2.sum() - intra_dist
intra_dist /= m
inter_dist /= m * (m - 1)
return intra_dist
def dist(prelabel, feature):
k = len(np.unique(prelabel))
intra_dist = 0
for i in range(k):
Data_i = feature[np.where(prelabel == i)]
Dis = euclidean_distances(Data_i, Data_i)
n_i = Data_i.shape[0]
if n_i == 0 or n_i == 1:
intra_dist = intra_dist
else:
intra_dist = intra_dist + 1 / k * 1 / (n_i * (n_i - 1)) * sum(sum(Dis))
return intra_dist
def getbestlayer(adj,feature,n_clusters):
tt = 0
rep = 10
max_layer = 60 # 最大层数
adj_normalized = preprocess_adj(adj)
adj_normalized = (sp.eye(adj_normalized.shape[0]) + adj_normalized) / 2
intra_list = []
intra_list.append(10000)
while 1:
tt = tt + 1
power = tt
intraD = np.zeros(rep)
feature = adj_normalized.dot(feature)
#As the data features are nonnegative (the filtered features are also nonnegative), the similarity matrix W is the kernel matrix XX^T. Learning the eigenvectors of the kernel matrix XX^T is equivalent to computing the left singular vectors of X by SVD.
u, s, v = sp.linalg.svds(feature, k=n_clusters, which='LM') # 稀疏矩阵的奇异值分解 要找到哪些k个奇异值:最大量级 ('LM') 或最小量级 ('SM') 奇异值
for i in range(rep):
# kmeans = KMeans(n_clusters=n_clusters).fit(u)
# predict_labels = kmeans.predict(u)
##使用谱聚类
sc = SpectralClustering(n_clusters=n_clusters, affinity='precomputed', random_state=0)
similarity = np.matmul(u, np.transpose(u))
predict_labels = sc.fit_predict(similarity)
intraD[i] = square_dist(predict_labels, feature)
intramean = np.mean(intraD) # mean函数求均值
intra_list.append(intramean)
print('layer: {}'.format(power),
'intra_dist: {}'.format(intramean))
if intra_list[tt] > intra_list[tt - 1] or tt > max_layer:
print('bestlayer: {}'.format(tt - 1))
break
return tt-1
def Accuracy(output, labels):
preds = output.max(1)[1].type_as(labels)
correct = preds.eq(labels).double()
correct = correct.sum()
return correct / len(labels)