-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
1186 lines (983 loc) · 39.8 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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os.path as osp
import os
import numpy as np
import scipy.sparse as sp
import torch
import torch_geometric.transforms as T
import torch.nn.functional as F
import torch.nn as nn
import faiss
from ogb.nodeproppred import PygNodePropPredDataset
from torch_geometric.datasets import Planetoid
from torch_geometric.datasets.amazon_products import AmazonProducts
from torch_geometric.datasets.reddit import Reddit
from torch_geometric.datasets.reddit2 import Reddit2
from torch_geometric.datasets.flickr import Flickr
from torch_geometric.datasets.s3dis import S3DIS
from deeprobust.graph.data import Dataset
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from torch_geometric.loader import NeighborSampler
from torch_geometric.utils import add_remaining_self_loops, to_undirected, dropout_adj
from torch_geometric.loader import *
from torch_sparse import SparseTensor
def get_dataset(name, normalize_features=False, transform=None):
if name in ['ogbn-arxiv', 'ogbn-products', 'ogbn-papers100M']:
root = osp.join(osp.dirname(osp.realpath(__file__)), 'dataset')
else:
root = osp.join(osp.dirname(osp.realpath(__file__)), 'data')
if name in ['ogbn-arxiv', 'ogbn-products', 'ogbn-papers100M']:
dataset = PygNodePropPredDataset(name=name, root=root, transform=T.ToSparseTensor())
elif name in ['cora', 'citeseer', 'pubmed']:
dataset = Planetoid(root, name)
elif name=='amazon-products':
dataset = AmazonProducts(root+'/'+name)
elif name=='reddit':
dataset = Reddit(root+'/'+name)
elif name=='reddit2':
dataset = Reddit2(root+'/'+name)
elif name=='flickr':
dataset = Flickr(root+'/'+name)
elif name=='s3dis':
dataset = S3DIS(root+'/'+name)
if os.path.exists(root+'/temp/edge_index_'+name+'.pt'):
dataset.data.edge_index = torch.load(f'{root}/temp/edge_index_{name}.pt')
else:
k = 10
feat_np = dataset.data.pos.numpy()
index = faiss.IndexFlatL2(feat_np.shape[1])
index.add(feat_np)
D, I = index.search(feat_np, k+1)
source_nodes = torch.arange(len(dataset.data.y)).unsqueeze(1).repeat(1, k)
I_torch = torch.from_numpy(I[:, 1:]).long().clone()
dataset.data.edge_index = torch.cat((source_nodes.view(-1), I_torch.view(-1))).view(2, -1)
torch.save(dataset.data.edge_index, f'{root}/temp/edge_index_{name}.pt')
else:
raise NotImplementedError
if transform is not None and normalize_features:
dataset.transform = T.Compose([T.NormalizeFeatures(), transform])
elif normalize_features:
dataset.transform = T.NormalizeFeatures()
elif transform is not None:
dataset.transform = transform
dpr_data = Pyg2Dpr(dataset)
if name in ['ogbn-arxiv', 'reddit2']:
feat, idx_train = dpr_data.features, dpr_data.idx_train
feat_train = feat[idx_train]
scaler = StandardScaler()
scaler.fit(feat_train)
feat = scaler.transform(feat)
dpr_data.features = feat
return dpr_data
class Pyg2Dpr(Dataset):#input dataset and get the divided one. if we input partitioned dataset, then we can get what we want
def __init__(self, pyg_data, **kwargs):
try:
splits = pyg_data.get_idx_split()
except:
pass
try:
dataset_name = pyg_data.name
except:
pass
pyg_data = pyg_data.data
try:
if dataset_name == 'ogbn-papers100M':
pyg_data.edge_index, _ = dropout_adj(pyg_data.edge_index, p = 0.4, num_nodes= pyg_data.num_nodes)
if dataset_name in ['ogbn-arxiv', 'ogbn-papers100M']:
pyg_data.edge_index = to_undirected(edge_index=pyg_data.edge_index, edge_attr=None, num_nodes=pyg_data.num_nodes)
except:
pass
n = pyg_data.num_nodes
self.adj = sp.csr_matrix((np.ones(pyg_data.edge_index.shape[1]),
(pyg_data.edge_index[0], pyg_data.edge_index[1])), shape=(n, n))
self.features = pyg_data.x.numpy()
self.labels = pyg_data.y.numpy()
if self.labels.shape[-1]==107:
self.labels = np.argmax(self.labels, -1)
if len(self.labels.shape) == 2 and self.labels.shape[1] == 1:
self.labels = self.labels.reshape(-1) # ogb-arxiv needs to reshape
if hasattr(pyg_data, 'train_mask'):
# for fixed split
self.idx_train = mask_to_index(pyg_data.train_mask, n)
self.idx_val = mask_to_index(pyg_data.val_mask, n)
self.idx_test = mask_to_index(pyg_data.test_mask, n)
self.name = 'Pyg2Dpr'
else:
try:
# for ogb
self.idx_train = splits['train']
self.idx_val = splits['valid']
self.idx_test = splits['test']
self.name = 'Pyg2Dpr'
except:
# for other datasets
self.idx_train, self.idx_val, self.idx_test = get_train_val_test(
nnodes=n, val_size=0.1, test_size=0.8, stratify=self.labels)
print("train val test:",len(self.idx_train),len(self.idx_val),len(self.idx_test))
class Transd2Ind:
# transductive setting to inductive setting
def __init__(self, dpr_data, keep_ratio):
idx_train, idx_val, idx_test = dpr_data.idx_train, dpr_data.idx_val, dpr_data.idx_test
adj, features, labels = dpr_data.adj, dpr_data.features, dpr_data.labels
self.nclass = labels.max()+1
self.adj, self.features, self.labels = adj, features, labels
self.idx_train = np.array(idx_train)
self.idx_val = np.array(idx_val)
self.idx_test = np.array(idx_test)
if keep_ratio < 1:
idx_train, _ = train_test_split(idx_train,
random_state=None,
train_size=keep_ratio,
test_size=1-keep_ratio,
stratify=labels[idx_train])
self.adj_train = adj[np.ix_(idx_train, idx_train)]
self.adj_val = adj[np.ix_(idx_val, idx_val)]
self.adj_test = adj[np.ix_(idx_test, idx_test)]
print('size of adj_train:', self.adj_train.shape)
print('#edges in adj_train:', self.adj_train.sum())
self.labels_train = labels[idx_train]
self.labels_val = labels[idx_val]
self.labels_test = labels[idx_test]
self.feat_train = features[idx_train]
self.feat_val = features[idx_val]
self.feat_test = features[idx_test]
import torch.sparse as ts
from typing import List, Optional, Tuple, Union
from torch import Tensor
from torch.nn import LSTM
from torch_scatter import scatter_add
from torch_sparse import SparseTensor, fill_diag, matmul, mul
from torch_sparse import sum as sparsesum
from torch_sparse import SparseTensor, matmul
from torch_geometric.nn.inits import zeros
from torch_geometric.utils import add_remaining_self_loops
from torch_geometric.utils.num_nodes import maybe_num_nodes
@torch.jit._overload
def gcn_norm(edge_index, edge_weight=None, num_nodes=None, improved=False,
add_self_loops=True, flow="source_to_target", dtype=None):
# type: (Tensor, OptTensor, Optional[int], bool, bool, str, Optional[int]) -> PairTensor # noqa
pass
@torch.jit._overload
def gcn_norm(edge_index, edge_weight=None, num_nodes=None, improved=False,
add_self_loops=True, flow="source_to_target", dtype=None):
# type: (SparseTensor, OptTensor, Optional[int], bool, bool, str, Optional[int]) -> SparseTensor # noqa
pass
def gcn_norm(edge_index, edge_weight=None, num_nodes=None, improved=False,
add_self_loops=True, flow="source_to_target", dtype=None):
fill_value = 2. if improved else 1.
if isinstance(edge_index, SparseTensor):
assert flow in ["source_to_target"]
adj_t = edge_index
if not adj_t.has_value():
adj_t = adj_t.fill_value(1., dtype=dtype)
if add_self_loops:
adj_t = fill_diag(adj_t, fill_value)
deg = sparsesum(adj_t, dim=1)
deg_inv_sqrt = deg.pow_(-0.5)
deg_inv_sqrt.masked_fill_(deg_inv_sqrt == float('inf'), 0.)
adj_t = mul(adj_t, deg_inv_sqrt.view(-1, 1))
adj_t = mul(adj_t, deg_inv_sqrt.view(1, -1))
return adj_t
else:
assert flow in ["source_to_target", "target_to_source"]
num_nodes = maybe_num_nodes(edge_index, num_nodes)
if edge_weight is None:
edge_weight = torch.ones((edge_index.size(1), ), dtype=dtype,
device=edge_index.device)
if add_self_loops:
edge_index, tmp_edge_weight = add_remaining_self_loops(
edge_index, edge_weight, fill_value, num_nodes)
assert tmp_edge_weight is not None
edge_weight = tmp_edge_weight
row, col = edge_index[0], edge_index[1]
idx = col if flow == "source_to_target" else row
deg = scatter_add(edge_weight, idx, dim=0, dim_size=num_nodes)
deg_inv_sqrt = deg.pow_(-0.5)
deg_inv_sqrt.masked_fill_(deg_inv_sqrt == float('inf'), 0)
return edge_index, deg_inv_sqrt[row] * edge_weight * deg_inv_sqrt[col]
def encode_onehot(labels):
"""Convert label to onehot format.
Parameters
----------
labels : numpy.array
node labels
Returns
-------
numpy.array
onehot labels
"""
eye = np.eye(labels.max() + 1)
onehot_mx = eye[labels]
return onehot_mx
def tensor2onehot(labels):
"""Convert label tensor to label onehot tensor.
Parameters
----------
labels : torch.LongTensor
node labels
Returns
-------
torch.LongTensor
onehot labels tensor
"""
eye = torch.eye(labels.max() + 1)
onehot_mx = eye[labels]
return onehot_mx.to(labels.device)
def preprocess(adj, features, labels, preprocess_adj=False, preprocess_feature=False, sparse=False, device='cpu'):
"""Convert adj, features, labels from array or sparse matrix to
torch Tensor, and normalize the input data.
Parameters
----------
adj : scipy.sparse.csr_matrix
the adjacency matrix.
features : scipy.sparse.csr_matrix
node features
labels : numpy.array
node labels
preprocess_adj : bool
whether to normalize the adjacency matrix
preprocess_feature : bool
whether to normalize the feature matrix
sparse : bool
whether to return sparse tensor
device : str
'cpu' or 'cuda'
"""
if preprocess_adj:
adj = normalize_adj(adj)
if preprocess_feature:
features = normalize_feature(features)
labels = torch.LongTensor(labels)
if sparse:
adj = sparse_mx_to_torch_sparse_tensor(adj)
features = sparse_mx_to_torch_sparse_tensor(features)
else:
features = torch.FloatTensor(np.array(features.todense()))
adj = torch.FloatTensor(adj.todense())
return adj.to(device), features.to(device), labels.to(device)
def to_tensor(adj, device='cpu'):
"""Convert adj, features, labels from array or sparse matrix to
torch Tensor.
Parameters
----------
adj : scipy.sparse.csr_matrix
the adjacency matrix.
features : scipy.sparse.csr_matrix
node features
labels : numpy.array
node labels
device : str
'cpu' or 'cuda'
"""
if sp.issparse(adj):
adj = sparse_mx_to_torch_sparse_tensor(adj)
else:
adj = torch.FloatTensor(adj)
return adj.to(device)
# def to_tensor(adj, features=None, labels=None, device='cpu'):
# """Convert adj, features, labels from array or sparse matrix to
# torch Tensor.
# Parameters
# ----------
# adj : scipy.sparse.csr_matrix
# the adjacency matrix.
# features : scipy.sparse.csr_matrix
# node features
# labels : numpy.array
# node labels
# device : str
# 'cpu' or 'cuda'
# """
# if sp.issparse(adj):
# adj = sparse_mx_to_torch_sparse_tensor(adj)
# else:
# adj = torch.FloatTensor(adj)
# if features!=None:
# if sp.issparse(features):
# features = sparse_mx_to_torch_sparse_tensor(features)
# else:
# features = torch.FloatTensor(np.array(features))
# if labels is None:
# if features!=None:
# return adj.to(device), features.to(device)
# else:
# return adj.to(device)
# else:
# labels = torch.LongTensor(labels)
# if features!=None:
# return adj.to(device), features.to(device), labels.to(device)
# else:
# return adj.to(device), labels.to(device)
def normalize_feature(mx):
"""Row-normalize sparse matrix or dense matrix
Parameters
----------
mx : scipy.sparse.csr_matrix or numpy.array
matrix to be normalized
Returns
-------
scipy.sprase.lil_matrix
normalized matrix
"""
if type(mx) is not sp.lil.lil_matrix:
try:
mx = mx.tolil()
except AttributeError:
pass
rowsum = np.array(mx.sum(1))
r_inv = np.power(rowsum, -1).flatten()
r_inv[np.isinf(r_inv)] = 0.
r_mat_inv = sp.diags(r_inv)
mx = r_mat_inv.dot(mx)
return mx
def normalize_adj(mx):
"""Normalize sparse adjacency matrix,
A' = (D + I)^-1/2 * ( A + I ) * (D + I)^-1/2
Row-normalize sparse matrix
Parameters
----------
mx : scipy.sparse.csr_matrix
matrix to be normalized
Returns
-------
scipy.sprase.lil_matrix
normalized matrix
"""
# TODO: maybe using coo format would be better?
if type(mx) is not sp.lil.lil_matrix:
mx = mx.tolil()
if mx[0, 0] == 0 :
mx = mx + sp.eye(mx.shape[0])
rowsum = np.array(mx.sum(1))
r_inv = np.power(rowsum, -1/2).flatten()
r_inv[np.isinf(r_inv)] = 0.
r_mat_inv = sp.diags(r_inv)
mx = r_mat_inv.dot(mx)
mx = mx.dot(r_mat_inv)
rowsum = np.array(mx.sum(1))
rowsum_mean=np.mean(rowsum)
return mx
def normalize_sparse_tensor(adj, fill_value=1):
"""Normalize sparse tensor. Need to import torch_scatter
"""
edge_index = adj._indices()
edge_weight = adj._values()
num_nodes= adj.size(0)
edge_index, edge_weight = add_self_loops(
edge_index, edge_weight, fill_value, num_nodes)
row, col = edge_index
from torch_scatter import scatter_add
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
deg_inv_sqrt = deg.pow(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
values = deg_inv_sqrt[row] * edge_weight * deg_inv_sqrt[col]
shape = adj.shape
return torch.sparse.FloatTensor(edge_index, values, shape)
def add_self_loops(edge_index, edge_weight=None, fill_value=1, num_nodes=None):
# num_nodes = maybe_num_nodes(edge_index, num_nodes)
loop_index = torch.arange(0, num_nodes, dtype=torch.long,
device=edge_index.device)
loop_index = loop_index.unsqueeze(0).repeat(2, 1)
if edge_weight is not None:
assert edge_weight.numel() == edge_index.size(1)
loop_weight = edge_weight.new_full((num_nodes, ), fill_value)
edge_weight = torch.cat([edge_weight, loop_weight], dim=0)
edge_index = torch.cat([edge_index, loop_index], dim=1)
return edge_index, edge_weight
def normalize_adj_tensor(adj, sparse=False):
"""Normalize adjacency tensor matrix.
"""
device = adj.device
if sparse:
# warnings.warn('If you find the training process is too slow, you can uncomment line 207 in deeprobust/graph/utils.py. Note that you need to install torch_sparse')
# TODO if this is too slow, uncomment the following code,
# but you need to install torch_scatter
# return normalize_sparse_tensor(adj)
adj = to_scipy(adj)
mx = normalize_adj(adj)
return sparse_mx_to_torch_sparse_tensor(mx).to(device)
else:
mx = adj + torch.eye(adj.shape[0]).to(device)
# mx = adj.to(device)
rowsum = mx.sum(1)
r_inv = rowsum.pow(-1/2).flatten()
r_inv[torch.isinf(r_inv)] = 0.
r_mat_inv = torch.diag(r_inv)
mx = r_mat_inv @ mx
mx = mx @ r_mat_inv
return mx
def unnormalize_adj_tensor(adj, sparse=False):
"""Normalize adjacency tensor matrix.
"""
device = adj.device
if sparse:
# warnings.warn('If you find the training process is too slow, you can uncomment line 207 in deeprobust/graph/utils.py. Note that you need to install torch_sparse')
# TODO if this is too slow, uncomment the following code,
# but you need to install torch_scatter
# return normalize_sparse_tensor(adj)
adj = to_scipy(adj)
mx = adj + sp.eye(adj.shape[0])
return sparse_mx_to_torch_sparse_tensor(mx).to(device)
else:
mx = adj + torch.eye(adj.shape[0]).to(device)
# mx = adj.to(device)
rowsum = mx.sum(1)
r_inv = rowsum.pow(-1/2).flatten()
r_inv[torch.isinf(r_inv)] = 0.
r_mat_inv = torch.diag(r_inv)
mx = r_mat_inv @ mx
mx = mx @ r_mat_inv
return mx
def degree_normalize_adj(mx):
"""Row-normalize sparse matrix"""
mx = mx.tolil()
if mx[0, 0] == 0 :
mx = mx + sp.eye(mx.shape[0])
rowsum = np.array(mx.sum(1))
r_inv = np.power(rowsum, -1).flatten()
r_inv[np.isinf(r_inv)] = 0.
r_mat_inv = sp.diags(r_inv)
# mx = mx.dot(r_mat_inv)
mx = r_mat_inv.dot(mx)
return mx
def degree_normalize_sparse_tensor(adj, fill_value=1):
"""degree_normalize_sparse_tensor.
"""
edge_index = adj._indices()
edge_weight = adj._values()
num_nodes= adj.size(0)
edge_index, edge_weight = add_self_loops(
edge_index, edge_weight, fill_value, num_nodes)
row, col = edge_index
from torch_scatter import scatter_add
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
deg_inv_sqrt = deg.pow(-1)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
values = deg_inv_sqrt[row] * edge_weight
shape = adj.shape
return torch.sparse.FloatTensor(edge_index, values, shape)
def degree_normalize_adj_tensor(adj, sparse=True):
"""degree_normalize_adj_tensor.
"""
device = adj.device
if sparse:
# return degree_normalize_sparse_tensor(adj)
adj = to_scipy(adj)
mx = degree_normalize_adj(adj)
return sparse_mx_to_torch_sparse_tensor(mx).to(device)
else:
mx = adj + torch.eye(adj.shape[0]).to(device)
rowsum = mx.sum(1)
r_inv = rowsum.pow(-1).flatten()
r_inv[torch.isinf(r_inv)] = 0.
r_mat_inv = torch.diag(r_inv)
mx = r_mat_inv @ mx
return mx
def accuracy(output, labels):
"""Return accuracy of output compared to labels.
Parameters
----------
output : torch.Tensor
output from model
labels : torch.Tensor or numpy.array
node labels
Returns
-------
float
accuracy
"""
if not hasattr(labels, '__len__'):
labels = [labels]
if type(labels) is not torch.Tensor:
labels = torch.LongTensor(labels)
preds = output.max(1)[1].type_as(labels)
correct = preds.eq(labels).double()
correct = correct.sum()
return correct / len(labels)
def loss_acc(output, labels, targets, avg_loss=True):
if type(labels) is not torch.Tensor:
labels = torch.LongTensor(labels)
preds = output.max(1)[1].type_as(labels)
correct = preds.eq(labels).double()[targets]
loss = F.nll_loss(output[targets], labels[targets], reduction='mean' if avg_loss else 'none')
if avg_loss:
return loss, correct.sum() / len(targets)
return loss, correct
# correct = correct.sum()
# return loss, correct / len(labels)
def classification_margin(output, true_label):
"""Calculate classification margin for outputs.
`probs_true_label - probs_best_second_class`
Parameters
----------
output: torch.Tensor
output vector (1 dimension)
true_label: int
true label for this node
Returns
-------
list
classification margin for this node
"""
probs = torch.exp(output)
probs_true_label = probs[true_label].clone()
probs[true_label] = 0
probs_best_second_class = probs[probs.argmax()]
return (probs_true_label - probs_best_second_class).item()
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)
sparserow=torch.LongTensor(sparse_mx.row).unsqueeze(1)
sparsecol=torch.LongTensor(sparse_mx.col).unsqueeze(1)
sparseconcat=torch.cat((sparserow, sparsecol),1)
sparsedata=torch.FloatTensor(sparse_mx.data)
return torch.sparse.FloatTensor(sparseconcat.t(),sparsedata,torch.Size(sparse_mx.shape))
# slower version....
# 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 to_scipy(tensor):
"""Convert a dense/sparse tensor to scipy matrix"""
if is_sparse_tensor(tensor):
values = tensor._values()
indices = tensor._indices()
return sp.csr_matrix((values.cpu().numpy(), indices.cpu().numpy()), shape=tensor.shape)
else:
indices = tensor.nonzero().t()
values = tensor[indices[0], indices[1]]
return sp.csr_matrix((values.cpu().numpy(), indices.cpu().numpy()), shape=tensor.shape)
def is_sparse_tensor(tensor):
"""Check if a tensor is sparse tensor.
Parameters
----------
tensor : torch.Tensor
given tensor
Returns
-------
bool
whether a tensor is sparse tensor
"""
# if hasattr(tensor, 'nnz'):
if tensor.layout == torch.sparse_coo:
return True
else:
return False
def get_train_val_test(nnodes, val_size=0.1, test_size=0.8, stratify=None, seed=None):
"""This setting follows nettack/mettack, where we split the nodes
into 10% training, 10% validation and 80% testing data
Parameters
----------
nnodes : int
number of nodes in total
val_size : float
size of validation set
test_size : float
size of test set
stratify :
data is expected to split in a stratified fashion. So stratify should be labels.
seed : int or None
random seed
Returns
-------
idx_train :
node training indices
idx_val :
node validation indices
idx_test :
node test indices
"""
assert stratify is not None, 'stratify cannot be None!'
if seed is not None:
np.random.seed(seed)
idx = np.arange(nnodes)
train_size = 1 - val_size - test_size
idx_train_and_val, idx_test = train_test_split(idx,
random_state=None,
train_size=train_size + val_size,
test_size=test_size,
stratify=stratify)
if stratify is not None:
stratify = stratify[idx_train_and_val]
idx_train, idx_val = train_test_split(idx_train_and_val,
random_state=None,
train_size=(train_size / (train_size + val_size)),
test_size=(val_size / (train_size + val_size)),
stratify=stratify)
return idx_train, idx_val, idx_test
def get_train_test(nnodes, test_size=0.8, stratify=None, seed=None):
"""This function returns training and test set without validation.
It can be used for settings of different label rates.
Parameters
----------
nnodes : int
number of nodes in total
test_size : float
size of test set
stratify :
data is expected to split in a stratified fashion. So stratify should be labels.
seed : int or None
random seed
Returns
-------
idx_train :
node training indices
idx_test :
node test indices
"""
assert stratify is not None, 'stratify cannot be None!'
if seed is not None:
np.random.seed(seed)
idx = np.arange(nnodes)
train_size = 1 - test_size
idx_train, idx_test = train_test_split(idx, random_state=None,
train_size=train_size,
test_size=test_size,
stratify=stratify)
return idx_train, idx_test
def get_train_val_test_gcn(labels, seed=None):
"""This setting follows gcn, where we randomly sample 20 instances for each class
as training data, 500 instances as validation data, 1000 instances as test data.
Note here we are not using fixed splits. When random seed changes, the splits
will also change.
Parameters
----------
labels : numpy.array
node labels
seed : int or None
random seed
Returns
-------
idx_train :
node training indices
idx_val :
node validation indices
idx_test :
node test indices
"""
if seed is not None:
np.random.seed(seed)
idx = np.arange(len(labels))
nclass = labels.max() + 1
idx_train = []
idx_unlabeled = []
for i in range(nclass):
labels_i = idx[labels==i]
labels_i = np.random.permutation(labels_i)
idx_train = np.hstack((idx_train, labels_i[: 20])).astype(np.int)
idx_unlabeled = np.hstack((idx_unlabeled, labels_i[20: ])).astype(np.int)
idx_unlabeled = np.random.permutation(idx_unlabeled)
idx_val = idx_unlabeled[: 500]
idx_test = idx_unlabeled[500: 1500]
return idx_train, idx_val, idx_test
def get_train_test_labelrate(labels, label_rate):
"""Get train test according to given label rate.
"""
nclass = labels.max() + 1
train_size = int(round(len(labels) * label_rate / nclass))
print("=== train_size = %s ===" % train_size)
idx_train, idx_val, idx_test = get_splits_each_class(labels, train_size=train_size)
return idx_train, idx_test
def get_splits_each_class(labels, train_size):
"""We randomly sample n instances for class, where n = train_size.
"""
idx = np.arange(len(labels))
nclass = labels.max() + 1
idx_train = []
idx_val = []
idx_test = []
for i in range(nclass):
labels_i = idx[labels==i]
labels_i = np.random.permutation(labels_i)
idx_train = np.hstack((idx_train, labels_i[: train_size])).astype(np.int)
idx_val = np.hstack((idx_val, labels_i[train_size: 2*train_size])).astype(np.int)
idx_test = np.hstack((idx_test, labels_i[2*train_size: ])).astype(np.int)
return np.random.permutation(idx_train), np.random.permutation(idx_val), \
np.random.permutation(idx_test)
def unravel_index(index, array_shape):
rows = index // array_shape[1]
cols = index % array_shape[1]
return rows, cols
def get_degree_squence(adj):
try:
return adj.sum(0)
except:
return ts.sum(adj, dim=1).to_dense()
def likelihood_ratio_filter(node_pairs, modified_adjacency, original_adjacency, d_min, threshold=0.004, undirected=True):
"""
Filter the input node pairs based on the likelihood ratio test proposed by Zügner et al. 2018, see
https://dl.acm.org/citation.cfm?id=3220078. In essence, for each node pair return 1 if adding/removing the edge
between the two nodes does not violate the unnoticeability constraint, and return 0 otherwise. Assumes unweighted
and undirected graphs.
"""
N = int(modified_adjacency.shape[0])
# original_degree_sequence = get_degree_squence(original_adjacency)
# current_degree_sequence = get_degree_squence(modified_adjacency)
original_degree_sequence = original_adjacency.sum(0)
current_degree_sequence = modified_adjacency.sum(0)
concat_degree_sequence = torch.cat((current_degree_sequence, original_degree_sequence))
# Compute the log likelihood values of the original, modified, and combined degree sequences.
ll_orig, alpha_orig, n_orig, sum_log_degrees_original = degree_sequence_log_likelihood(original_degree_sequence, d_min)
ll_current, alpha_current, n_current, sum_log_degrees_current = degree_sequence_log_likelihood(current_degree_sequence, d_min)
ll_comb, alpha_comb, n_comb, sum_log_degrees_combined = degree_sequence_log_likelihood(concat_degree_sequence, d_min)
# Compute the log likelihood ratio
current_ratio = -2 * ll_comb + 2 * (ll_orig + ll_current)
# Compute new log likelihood values that would arise if we add/remove the edges corresponding to each node pair.
new_lls, new_alphas, new_ns, new_sum_log_degrees = updated_log_likelihood_for_edge_changes(node_pairs,
modified_adjacency, d_min)
# Combination of the original degree distribution with the distributions corresponding to each node pair.
n_combined = n_orig + new_ns
new_sum_log_degrees_combined = sum_log_degrees_original + new_sum_log_degrees
alpha_combined = compute_alpha(n_combined, new_sum_log_degrees_combined, d_min)
new_ll_combined = compute_log_likelihood(n_combined, alpha_combined, new_sum_log_degrees_combined, d_min)
new_ratios = -2 * new_ll_combined + 2 * (new_lls + ll_orig)
# Allowed edges are only those for which the resulting likelihood ratio measure is < than the threshold
allowed_edges = new_ratios < threshold
if allowed_edges.is_cuda:
filtered_edges = node_pairs[allowed_edges.cpu().numpy().astype(np.bool)]
else:
filtered_edges = node_pairs[allowed_edges.numpy().astype(np.bool)]
allowed_mask = torch.zeros(modified_adjacency.shape)
allowed_mask[filtered_edges.T] = 1
if undirected:
allowed_mask += allowed_mask.t()
return allowed_mask, current_ratio
def degree_sequence_log_likelihood(degree_sequence, d_min):
"""
Compute the (maximum) log likelihood of the Powerlaw distribution fit on a degree distribution.
"""
# Determine which degrees are to be considered, i.e. >= d_min.
D_G = degree_sequence[(degree_sequence >= d_min.item())]
try:
sum_log_degrees = torch.log(D_G).sum()
except:
sum_log_degrees = np.log(D_G).sum()
n = len(D_G)
alpha = compute_alpha(n, sum_log_degrees, d_min)
ll = compute_log_likelihood(n, alpha, sum_log_degrees, d_min)
return ll, alpha, n, sum_log_degrees
def updated_log_likelihood_for_edge_changes(node_pairs, adjacency_matrix, d_min):
""" Adopted from https://github.com/danielzuegner/nettack
"""
# For each node pair find out whether there is an edge or not in the input adjacency matrix.
edge_entries_before = adjacency_matrix[node_pairs.T]
degree_sequence = adjacency_matrix.sum(1)
D_G = degree_sequence[degree_sequence >= d_min.item()]
sum_log_degrees = torch.log(D_G).sum()
n = len(D_G)
deltas = -2 * edge_entries_before + 1
d_edges_before = degree_sequence[node_pairs]
d_edges_after = degree_sequence[node_pairs] + deltas[:, None]
# Sum the log of the degrees after the potential changes which are >= d_min
sum_log_degrees_after, new_n = update_sum_log_degrees(sum_log_degrees, n, d_edges_before, d_edges_after, d_min)
# Updated estimates of the Powerlaw exponents
new_alpha = compute_alpha(new_n, sum_log_degrees_after, d_min)
# Updated log likelihood values for the Powerlaw distributions
new_ll = compute_log_likelihood(new_n, new_alpha, sum_log_degrees_after, d_min)
return new_ll, new_alpha, new_n, sum_log_degrees_after
def update_sum_log_degrees(sum_log_degrees_before, n_old, d_old, d_new, d_min):
# Find out whether the degrees before and after the change are above the threshold d_min.
old_in_range = d_old >= d_min
new_in_range = d_new >= d_min
d_old_in_range = d_old * old_in_range.float()
d_new_in_range = d_new * new_in_range.float()
# Update the sum by subtracting the old values and then adding the updated logs of the degrees.
sum_log_degrees_after = sum_log_degrees_before - (torch.log(torch.clamp(d_old_in_range, min=1))).sum(1) \
+ (torch.log(torch.clamp(d_new_in_range, min=1))).sum(1)
# Update the number of degrees >= d_min
new_n = n_old - (old_in_range!=0).sum(1) + (new_in_range!=0).sum(1)
new_n = new_n.float()
return sum_log_degrees_after, new_n
def compute_alpha(n, sum_log_degrees, d_min):
try:
alpha = 1 + n / (sum_log_degrees - n * torch.log(d_min - 0.5))
except:
alpha = 1 + n / (sum_log_degrees - n * np.log(d_min - 0.5))
return alpha
def compute_log_likelihood(n, alpha, sum_log_degrees, d_min):
# Log likelihood under alpha
try:
ll = n * torch.log(alpha) + n * alpha * torch.log(d_min) + (alpha + 1) * sum_log_degrees
except:
ll = n * np.log(alpha) + n * alpha * np.log(d_min) + (alpha + 1) * sum_log_degrees
return ll
def ravel_multiple_indices(ixs, shape, reverse=False):
"""
"Flattens" multiple 2D input indices into indices on the flattened matrix, similar to np.ravel_multi_index.
Does the same as ravel_index but for multiple indices at once.
Parameters
----------
ixs: array of ints shape (n, 2)
The array of n indices that will be flattened.
shape: list or tuple of ints of length 2
The shape of the corresponding matrix.
Returns
-------
array of n ints between 0 and shape[0]*shape[1]-1
The indices on the flattened matrix corresponding to the 2D input indices.
"""
if reverse:
return ixs[:, 1] * shape[1] + ixs[:, 0]
return ixs[:, 0] * shape[1] + ixs[:, 1]
def reshape_mx(mx, shape):
indices = mx.nonzero()
return sp.csr_matrix((mx.data, (indices[0], indices[1])), shape=shape)
# def check_path(file_path):
# if not osp.exists(file_path):
# os.system(f'mkdir -p {file_path}')
def get_cos_sim(feature1,feature2):
num = torch.dot(feature1, feature2)
denom = torch.linalg.norm(feature1) * torch.linalg.norm(feature2)
return num / denom if denom != 0 else 0
def mask_to_index(index, size):