-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
404 lines (313 loc) · 13.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
import argparse
import torch
import scipy.sparse as sp
from dgl import DGLGraph
from sklearn.model_selection import ShuffleSplit
from tqdm import tqdm
import dgl
from sklearn.metrics import f1_score
import scipy.sparse as sp
import numpy as np
import networkx as nx
from numpy import *
from sklearn.metrics import normalized_mutual_info_score, adjusted_rand_score, jaccard_score
# Training settings
def parse_args():
"""
Generate a parameters parser.
"""
# parse parameters
parser = argparse.ArgumentParser()
# main parameters
parser.add_argument('--name', type=str, default=None)
parser.add_argument('--dataset', type=str, default='cora',
help='Choose from {pubmed}')
parser.add_argument('--device', type=int, default=1,
help='Device cuda id')
parser.add_argument('--seed', type=int, default=0,
help='Random seed.')
# model parameters
parser.add_argument('--hops', type=int, default=7,
help='Hop of neighbors to be calculated')
parser.add_argument('--pe_dim', type=int, default=15,
help='position embedding size')
parser.add_argument('--hidden_dim', type=int, default=512,
help='Hidden layer size')
parser.add_argument('--ffn_dim', type=int, default=64,
help='FFN layer size')
parser.add_argument('--n_layers', type=int, default=1,
help='Number of Transformer layers')
parser.add_argument('--n_heads', type=int, default=8,
help='Number of Transformer heads')
parser.add_argument('--dropout', type=float, default=0.1,
help='Dropout')
parser.add_argument('--attention_dropout', type=float, default=0.1,
help='Dropout in the attention layer')
parser.add_argument('--readout', type=str, default="mean")
parser.add_argument('--alpha', type=float, default=0.1,
help='the value the balance the loss.')
# training parameters
parser.add_argument('--batch_size', type=int, default=1000,
help='Batch size')
parser.add_argument('--group_epoch_gap', type=int, default=20,
help='Batch size')
parser.add_argument('--epochs', type=int, default=100,
help='Number of epochs to train.')
parser.add_argument('--tot_updates', type=int, default=1000,
help='used for optimizer learning rate scheduling')
parser.add_argument('--warmup_updates', type=int, default=400,
help='warmup steps')
parser.add_argument('--peak_lr', type=float, default=0.001,
help='learning rate')
parser.add_argument('--end_lr', type=float, default=0.0001,
help='learning rate')
parser.add_argument('--weight_decay', type=float, default=0.00001,
help='weight decay')
parser.add_argument('--patience', type=int, default=50,
help='Patience for early stopping')
# model saving
parser.add_argument('--save_path', type=str, default='./model/',
help='The path for the model to save')
parser.add_argument('--model_name', type=str, default='cora',
help='The name for the model to save')
parser.add_argument('--embedding_path', type=str, default='./pretrain_result/',
help='The path for the embedding to save')
return parser.parse_args()
def laplacian_positional_encoding(g, pos_enc_dim):
"""
Graph positional encoding v/ Laplacian eigenvectors
"""
# Laplacian
#adjacency_matrix(transpose, scipy_fmt="csr")
# A = g.adjacency_matrix_scipy(return_edge_ids=False).astype(float)
A = g.adj_external(scipy_fmt='csr')
N = sp.diags(dgl.backend.asnumpy(g.in_degrees()).clip(1) ** -0.5, dtype=float)
L = sp.eye(g.number_of_nodes()) - N * A * N
# Eigenvectors with scipy
#EigVal, EigVec = sp.linalg.eigs(L, k=pos_enc_dim+1, which='SR')
EigVal, EigVec = sp.linalg.eigs(L, k=pos_enc_dim+1, which='SR', tol=1e-2) # for 40 PEs
EigVec = EigVec[:, EigVal.argsort()] # increasing order
lap_pos_enc = torch.from_numpy(EigVec[:,1:pos_enc_dim+1]).float()
return lap_pos_enc
def re_features(adj, features, K):
#传播之后的特征矩阵,size= (N, 1, K+1, d )
nodes_features = torch.empty(features.shape[0], 1, K+1, features.shape[1])
for i in range(features.shape[0]):
nodes_features[i, 0, 0, :] = features[i]
x = features + torch.zeros_like(features)
for i in range(K):
x = torch.matmul(adj, x)
for index in range(features.shape[0]):
nodes_features[index, 0, i + 1, :] = x[index]
nodes_features = nodes_features.squeeze()
return nodes_features
def conductance_hop(adj, max_khop):
adj = adj.to(dtype=torch.float)
adj_current_hop = adj
results = torch.zeros((max_khop+1, adj.shape[0]))
for hop in range(max_khop+1):
adj_current_hop = torch.matmul(adj_current_hop, adj)
degree = torch.sum(adj_current_hop, dim=0)
adj_current_hop_sign = torch.sign(adj_current_hop)
degree_1 = torch.sum(adj_current_hop_sign, dim=0)
results[hop] = (degree-degree_1).to_dense().reshape(1, -1)
hop += 1
results = results.T
max_indices = torch.argmax(results, dim=1)
for i in range(results.shape[0]):
for j in range(results.shape[1]):
if j>max_indices[i] and max_indices[i] != 0:
results[i][j] = 0
else:
results[i][j] = 1
if hop==1:
results==torch.ones((max_khop+1, adj.shape[0]))
return results
# def f1_score_calculation(y_pred, y_true):
# if len(y_pred.shape) == 1:
# y_pred = y_pred.reshape(1, -1)
# y_true = y_true.reshape(1, -1)
# F1 = []
# for i in range(y_pred.shape[0]):
# pre = torch.sum(torch.multiply(y_pred[i], y_true[i]))/(torch.sum(y_pred[i])+1E-9)
# rec = torch.sum(torch.multiply(y_pred[i], y_true[i]))/(torch.sum(y_true[i])+1E-9)
# F1.append(2 * pre * rec / (pre + rec+1E-9))
# return mean(F1)
def f1_score_calculation(y_pred, y_true):
y_pred = y_pred.reshape(1, -1)
y_true = y_true.reshape(1, -1)
pre = torch.sum(torch.multiply(y_pred, y_true))/(torch.sum(y_pred)+1E-9)
rec = torch.sum(torch.multiply(y_pred, y_true))/(torch.sum(y_true)+1E-9)
F1 = 2 * pre * rec / (pre + rec+1E-9)
print("recall: ", rec, "pre: ", pre)
return F1
def evaluation(comm_find, comm):
comm_find = comm_find.reshape(-1)
comm = comm.reshape(-1)
return normalized_mutual_info_score(comm, comm_find), adjusted_rand_score(comm, comm_find), jaccard_score(comm, comm_find)
# def evaluation(comm_find, comm):
# nmi_all, ari_all, jac_all = [], [], []
# for i in range(comm_find.shape[0]):
# nmi_all.append(NMI_score(comm_find[i], comm[i]))
# ari_all.append(ARI_score(comm_find[i], comm[i]))
# jac_all.append(JAC_score(comm_find[i], comm[i]))
# return np.mean(nmi_all), np.mean(ari_all), np.mean(jac_all)
def NMI_score(comm_find, comm):
score = normalized_mutual_info_score(comm, comm_find)
#print("q, nmi:", score)
return score
def ARI_score(comm_find, comm):
score = adjusted_rand_score(comm, comm_find)
#print("q, ari:", score)
return score
def JAC_score(comm_find, comm):
score = jaccard_score(comm, comm_find)
#print("q, jac:", score)
return score
def load_query_n_gt(path, dataset, vec_length):
# load query and ground truth
query = []
file_query = open(path + dataset + '/' + dataset + ".query", 'r')
for line in file_query:
vec = [0 for i in range(vec_length)]
line = line.strip()
line = line.split(" ")
for i in line:
vec[int(i)] = 1
query.append(vec)
gt = []
file_gt = open(path + dataset + '/' + dataset + ".gt", 'r')
for line in file_gt:
vec = [0 for i in range(vec_length)]
line = line.strip()
line = line.split(" ")
for i in line:
vec[int(i)] = 1
gt.append(vec)
return torch.Tensor(query), torch.Tensor(gt)
def get_gt_legnth(path, dataset):
gt_legnth = []
file_gt = open(path + dataset + '/' + dataset + ".gt", 'r')
for line in file_gt:
line = line.strip()
line = line.split(" ")
gt_legnth.append(len(line))
return torch.Tensor(gt_legnth)
def cosin_similarity(query_tensor, emb_tensor):
# similarity = torch.stack([torch.cosine_similarity(query_tensor[i], emb_tensor, dim=1) for i in range(len(query_tensor))], 0)
similarity = torch.stack([torch.cosine_similarity(query_tensor[i].reshape(1, -1), emb_tensor, dim=1) for i in range(len(query_tensor))], 0)
# print(similarity.shape)
return similarity
def dot_similarity(query_tensor, emb_tensor):
similarity = torch.mm(query_tensor, emb_tensor.t()) # (query_num, node_num)
similarity = torch.nn.Softmax(dim=1)(similarity)
return similarity
def transform_coo_to_csr(adj):
row=adj._indices()[0]
col=adj._indices()[1]
data=adj._values()
shape=adj.size()
adj=sp.csr_matrix((data, (row, col)), shape=shape)
return adj
def transform_csr_to_coo(adj, size=None):
adj = adj.tocoo()
adj = torch.sparse.LongTensor(torch.LongTensor([adj.row.tolist(), adj.col.tolist()]),
torch.LongTensor(adj.data.astype(np.int32)),
torch.Size([size, size]))
return adj
def transform_sp_csr_to_coo(adj, batch_size, node_num):
# chunks
node_index = [i for i in range(node_num)]
divide_index = [node_index[i:i+batch_size] for i in range(0, len(node_index), batch_size)]
# adj of each chunks, in the format of sp_csr
print("start mini batch: adj of each chunks")
adj_sp_csr = [adj[divide_index[i]][:, divide_index[i]] for i in range(len(divide_index))]
print("start mini batch: minus adj of each chunks")
minus_adj_sp_csr = [sp.csr_matrix(torch.ones(item.shape))-item for item in adj_sp_csr]
# adj_tensor_coo = [transform_csr_to_coo(item).to_dense() for item in adj_sp_csr]
# minus_adj_tensor_coo = [transform_csr_to_coo(item).to_dense() for item in minus_adj_sp_csr]
print("start mini batch: back to torch coo adj")
adj_tensor_coo = [transform_csr_to_coo(adj_sp_csr[i], len(divide_index[i])).to_dense() for i in range(len(divide_index))]
print("start mini batch: back to torch coo minus adj")
minus_adj_tensor_coo = [transform_csr_to_coo(minus_adj_sp_csr[i], len(divide_index[i])).to_dense() for i in range(len(divide_index))]
return adj_tensor_coo, minus_adj_tensor_coo
# transform coo to edge index in pytorch geometric
def transform_coo_to_edge_index(adj):
adj = adj.coalesce()
edge_index = adj.indices().detach().long()
return edge_index
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_adj_to_scipy(adj):
shape = adj.shape
coords = adj.coalesce().indices()
values = adj.coalesce().values()
scipy_sparse = sp.coo_matrix((values.cpu().numpy(), (coords[0].cpu().numpy(), coords[1].cpu().numpy())), shape=shape)
return scipy_sparse
# determine one edge in edge_index or not of torch geometric
def is_edge_in_edge_index(edge_index, source, target):
mask = (edge_index[0] == source) & (edge_index[1] == target)
return mask.any()
def construct_pseudo_assignment(cluster_ids_x):
pseudo_assignment = torch.zeros(cluster_ids_x.shape[0], int(cluster_ids_x.max()+1))
for i in range(cluster_ids_x.shape[0]):
pseudo_assignment[i][int(cluster_ids_x[i])] = 1
return pseudo_assignment
def pq_computation(similarity):
q = torch.nn.functional.normalize(similarity, dim=1, p=1)
p_temp = torch.mul(q, q)
q_colsum = torch.sum(q, axis=0)
p_temp = torch.div(p_temp,q_colsum)
p = torch.nn.functional.normalize(p_temp, dim=1, p=1)
return q, p
def coo_matrix_to_nx_graph(matrix):
# Create an empty NetworkX graph
graph = nx.Graph()
# Get the number of nodes in the COO matrix
num_nodes = matrix.shape[0]
# Convert the COO matrix to a dense matrix
dense_matrix = matrix.to_dense()
# Iterate over the non-zero entries in the dense matrix
for i in range(num_nodes):
for j in range(num_nodes):
if dense_matrix[i][j] != 0:
# Add an edge to the NetworkX graph
graph.add_edge(i, j)
graph.add_edge(j, i)
return graph
def coo_matrix_to_nx_graph_efficient(adj_matrix):
# 创建一个无向图对象
graph = nx.Graph()
# 获取 COO 矩阵的行和列索引以及权重值
adj_matrix = adj_matrix.coalesce()
rows = adj_matrix.indices()[0]
cols = adj_matrix.indices()[1]
# 添加节点和边到图中
for i in range(len(rows)):
graph.add_edge(int(rows[i]), int(cols[i]))
graph.add_edge(int(cols[i]), int(rows[i]))
return graph
def obtain_adj_from_nx(graph):
return np.array(nx.adjacency_matrix(graph, nodelist=[i for i in range(max(graph.nodes)+1)]).todense())
def find_all_neighbors_bynx(query, Graph):
nodes = Graph.nodes()
neighbors = []
for i in range(len(query)):
if query[i] not in nodes:
continue
for j in Graph.neighbors(query[i]):
if j not in query:
neighbors.append(j)
return neighbors
def MaxMinNormalization(x, Min, Max):
x = np.array(x)
x_max = np.max(x)
x_min = np.min(x)
x = [(item-x_min)*(Max-Min)/(x_max - x_min) + Min for item in x]
return x