-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils.py
400 lines (300 loc) · 14.5 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
import argparse
import scipy.sparse as sp
import numpy as np
import torch
import ipdb
from scipy.io import loadmat
import networkx as nx
import multiprocessing as mp
import torch.nn.functional as F
from functools import partial
import random
from sklearn.metrics import roc_auc_score, f1_score
from copy import deepcopy
from scipy.spatial.distance import pdist,squareform
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--no-cuda', action='store_true', default=False,
help='Disables CUDA training.')
parser.add_argument('--seed', type=int, default=42)
parser.add_argument('--nhid', type=int, default=64)
parser.add_argument('--dataset', type=str, default='cora')
parser.add_argument('--size', type=int, default=100)
parser.add_argument('--epochs', type=int, default=2010,
help='Number of epochs to train.')
parser.add_argument('--lr', type=float, default=0.001)
parser.add_argument('--weight_decay', type=float, default=5e-4)
parser.add_argument('--dropout', type=float, default=0.1)
parser.add_argument('--batch_nums', type=int, default=6000, help='number of batches per epoch')
parser.add_argument('--batch_size', type=int, default=40, help='number of batches per epoch')
parser.add_argument('--imbalance', action='store_true', default=False)
parser.add_argument('--setting', type=str, default='no',
choices=['no','upsampling', 'smote','reweight','embed_up', 'recon','newG_cls','recon_newG'])
#upsampling: oversample in the raw input; smote: ; reweight: reweight minority classes;
# embed_up:
# recon: pretrain; newG_cls: pretrained decoder; recon_newG: also finetune the decoder
parser.add_argument('--opt_new_G', action='store_true', default=False) # whether optimize the decoded graph based on classification result.
parser.add_argument('--load', type=str, default=None)
parser.add_argument('--up_scale', type=float, default=1)
parser.add_argument('--im_ratio', type=float, default=0.5)
parser.add_argument('--rec_weight', type=float, default=0.000001)
parser.add_argument('--model', type=str, default='sage',
choices=['sage','gcn','GAT'])
return parser
def split_arti(labels, c_train_num):
#labels: n-dim Longtensor, each element in [0,...,m-1].
#cora: m=7
num_classes = len(set(labels.tolist()))
c_idxs = [] # class-wise index
train_idx = []
val_idx = []
test_idx = []
c_num_mat = np.zeros((num_classes,3)).astype(int)
c_num_mat[:,1] = 25
c_num_mat[:,2] = 55
for i in range(num_classes):
c_idx = (labels==i).nonzero()[:,-1].tolist()
print('{:d}-th class sample number: {:d}'.format(i,len(c_idx)))
random.shuffle(c_idx)
c_idxs.append(c_idx)
train_idx = train_idx + c_idx[:c_train_num[i]]
c_num_mat[i,0] = c_train_num[i]
val_idx = val_idx + c_idx[c_train_num[i]:c_train_num[i]+25]
test_idx = test_idx + c_idx[c_train_num[i]+25:c_train_num[i]+80]
random.shuffle(train_idx)
#ipdb.set_trace()
train_idx = torch.LongTensor(train_idx)
val_idx = torch.LongTensor(val_idx)
test_idx = torch.LongTensor(test_idx)
#c_num_mat = torch.LongTensor(c_num_mat)
return train_idx, val_idx, test_idx, c_num_mat
def split_genuine(labels):
#labels: n-dim Longtensor, each element in [0,...,m-1].
#cora: m=7
num_classes = len(set(labels.tolist()))
c_idxs = [] # class-wise index
train_idx = []
val_idx = []
test_idx = []
c_num_mat = np.zeros((num_classes,3)).astype(int)
for i in range(num_classes):
c_idx = (labels==i).nonzero()[:,-1].tolist()
c_num = len(c_idx)
print('{:d}-th class sample number: {:d}'.format(i,len(c_idx)))
random.shuffle(c_idx)
c_idxs.append(c_idx)
if c_num <4:
if c_num < 3:
print("too small class type")
ipdb.set_trace()
c_num_mat[i,0] = 1
c_num_mat[i,1] = 1
c_num_mat[i,2] = 1
else:
c_num_mat[i,0] = int(c_num/4)
c_num_mat[i,1] = int(c_num/4)
c_num_mat[i,2] = int(c_num/2)
train_idx = train_idx + c_idx[:c_num_mat[i,0]]
val_idx = val_idx + c_idx[c_num_mat[i,0]:c_num_mat[i,0]+c_num_mat[i,1]]
test_idx = test_idx + c_idx[c_num_mat[i,0]+c_num_mat[i,1]:c_num_mat[i,0]+c_num_mat[i,1]+c_num_mat[i,2]]
random.shuffle(train_idx)
#ipdb.set_trace()
train_idx = torch.LongTensor(train_idx)
val_idx = torch.LongTensor(val_idx)
test_idx = torch.LongTensor(test_idx)
#c_num_mat = torch.LongTensor(c_num_mat)
return train_idx, val_idx, test_idx, c_num_mat
def print_edges_num(dense_adj, labels):
c_num = labels.max().item()+1
dense_adj = np.array(dense_adj)
labels = np.array(labels)
for i in range(c_num):
for j in range(c_num):
#ipdb.set_trace()
row_ind = labels == i
col_ind = labels == j
edge_num = dense_adj[row_ind].transpose()[col_ind].sum()
print("edges between class {:d} and class {:d}: {:f}".format(i,j,edge_num))
def accuracy(output, labels):
preds = output.max(1)[1].type_as(labels)
correct = preds.eq(labels).double()
correct = correct.sum()
return correct / len(labels)
def print_class_acc(output, labels, class_num_list, pre='valid'):
pre_num = 0
#print class-wise performance
'''
for i in range(labels.max()+1):
cur_tpr = accuracy(output[pre_num:pre_num+class_num_list[i]], labels[pre_num:pre_num+class_num_list[i]])
print(str(pre)+" class {:d} True Positive Rate: {:.3f}".format(i,cur_tpr.item()))
index_negative = labels != i
labels_negative = labels.new(labels.shape).fill_(i)
cur_fpr = accuracy(output[index_negative,:], labels_negative[index_negative])
print(str(pre)+" class {:d} False Positive Rate: {:.3f}".format(i,cur_fpr.item()))
pre_num = pre_num + class_num_list[i]
'''
#ipdb.set_trace()
if labels.max() > 1:
auc_score = roc_auc_score(labels.detach(), F.softmax(output, dim=-1).detach(), average='macro', multi_class='ovr')
else:
auc_score = roc_auc_score(labels.detach(), F.softmax(output, dim=-1)[:,1].detach(), average='macro')
macro_F = f1_score(labels.detach(), torch.argmax(output, dim=-1).detach(), average='macro')
print(str(pre)+' current auc-roc score: {:f}, current macro_F score: {:f}'.format(auc_score,macro_F))
return
def src_upsample(adj,features,labels,idx_train, portion=1.0, im_class_num=3):
c_largest = labels.max().item()
adj_back = adj.to_dense()
chosen = None
#ipdb.set_trace()
avg_number = int(idx_train.shape[0]/(c_largest+1))
for i in range(im_class_num):
new_chosen = idx_train[(labels==(c_largest-i))[idx_train]]
if portion == 0:#refers to even distribution
c_portion = int(avg_number/new_chosen.shape[0])
for j in range(c_portion):
if chosen is None:
chosen = new_chosen
else:
chosen = torch.cat((chosen, new_chosen), 0)
else:
c_portion = int(portion)
portion_rest = portion-c_portion
for j in range(c_portion):
num = int(new_chosen.shape[0])
new_chosen = new_chosen[:num]
if chosen is None:
chosen = new_chosen
else:
chosen = torch.cat((chosen, new_chosen), 0)
num = int(new_chosen.shape[0]*portion_rest)
new_chosen = new_chosen[:num]
if chosen is None:
chosen = new_chosen
else:
chosen = torch.cat((chosen, new_chosen), 0)
add_num = chosen.shape[0]
new_adj = adj_back.new(torch.Size((adj_back.shape[0]+add_num, adj_back.shape[0]+add_num)))
new_adj[:adj_back.shape[0], :adj_back.shape[0]] = adj_back[:,:]
new_adj[adj_back.shape[0]:, :adj_back.shape[0]] = adj_back[chosen,:]
new_adj[:adj_back.shape[0], adj_back.shape[0]:] = adj_back[:,chosen]
new_adj[adj_back.shape[0]:, adj_back.shape[0]:] = adj_back[chosen,:][:,chosen]
#ipdb.set_trace()
features_append = deepcopy(features[chosen,:])
labels_append = deepcopy(labels[chosen])
idx_new = np.arange(adj_back.shape[0], adj_back.shape[0]+add_num)
idx_train_append = idx_train.new(idx_new)
features = torch.cat((features,features_append), 0)
labels = torch.cat((labels,labels_append), 0)
idx_train = torch.cat((idx_train,idx_train_append), 0)
adj = new_adj.to_sparse()
return adj, features, labels, idx_train
def src_smote(adj,features,labels,idx_train, portion=1.0, im_class_num=3):
c_largest = labels.max().item()
adj_back = adj.to_dense()
chosen = None
new_features = None
#ipdb.set_trace()
avg_number = int(idx_train.shape[0]/(c_largest+1))
for i in range(im_class_num):
new_chosen = idx_train[(labels==(c_largest-i))[idx_train]]
if portion == 0:#refers to even distribution
c_portion = int(avg_number/new_chosen.shape[0])
portion_rest = (avg_number/new_chosen.shape[0]) - c_portion
else:
c_portion = int(portion)
portion_rest = portion-c_portion
for j in range(c_portion):
num = int(new_chosen.shape[0])
new_chosen = new_chosen[:num]
chosen_embed = features[new_chosen,:]
distance = squareform(pdist(chosen_embed.cpu().detach()))
np.fill_diagonal(distance,distance.max()+100)
idx_neighbor = distance.argmin(axis=-1)
interp_place = random.random()
embed = chosen_embed + (chosen_embed[idx_neighbor,:]-chosen_embed)*interp_place
if chosen is None:
chosen = new_chosen
new_features = embed
else:
chosen = torch.cat((chosen, new_chosen), 0)
new_features = torch.cat((new_features, embed),0)
num = int(new_chosen.shape[0]*portion_rest)
new_chosen = new_chosen[:num]
chosen_embed = features[new_chosen,:]
distance = squareform(pdist(chosen_embed.cpu().detach()))
np.fill_diagonal(distance,distance.max()+100)
idx_neighbor = distance.argmin(axis=-1)
interp_place = random.random()
embed = chosen_embed + (chosen_embed[idx_neighbor,:]-chosen_embed)*interp_place
if chosen is None:
chosen = new_chosen
new_features = embed
else:
chosen = torch.cat((chosen, new_chosen), 0)
new_features = torch.cat((new_features, embed),0)
add_num = chosen.shape[0]
new_adj = adj_back.new(torch.Size((adj_back.shape[0]+add_num, adj_back.shape[0]+add_num)))
new_adj[:adj_back.shape[0], :adj_back.shape[0]] = adj_back[:,:]
new_adj[adj_back.shape[0]:, :adj_back.shape[0]] = adj_back[chosen,:]
new_adj[:adj_back.shape[0], adj_back.shape[0]:] = adj_back[:,chosen]
new_adj[adj_back.shape[0]:, adj_back.shape[0]:] = adj_back[chosen,:][:,chosen]
#ipdb.set_trace()
features_append = deepcopy(new_features)
labels_append = deepcopy(labels[chosen])
idx_new = np.arange(adj_back.shape[0], adj_back.shape[0]+add_num)
idx_train_append = idx_train.new(idx_new)
features = torch.cat((features,features_append), 0)
labels = torch.cat((labels,labels_append), 0)
idx_train = torch.cat((idx_train,idx_train_append), 0)
adj = new_adj.to_sparse()
return adj, features, labels, idx_train
def recon_upsample(embed, labels, idx_train, adj=None, portion=1.0, im_class_num=3):
c_largest = labels.max().item()
avg_number = int(idx_train.shape[0]/(c_largest+1))
#ipdb.set_trace()
adj_new = None
for i in range(im_class_num):
chosen = idx_train[(labels==(c_largest-i))[idx_train]]
num = int(chosen.shape[0]*portion)
if portion == 0:
c_portion = int(avg_number/chosen.shape[0])
num = chosen.shape[0]
else:
c_portion = 1
for j in range(c_portion):
chosen = chosen[:num]
chosen_embed = embed[chosen,:]
distance = squareform(pdist(chosen_embed.cpu().detach()))
np.fill_diagonal(distance,distance.max()+100)
idx_neighbor = distance.argmin(axis=-1)
interp_place = random.random()
new_embed = embed[chosen,:] + (chosen_embed[idx_neighbor,:]-embed[chosen,:])*interp_place
new_labels = labels.new(torch.Size((chosen.shape[0],1))).reshape(-1).fill_(c_largest-i)
idx_new = np.arange(embed.shape[0], embed.shape[0]+chosen.shape[0])
idx_train_append = idx_train.new(idx_new)
embed = torch.cat((embed,new_embed), 0)
labels = torch.cat((labels,new_labels), 0)
idx_train = torch.cat((idx_train,idx_train_append), 0)
if adj is not None:
if adj_new is None:
adj_new = adj.new(torch.clamp_(adj[chosen,:] + adj[idx_neighbor,:], min=0.0, max = 1.0))
else:
temp = adj.new(torch.clamp_(adj[chosen,:] + adj[idx_neighbor,:], min=0.0, max = 1.0))
adj_new = torch.cat((adj_new, temp), 0)
if adj is not None:
add_num = adj_new.shape[0]
new_adj = adj.new(torch.Size((adj.shape[0]+add_num, adj.shape[0]+add_num))).fill_(0.0)
new_adj[:adj.shape[0], :adj.shape[0]] = adj[:,:]
new_adj[adj.shape[0]:, :adj.shape[0]] = adj_new[:,:]
new_adj[:adj.shape[0], adj.shape[0]:] = torch.transpose(adj_new, 0, 1)[:,:]
return embed, labels, idx_train, new_adj.detach()
else:
return embed, labels, idx_train
def adj_mse_loss(adj_rec, adj_tgt, adj_mask = None):
edge_num = adj_tgt.nonzero().shape[0]
total_num = adj_tgt.shape[0]**2
neg_weight = edge_num / (total_num-edge_num)
weight_matrix = adj_rec.new(adj_tgt.shape).fill_(1.0)
weight_matrix[adj_tgt==0] = neg_weight
loss = torch.sum(weight_matrix * (adj_rec - adj_tgt) ** 2)
return loss