-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
230 lines (208 loc) · 8.73 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
import torch
import numpy as np
import torch.nn as nn
import scipy.sparse as sp
import pickle as pkl
import sys
import os
import networkx as nx
def encode_onehot(labels):
classes = set(labels)
classes_dict = {c: np.identity(len(classes))[i, :] for i, c in
enumerate(classes)}
labels_onehot = np.array(list(map(classes_dict.get, labels)),
dtype=np.int32)
return labels_onehot
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 roc_auc_compute_fn(y_preds, y_targets):
try:
from sklearn.metrics import roc_auc_score
except ImportError:
raise RuntimeError("This contrib module requires sklearn to be installed.")
y_true = y_targets.cpu().numpy()
y_true = encode_onehot(y_true)
y_pred = y_preds.cpu().detach().numpy()
return roc_auc_score(y_true, y_pred)
def parse_index_file(filename):
"""Parse index file."""
index = []
for line in open(filename):
index.append(int(line.strip()))
return index
def row_normalize(mx):
"""Row-normalize sparse matrix"""
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 adj_normalize(adj):
adj = sp.coo_matrix(adj)
adj_ = adj + sp.eye(adj.shape[0]) # 邻接矩阵加入自身信息,adj = adj + I
rowsum = np.array(adj_.sum(1))
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()
return adj_normalized
def preprocess_adj(adj, features):
adj = adj_normalize(adj)
features = row_normalize(features)
return adj, features
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 load_citation(dataset_str="cora", porting_to_torch=True,data_path="data"): ###
names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
objects = []
for i in range(len(names)):
with open(os.path.join(data_path, "ind.{}.{}".format(dataset_str.lower(), names[i])), 'rb') as f:
if sys.version_info > (3, 0):
objects.append(pkl.load(f, encoding='latin1'))
else:
objects.append(pkl.load(f))
x, y, tx, ty, allx, ally, graph = tuple(objects)
test_idx_reorder = parse_index_file(os.path.join(data_path, "ind.{}.test.index".format(dataset_str)))
test_idx_range = np.sort(test_idx_reorder)
if dataset_str == 'citeseer':
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, :]
G = nx.from_dict_of_lists(graph)
adj = nx.adjacency_matrix(G)
adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
degree = np.sum(adj, axis=1) # degree = np.asarray(G.degree)
labels = np.vstack((ally, ty))
labels[test_idx_reorder, :] = labels[test_idx_range, :]
idx_test = test_idx_range.tolist()
idx_train = range(len(ally)- 500)
idx_val = range(len(ally) - 500, len(ally))
adj, features = preprocess_adj(adj, features)
features = np.array(features.todense())
labels = np.argmax(labels, axis=1)
if porting_to_torch:
features = torch.FloatTensor(features).float()
labels = torch.LongTensor(labels)
adj = sparse_mx_to_torch_sparse_tensor(adj).float()
idx_train = torch.LongTensor(idx_train)
idx_val = torch.LongTensor(idx_val)
idx_test = torch.LongTensor(idx_test)
degree = torch.LongTensor(degree)
learning_type = "transductive"
return adj, features, labels, idx_train, idx_val, idx_test, degree, learning_type
def data_loader(dataset, data_path="data", porting_to_torch=True, ): ##
(adj,
features,
labels,
idx_train,
idx_val,
idx_test,
degree,
learning_type) = load_citation(dataset, porting_to_torch, data_path)
train_adj = adj
train_features = features
return adj, train_adj, features, train_features, labels, idx_train, idx_val, idx_test, degree, learning_type
class PairNorm(nn.Module):
def __init__(self, mode='PN', scale=1):
assert mode in ['None', 'PN']
super(PairNorm, self).__init__()
self.mode = mode
self.scale = scale
def forward(self, x):
if self.mode == 'None':
return x
col_mean = x.mean(dim=0)
if self.mode == 'PN':
x = x - col_mean
rownorm_mean = (1e-6 + x.pow(2).sum(dim=1).mean()).sqrt()
x = self.scale * x / rownorm_mean
return x
class DropEdge:
def __init__(self, dataset, data_path="data"):
self.dataset = dataset
self.data_path = data_path
(self.adj,
self.train_adj,
self.features,
self.train_features,
self.labels,
self.idx_train,
self.idx_val,
self.idx_test,
self.degree,
self.learning_type) = data_loader(dataset, data_path, False)
self.features = torch.FloatTensor(self.features).float() # ndarray变为tensor
self.train_features = torch.FloatTensor(self.train_features).float()
self.labels_torch = torch.LongTensor(self.labels)
self.idx_train_torch = torch.LongTensor(self.idx_train)
self.idx_val_torch = torch.LongTensor(self.idx_val)
self.idx_test_torch = torch.LongTensor(self.idx_test)
self.pos_train_idx = np.where(self.labels[self.idx_train] == 1)[0]
self.neg_train_idx = np.where(self.labels[self.idx_train] == 0)[0]
self.nfeat = self.features.shape[1]
self.nclass = int(self.labels.max().item() + 1)
self.trainadj_cache = {}
self.adj_cache = {}
self.degree_p = None
def _preprocess_adj(self, normalization, adj, cuda): ###
r_adj = adj_normalize(adj)
r_adj = sparse_mx_to_torch_sparse_tensor(r_adj).float()
if cuda:
r_adj = r_adj.cuda()
return r_adj
def _preprocess_fea(self, fea, cuda): ###
if cuda:
return fea.cuda()
else:
return fea
def stub_sampler(self, normalization, cuda): ###
if normalization in self.trainadj_cache:
r_adj = self.trainadj_cache[normalization]
else:
r_adj = self._preprocess_adj(normalization, self.train_adj, cuda)
self.trainadj_cache[normalization] = r_adj
fea = self._preprocess_fea(self.train_features, cuda)
return r_adj, fea
def randomedge_sampler(self, percent, normalization, cuda): ##
if percent >= 1.0:
return self.stub_sampler(normalization, cuda)
nnz = self.train_adj.nnz
perm = np.random.permutation(nnz)
preserve_nnz = int(nnz * percent)
perm = perm[:preserve_nnz]
r_adj = sp.coo_matrix((self.train_adj.data[perm],
(self.train_adj.row[perm],
self.train_adj.col[perm])),
shape=self.train_adj.shape)
r_adj = self._preprocess_adj(normalization, r_adj, cuda)
fea = self._preprocess_fea(self.train_features, cuda)
return r_adj, fea
def get_test_set(self, normalization, cuda): ##
if self.learning_type == "transductive":
return self.stub_sampler(normalization, cuda)
else:
if normalization in self.adj_cache:
r_adj = self.adj_cache[normalization]
else:
r_adj = self._preprocess_adj(normalization, self.adj, cuda)
self.adj_cache[normalization] = r_adj
fea = self._preprocess_fea(self.features, cuda)
return r_adj, fea
def get_label_and_idxes(self, cuda): ###
if cuda:
return self.labels_torch.cuda(), self.idx_train_torch.cuda(), self.idx_val_torch.cuda(), self.idx_test_torch.cuda()
return self.labels_torch, self.idx_train_torch, self.idx_val_torch, self.idx_test_torch