-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
320 lines (286 loc) · 11.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
# Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the Apache License Version 2.0.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License Version 2.0 for more details.
# ============================================================================
from collections import namedtuple
import numpy as np
import scipy as sp
import networkx as nx
import tensorflow as tf
from tensorflow.python.util import nest
from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsRestClassifier
from sklearn import metrics
TaskInfo = namedtuple("TaskInfo", ["label_id",
"train_nodes", "train_labels",
"test_nodes", "test_labels"])
def batching_tasks(tasks):
assert isinstance(tasks, list)
assert isinstance(tasks[0], TaskInfo)
new = {}
for attr in ["label_id", "train_nodes", "train_labels",
"test_nodes", "test_labels"]:
new[attr] = np.stack([getattr(t, attr) for t in tasks])
batch_tasks = TaskInfo(**new)
return batch_tasks
def sample_tasks(
rng,
num_tasks,
num_positive_nodes,
num_negative_nodes,
sample_from_labels,
G,
test_num_pos=None,
test_num_neg=None):
tasks = []
while len(tasks) < num_tasks:
tmp_task = sample_a_task(
rng, num_positive_nodes, num_negative_nodes,
sample_from_labels, G,
test_num_pos, test_num_neg)
if not any([compare_tasks(t, tmp_task) for t in tasks]):
tasks.append(tmp_task)
else:
import ipdb; ipdb.set_trace()
return tasks
def sample_a_task(
rng,
num_positive_nodes,
num_negative_nodes,
sample_from_labels,
G,
test_num_pos,
test_num_neg):
if test_num_pos is None:
test_num_pos = num_positive_nodes
test_num_neg = num_negative_nodes
label_array = G.graph["label_array"]
sampled_label = None
while not sampled_label:
tmp = rng.choice(sample_from_labels)
if np.count_nonzero(label_array[:, tmp]) > num_positive_nodes + test_num_pos:
sampled_label = tmp
all_pos_nodes = np.nonzero(label_array[:, sampled_label] != 0)[0]
all_neg_nodes = np.nonzero(label_array[:, sampled_label] == 0)[0]
sampled_pos_nodes = rng.choice(all_pos_nodes,
size=num_positive_nodes + test_num_pos,
replace=False)
sampled_neg_nodes = rng.choice(all_neg_nodes,
size=num_negative_nodes + test_num_neg,
replace=False)
train_pos_nodes, test_pos_nodes = np.split(
sampled_pos_nodes, [num_positive_nodes])
train_neg_nodes, test_neg_nodes = np.split(
sampled_neg_nodes, [num_negative_nodes])
train_nodes = rng.permutation(
np.concatenate((train_pos_nodes, train_neg_nodes)))
train_labels = label_array[train_nodes, sampled_label]
test_nodes = rng.permutation(
np.concatenate((test_pos_nodes, test_neg_nodes)))
test_labels = label_array[test_nodes, sampled_label]
task = TaskInfo(label_id=sampled_label,
train_nodes=train_nodes,
train_labels=train_labels,
test_nodes=test_nodes,
test_labels=test_labels)
return task
def sample_fixed_tasks(
rng,
num_tasks,
num_positive_nodes,
num_negative_nodes,
sample_from_labels,
G,
test_num):
tasks = []
while len(tasks) < num_tasks:
label_array = G.graph["label_array"]
sampled_label = None
while not sampled_label:
tmp = rng.choice(sample_from_labels)
if np.count_nonzero(label_array[:, tmp]) > num_positive_nodes:
sampled_label = tmp
all_pos_nodes = np.nonzero(label_array[:, sampled_label] != 0)[0]
all_neg_nodes = np.nonzero(label_array[:, sampled_label] == 0)[0]
sampled_pos_nodes = rng.choice(all_pos_nodes,
size=num_positive_nodes,
replace=False)
sampled_neg_nodes = rng.choice(all_neg_nodes,
size=num_negative_nodes,
replace=False)
# train_pos_nodes, test_pos_nodes = np.split(
# sampled_pos_nodes, [num_positive_nodes])
# train_neg_nodes, test_neg_nodes = np.split(
# sampled_neg_nodes, [num_negative_nodes])
train_nodes = rng.permutation(
np.concatenate((sampled_pos_nodes, sampled_neg_nodes)))
train_labels = label_array[train_nodes, sampled_label]
# test_nodes = rng.permutation(
# np.concatenate((test_pos_nodes, test_neg_nodes)))
done = False
while not done:
test_nodes = []
while len(test_nodes) < test_num:
tmp = rng.randint(label_array.shape[0])
if tmp not in train_nodes:
test_nodes.append(tmp)
test_nodes = np.asarray(test_nodes)
test_labels = label_array[test_nodes, sampled_label]
if np.sum(test_labels) > 0:
done = True
task = TaskInfo(label_id=sampled_label,
train_nodes=train_nodes,
train_labels=train_labels,
test_nodes=test_nodes,
test_labels=test_labels)
if not any([compare_tasks(t, task) for t in tasks]):
tasks.append(task)
else:
import ipdb; ipdb.set_trace()
return tasks
def sample_random_tasks(
rng,
num_tasks,
num_positive_nodes,
num_negative_nodes,
G):
tasks = []
while len(tasks) < num_tasks:
random_nodes = rng.choice(G.number_of_nodes(),
size=2 * (num_positive_nodes + num_negative_nodes),
replace=False)
train_nodes, test_nodes = np.split(random_nodes, 2)
train_labels, test_labels = np.zeros_like(train_nodes), np.zeros_like(test_nodes)
train_labels[rng.permutation(train_labels.shape[0])[:num_positive_nodes]] = 1
test_labels[rng.permutation(test_labels.shape[0])[:num_positive_nodes]] = 1
tmp_task = TaskInfo(label_id=-1,
train_nodes=train_nodes, train_labels=train_labels,
test_nodes=test_nodes, test_labels=test_labels)
if not any([compare_tasks(t, tmp_task) for t in tasks]):
tasks.append(tmp_task)
else:
import ipdb; ipdb.set_trace()
return tasks
def compare_tasks(task_1, task_2):
if task_1.label_id != task_2.label_id:
return False
else:
for attr in ["train_nodes", "test_nodes"]:
if set(getattr(task_1, attr)) == set(getattr(task_2, attr)):
continue
else:
return False
return True
def _sample_tasks(rng,
num_tasks,
num_labels_per_task,
num_shots_per_label,
sample_from_labels,
label_array):
all_tasks = []
while len(all_tasks) < num_tasks:
sampled_labels = rng.choice(sample_from_labels,
size=num_labels_per_task,
replace=False)
associated_nodes = set()
for label in sampled_labels:
nodes = label_array[:, label].nonzero()[0]
associated_nodes.update(nodes)
associated_nodes = np.asarray(list(associated_nodes), dtype=np.int64)
num_sampled_nodes = num_labels_per_task * num_shots_per_label
rng.shuffle(associated_nodes)
train_nodes = associated_nodes[:num_sampled_nodes]
test_nodes = associated_nodes[num_sampled_nodes: num_sampled_nodes * 2]
if np.all(label_array[train_nodes][:, sampled_labels].sum(0)) \
and np.all(label_array[test_nodes][:, sampled_labels].sum(0)):
task = TaskInfo(labels=sampled_labels,
train_nodes=train_nodes,
test_nodes=test_nodes)
if not any([compare_tasks(x, task) for x in all_tasks]):
all_tasks.append(task)
else:
import ipdb; ipdb.set_trace()
print(f"{len(all_tasks)}")
return all_tasks
def calculate_metrics(labels, logits):
true_labels = labels
pred_probs = tf.nn.softmax(logits, axis=-1).numpy()
pred_scores = pred_probs[:, :, 1]
pred_labels = np.argmax(logits, axis=-1)
# pred_scores = tf.sigmoid(logits).numpy().squeeze()
# pred_labels = np.asarray(pred_scores > 0.5, np.int64)
accs = []
# baccs = []
aucs = []
precisions = []
recalls = []
f1s = []
# for y_true, y_pred in zip(true_labels, pred_labels):
for y_true, y_pred, y_score in zip(true_labels, pred_labels, pred_scores):
accs.append(metrics.accuracy_score(y_true, y_pred))
# baccs.append(metrics.balanced_accuracy_score(y_true, y_pred))
aucs.append(metrics.roc_auc_score(y_true, y_score))
precisions.append(metrics.precision_score(y_true, y_pred))
recalls.append(metrics.recall_score(y_true, y_pred))
f1s.append(metrics.f1_score(y_true, y_pred))
acc = np.mean(accs)
auc = np.mean(aucs)
precison = np.mean(precisions)
recall = np.mean(recalls)
f1 = np.mean(f1s)
outputs = {
# "accuracy": acc,
"roc_auc": auc,
"precision": precison,
"recall": recall,
"f1": f1
}
return outputs
def flatten_dict(dt, separator="/"):
dt = dt.copy()
while any(isinstance(v, dict) for v in dt.values()):
remove = []
add = {}
for key, value in dt.items():
if isinstance(value, dict):
for subkey, v in value.items():
add[separator.join([key, subkey])] = v
remove.append(key)
dt.update(add)
for k in remove:
del dt[k]
return dt
class DotDict(dict):
def __init__(self, *args, **kwargs):
super(DotDict, self).__init__(*args, **kwargs)
self.__dict__ = self
def __getattribute__(self, item):
value = dict.__getattribute__(self, item)
if isinstance(value, dict):
return DotDict(value)
else:
return value
if __name__ == "__main__":
data = "BlogCatalog"
rng = np.random.RandomState(123)
G = nx.read_gpickle(f"./dataset/{data}/{data}_nx.pkl")
# label_array = G.graph["label_array"]
# num_labels = label_array.shape[1]
# rng = np.random.RandomState(123)
# shuffled_labels = rng.permutation(num_labels)
# metatrain_labels = shuffled_labels[:23]
# metaval_labels = shuffled_labels[23: -8]
# metatest_labels = shuffled_labels[-8:]
# metatrain_tasks = sample_tasks(rng, 10, 5, 5, metatrain_labels, label_array)
# nodes = list(G.nodes())
# pairs = run_random_walks(G, nodes, 50, 5)
# with open(f"./dataset/{data}/{data}_context_pairs.txt", "w") as f:
# f.write("\n".join([f"{p[0]},{p[1]}" for p in pairs]))
tasks = sample_tasks(
rng, 100, 10, 10,
rng.choice(G.graph["label_array"].shape[1], size=20, replace=False),
G)