-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
140 lines (121 loc) · 4.4 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
#%%
import torch
import numpy as np
def tensor2onehot(labels):
"""Convert label tensor to label onehot tensor.
Parameters
----------
labels : torch.LongTensor
node labels
Returns
-------
torch.LongTensor
onehot labels tensor
"""
labels = labels.long()
eye = torch.eye(labels.max() + 1)
onehot_mx = eye[labels]
return onehot_mx.to(labels.device)
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 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))
def idx_to_mask(indices, n):
mask = torch.zeros(n, dtype=torch.bool)
mask[indices] = True
return mask
# %%
def attribute_mask(features, drop_rate):
import copy
features = copy.deepcopy(features)
mask = np.random.binomial(1, drop_rate, size=features.shape)
# mask = torch.tensor(mask, device=features.device)
features[mask.nonzero()]=0
return features
def get_splits_each_class(labels, train_size=50, val_size=100, test_size=500):
"""We randomly sample n instances for class, where n = train_size.
"""
np.random.seed(15)
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: train_size+val_size])).astype(np.int)
idx_test = np.hstack((idx_test, labels_i[train_size+val_size: train_size+val_size+test_size])).astype(np.int)
return idx_to_mask(idx_train, len(labels)), idx_to_mask(idx_val, len(labels)), \
idx_to_mask(idx_test, len(labels))
# %%
from sklearn.model_selection import train_test_split
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
# %%