-
Notifications
You must be signed in to change notification settings - Fork 0
/
samplers.py
411 lines (371 loc) · 17.7 KB
/
samplers.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
from utils import *
import metis
from partition_utils import partition_graph
def CalculateThreshold(candidatesArray, sampleSize, sumSmall=0, nLarge=0):
candidate = candidatesArray[candidatesArray > 0][0]
smallArray = candidatesArray[candidatesArray < candidate]
largeArray = candidatesArray[candidatesArray > candidate]
equalArray = candidatesArray[candidatesArray == candidate]
curSampleSize = (sum(smallArray) + sumSmall) / candidate + \
len(largeArray) + nLarge + len(equalArray)
if curSampleSize < sampleSize:
if len(smallArray) == 0:
return sumSmall/(sampleSize-nLarge-len(largeArray)-1)
else:
nLarge = nLarge + len(largeArray)+len(equalArray)
return CalculateThreshold(smallArray, sampleSize, sumSmall, nLarge)
else:
if len(largeArray) == 0:
return (sumSmall + sum(smallArray) + sum(equalArray))/(sampleSize-nLarge)
else:
sumSmall = sumSmall + sum(smallArray) + sum(equalArray)
return CalculateThreshold(largeArray, sampleSize, sumSmall, nLarge)
class base_sampler:
def __init__(self, adj_matrix, train_nodes):
assert(adj_matrix.diagonal().sum() == 0) # make sure diagnal is zero
# make sure is symmetric
assert((adj_matrix != adj_matrix.T).nnz == 0)
self.adj_matrix = adj_matrix
self.train_nodes = train_nodes
self.lap_matrix = normalize(adj_matrix + sp.eye(adj_matrix.shape[0]))
self.lap_matrix_sq = self.lap_matrix.multiply(self.lap_matrix)
self.lap_norm = np.array(np.sum(self.lap_matrix, axis=1))
def full_batch(self, batch_nodes, num_nodes, depth):
adjs = [sparse_mx_to_torch_sparse_tensor(
self.lap_matrix) for _ in range(depth)]
input_nodes = np.arange(num_nodes)
sampled_nodes = [np.arange(num_nodes) for _ in range(depth)]
return adjs, input_nodes, sampled_nodes
def large_batch(self, batch_nodes, num_nodes, depth):
previous_nodes = batch_nodes
sampled_nodes = []
adjs = []
for d in range(depth):
U = self.lap_matrix[previous_nodes, :]
after_nodes = []
for U_row in U:
indices = U_row.indices
after_nodes.append(indices)
after_nodes = np.unique(np.concatenate(after_nodes))
after_nodes = np.concatenate(
[previous_nodes, np.setdiff1d(after_nodes, previous_nodes)])
adj = U[:, after_nodes]
adjs += [sparse_mx_to_torch_sparse_tensor(adj)]
sampled_nodes.append(previous_nodes)
previous_nodes = after_nodes
adjs.reverse()
sampled_nodes.reverse()
return adjs, previous_nodes, sampled_nodes
def row_norm(self, adj, select):
adj = row_normalize(adj)
adj = adj.multiply(self.lap_norm[select])
# adj_norm = np.array(np.sum(adj, axis=1))
# adj = adj.multiply(self.lap_norm[select]/adj_norm)
return adj
class fastgcn_sampler(base_sampler):
def __init__(self, adj_matrix, train_nodes):
assert(adj_matrix.diagonal().sum() == 0) # make sure diagnal is zero
# make sure is symmetric
assert((adj_matrix != adj_matrix.T).nnz == 0)
self.adj_matrix = adj_matrix
self.train_nodes = train_nodes
self.lap_matrix = normalize(adj_matrix + sp.eye(adj_matrix.shape[0]))
self.lap_matrix_sq = self.lap_matrix.multiply(self.lap_matrix)
def mini_batch(self, seed, batch_nodes, probs_nodes, samp_num_list, num_nodes, adj_matrix, depth):
np.random.seed(seed)
previous_nodes = batch_nodes
sampled_nodes = []
adjs = []
pi = np.array(np.sum(self.lap_matrix_sq, axis=0))[0]
p = pi / np.sum(pi)
for d in range(depth):
U = self.lap_matrix[previous_nodes, :]
s_num = np.min([np.sum(p > 0), samp_num_list[d]])
after_nodes = np.random.choice(
num_nodes, s_num, p=p, replace=False)
after_nodes = np.unique(after_nodes)
adj = U[:, after_nodes].multiply(1/p[after_nodes]/num_nodes)
adjs += [sparse_mx_to_torch_sparse_tensor(row_normalize(adj))]
sampled_nodes += [previous_nodes]
previous_nodes = after_nodes
sampled_nodes.reverse()
adjs.reverse()
return adjs, previous_nodes, batch_nodes, probs_nodes, sampled_nodes
class ladies_sampler(base_sampler):
def __init__(self, adj_matrix, train_nodes):
assert(adj_matrix.diagonal().sum() == 0) # make sure diagnal is zero
# make sure is symmetric
assert((adj_matrix != adj_matrix.T).nnz == 0)
self.adj_matrix = adj_matrix
self.train_nodes = train_nodes
self.lap_matrix = normalize(adj_matrix + sp.eye(adj_matrix.shape[0]))
self.lap_matrix_sq = self.lap_matrix.multiply(self.lap_matrix)
def mini_batch(self, seed, batch_nodes, probs_nodes, samp_num_list, num_nodes, adj_matrix, depth):
np.random.seed(seed)
previous_nodes = batch_nodes
sampled_nodes = []
adjs = []
for d in range(depth):
U = self.lap_matrix[previous_nodes, :]
pi = np.array(
np.sum(self.lap_matrix_sq[previous_nodes, :], axis=0))[0]
p = pi / np.sum(pi)
s_num = np.min([np.sum(p > 0), samp_num_list[d]])
after_nodes = np.random.choice(
num_nodes, s_num, p=p, replace=False)
after_nodes = np.unique(np.concatenate((after_nodes, batch_nodes)))
adj = U[:, after_nodes].multiply(1/p[after_nodes])
adj = row_normalize(adj)
adjs += [sparse_mx_to_torch_sparse_tensor(adj)]
sampled_nodes += [previous_nodes]
previous_nodes = after_nodes
sampled_nodes.reverse()
adjs.reverse()
return adjs, previous_nodes, batch_nodes, probs_nodes, sampled_nodes
def mini_batch_ld(self, seed, batch_nodes, probs_nodes, samp_num_list, num_nodes, adj_matrix, depth):
np.random.seed(seed)
previous_nodes = batch_nodes
sampled_nodes = []
adjs = []
for d in range(depth):
U = self.lap_matrix[previous_nodes, :]
pi = np.array(
np.sum(self.lap_matrix_sq[previous_nodes, :], axis=0))[0]
p = pi / np.sum(pi)
s_num = np.min([np.sum(p > 0), samp_num_list[d]])
after_nodes = np.random.choice(
num_nodes, s_num, p=p, replace=False)
after_nodes = np.unique(after_nodes)
after_nodes = np.concatenate(
[previous_nodes, np.setdiff1d(after_nodes, previous_nodes)])
adj = U[:, after_nodes].multiply(1/p[after_nodes])
adj = row_normalize(adj)
adjs += [sparse_mx_to_torch_sparse_tensor(adj)]
sampled_nodes += [previous_nodes]
previous_nodes = after_nodes
sampled_nodes.reverse()
adjs.reverse()
return adjs, previous_nodes, batch_nodes, probs_nodes, sampled_nodes
class cluster_sampler(base_sampler):
def __init__(self, adj_matrix, train_nodes, num_clusters):
assert(adj_matrix.diagonal().sum() == 0) # make sure diagnal is zero
# make sure is symmetric
assert((adj_matrix != adj_matrix.T).nnz == 0)
self.adj_matrix = adj_matrix
self.lap_matrix = normalize(adj_matrix+sp.eye(adj_matrix.shape[0]))
self.train_nodes = train_nodes
self.num_clusters = num_clusters
self.parts = partition_graph(
adj_matrix, train_nodes, num_clusters)
def sample_subgraph(self, seed, size=1):
np.random.seed(seed)
select = np.random.choice(self.num_clusters, size, replace=False)
select = [self.parts[i] for i in select]
batch_nodes = np.concatenate(select)
return batch_nodes
def mini_batch(self, seed, batch_nodes, probs_nodes, samp_num_list, num_nodes, adj_matrix, depth):
bsize = samp_num_list[0]
batch_nodes = self.sample_subgraph(seed, bsize)
sampled_nodes = []
adj = self.adj_matrix[batch_nodes, :][:, batch_nodes]
adj = normalize(adj+sp.eye(adj.shape[0]))
adjs = []
for d in range(depth):
adjs += [sparse_mx_to_torch_sparse_tensor(adj)]
sampled_nodes.append(batch_nodes)
adjs.reverse()
sampled_nodes.reverse()
return adjs, batch_nodes, batch_nodes, probs_nodes, sampled_nodes
class graphsage_sampler(base_sampler):
def __init__(self, adj_matrix, train_nodes):
assert(adj_matrix.diagonal().sum() == 0) # make sure diagnal is zero
# make sure is symmetric
assert((adj_matrix != adj_matrix.T).nnz == 0)
self.adj_matrix = adj_matrix
self.train_nodes = train_nodes
self.lap_matrix = normalize(
adj_matrix + sp.eye(adj_matrix.shape[0]))
self.lap_norm = np.array(np.sum(self.lap_matrix, axis=1))
def mini_batch(self, seed, batch_nodes, probs_nodes, samp_num_list, num_nodes, adj_matrix, depth):
np.random.seed(seed)
sampled_nodes = []
previous_nodes = batch_nodes
adjs = []
for d in range(depth):
U = self.lap_matrix[previous_nodes, :]
after_nodes = []
for U_row in U:
indices = U_row.indices
sampled_indices = np.random.choice(
indices, samp_num_list[d], replace=True)
after_nodes.append(sampled_indices)
after_nodes = np.unique(np.concatenate(after_nodes))
after_nodes = np.concatenate(
[previous_nodes, np.setdiff1d(after_nodes, previous_nodes)])
adj = U[:, after_nodes]
adj = self.row_norm(adj, previous_nodes)
adjs += [sparse_mx_to_torch_sparse_tensor(adj)]
sampled_nodes.append(previous_nodes)
previous_nodes = after_nodes
adjs.reverse()
sampled_nodes.reverse()
return adjs, previous_nodes, batch_nodes, probs_nodes, sampled_nodes
class vrgcn_sampler(base_sampler):
def __init__(self, adj_matrix, train_nodes):
assert(adj_matrix.diagonal().sum() == 0) # make sure diagnal is zero
# make sure is symmetric
assert((adj_matrix != adj_matrix.T).nnz == 0)
self.adj_matrix = adj_matrix
self.lap_matrix = normalize(
adj_matrix + sp.eye(adj_matrix.shape[0]))
self.train_nodes = train_nodes
self.lap_norm = np.array(np.sum(self.lap_matrix, axis=1))
def mini_batch(self, seed, batch_nodes, probs_nodes, samp_num_list, num_nodes, adj_matrix, depth):
np.random.seed(seed)
sampled_nodes = []
exact_input_nodes = []
previous_nodes = batch_nodes
adjs = []
adjs_exact = []
for d in range(depth):
U = self.lap_matrix[previous_nodes, :]
after_nodes = []
after_nodes_exact = []
for U_row in U:
indices = U_row.indices
s_num = min(len(indices), samp_num_list[d])
sampled_indices = np.random.choice(
indices, s_num, replace=False)
after_nodes.append(sampled_indices)
after_nodes_exact.append(indices)
after_nodes = np.unique(np.concatenate(after_nodes))
after_nodes = np.concatenate(
[previous_nodes, np.setdiff1d(after_nodes, previous_nodes)])
after_nodes_exact = np.unique(np.concatenate(after_nodes_exact))
after_nodes_exact = np.concatenate(
[previous_nodes, np.setdiff1d(after_nodes_exact, previous_nodes)])
adj = U[:, after_nodes]
adj = self.row_norm(adj, previous_nodes)
adjs += [sparse_mx_to_torch_sparse_tensor(adj)]
adj_exact = U[:, after_nodes_exact]
adjs_exact += [sparse_mx_to_torch_sparse_tensor(adj_exact)]
sampled_nodes.append(previous_nodes)
exact_input_nodes.append(after_nodes_exact)
previous_nodes = after_nodes
adjs.reverse()
sampled_nodes.reverse()
adjs_exact.reverse()
exact_input_nodes.reverse()
return adjs, adjs_exact, previous_nodes, batch_nodes, probs_nodes, sampled_nodes, exact_input_nodes
class graphsaint_sampler(base_sampler):
def __init__(self, adj_matrix, train_nodes, node_budget):
assert(adj_matrix.diagonal().sum() == 0) # make sure diagnal is zero
# make sure is symmetric
assert((adj_matrix != adj_matrix.T).nnz == 0)
self.adj_matrix = adj_matrix
self.lap_matrix = normalize(adj_matrix + sp.eye(adj_matrix.shape[0]))
adj_matrix_train = adj_matrix[train_nodes, :][:, train_nodes]
lap_matrix_train = normalize(
adj_matrix_train + sp.eye(adj_matrix_train.shape[0]))
self.lap_matrix_train = lap_matrix_train
lap_matrix_train_sq = lap_matrix_train.multiply(lap_matrix_train)
p = np.array(np.sum(lap_matrix_train_sq, axis=0))[0]
self.sample_prob = node_budget*p/p.sum()
self.train_nodes = train_nodes
self.node_budget = node_budget
self.lap_norm = np.array(np.sum(self.lap_matrix, axis=1))
def mini_batch(self, seed, batch_nodes, probs_nodes, samp_num_list, num_nodes, adj_matrix, depth):
np.random.seed(seed)
sample_mask = np.random.uniform(
0, 1, len(self.train_nodes)) <= self.sample_prob
probs_nodes = self.sample_prob[sample_mask]
batch_nodes = self.train_nodes[sample_mask]
adj = self.lap_matrix[batch_nodes, :][:, batch_nodes].multiply(1/probs_nodes)
adj = self.row_norm(adj, batch_nodes)
adjs = []
sampled_nodes = []
for d in range(depth):
adjs += [sparse_mx_to_torch_sparse_tensor(adj)]
sampled_nodes.append(batch_nodes)
adjs.reverse()
sampled_nodes.reverse()
return adjs, batch_nodes, batch_nodes, probs_nodes*len(self.train_nodes), sampled_nodes
class subgraph_sampler(base_sampler):
def __init__(self, adj_matrix, train_nodes):
assert(adj_matrix.diagonal().sum() == 0) # make sure diagnal is zero
# make sure is symmetric
assert((adj_matrix != adj_matrix.T).nnz == 0)
self.adj_matrix = adj_matrix.tocoo()
self.lap_matrix = normalize(adj_matrix + sp.eye(adj_matrix.shape[0]))
self.train_nodes = train_nodes
def dropedge(self, percent=0.8):
nnz = self.adj_matrix.nnz
perm = np.random.permutation(nnz)
preserve_nnz = int(nnz*percent)
perm = perm[:preserve_nnz]
adj_matrix = sp.csr_matrix((self.adj_matrix.data[perm],
(self.adj_matrix.row[perm],
self.adj_matrix.col[perm])),
shape=self.adj_matrix.shape)
lap_matrix = normalize(adj_matrix + sp.eye(adj_matrix.shape[0]))
return lap_matrix
def mini_batch(self, seed, batch_nodes, probs_nodes, samp_num_list, num_nodes, adj_matrix, depth):
# lap_matrix = self.dropedge(percent=0.8)
# adj = lap_matrix[batch_nodes, :][:, batch_nodes]
# U = lap_matrix[batch_nodes, :]
adj = self.lap_matrix[batch_nodes, :][:, batch_nodes]
U = self.lap_matrix[batch_nodes, :]
is_neighbor = np.array(np.sum(U, axis=0))[0]>0
neighbors = np.arange(len(is_neighbor))[is_neighbor]
neighbors = np.setdiff1d(neighbors, batch_nodes)
after_nodes_exact = np.concatenate([batch_nodes, neighbors])
adj_exact = U[:, after_nodes_exact]
# for U_row in U:
# indices = U_row.indices
# after_nodes.append(indices)
# after_nodes = np.unique(np.concatenate(after_nodes))
# after_nodes = np.setdiff1d(after_nodes, batch_nodes)
# after_nodes_exact = np.concatenate([batch_nodes, after_nodes])
# adj_exact = U[:, after_nodes_exact]
adjs = []
adjs_exact = []
sampled_nodes = []
input_nodes_exact = []
for d in range(depth):
adjs += [sparse_mx_to_torch_sparse_tensor(adj)]
adjs_exact += [sparse_mx_to_torch_sparse_tensor(adj_exact)]
sampled_nodes.append(batch_nodes)
input_nodes_exact.append(neighbors)
adjs.reverse()
adjs_exact.reverse()
sampled_nodes.reverse()
input_nodes_exact.reverse()
return adjs, adjs_exact, batch_nodes, batch_nodes, probs_nodes, sampled_nodes, input_nodes_exact
class exact_sampler(base_sampler):
def __init__(self, adj_matrix, train_nodes):
assert(adj_matrix.diagonal().sum() == 0) # make sure diagnal is zero
# make sure is symmetric
assert((adj_matrix != adj_matrix.T).nnz == 0)
self.adj_matrix = adj_matrix
self.train_nodes = train_nodes
self.lap_matrix = normalize(adj_matrix + sp.eye(adj_matrix.shape[0]))
def mini_batch(self, seed, batch_nodes, probs_nodes, samp_num_list, num_nodes, adj_matrix, depth):
sampled_nodes = []
previous_nodes = batch_nodes
adjs = []
for d in range(depth):
U = self.lap_matrix[previous_nodes, :]
after_nodes = []
for U_row in U:
indices = U_row.indices
after_nodes.append(indices)
after_nodes = np.unique(np.concatenate(after_nodes))
after_nodes = np.concatenate(
[previous_nodes, np.setdiff1d(after_nodes, previous_nodes)])
adj = U[:, after_nodes]
adjs += [sparse_mx_to_torch_sparse_tensor(adj)]
sampled_nodes.append(previous_nodes)
previous_nodes = after_nodes
adjs.reverse()
sampled_nodes.reverse()
return adjs, previous_nodes, batch_nodes, probs_nodes, sampled_nodes