-
Notifications
You must be signed in to change notification settings - Fork 0
/
subgraph_gcn.py
338 lines (267 loc) · 12.8 KB
/
subgraph_gcn.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
from utils import *
from packages import *
from optimizers import sgd_step, variance_reduced_step, boost_step
from forward_wrapper import ForwardWrapper
from model import GCN, GraphSageGCN
"""
ClusterGCN
"""
def clustergcn(feat_data, labels, adj_matrix, train_nodes, valid_nodes, test_nodes, args, device, concat=False):
if args.dataset=='ppi':
samp_num_list = np.array([1 for _ in range(args.n_layers)])
cluster_num = 50
else:
samp_num_list = np.array([int(args.batch_size/128) for _ in range(args.n_layers)])
cluster_num = int(len(train_nodes)/128)
# use multiprocess sample data
process_ids = np.arange(args.batch_num)
print(cluster_num)
cluster_sampler_ = cluster_sampler(adj_matrix, train_nodes, cluster_num)
if concat:
susage = GraphSageGCN(nfeat=feat_data.shape[1], nhid=args.nhid, num_classes=args.num_classes,
layers=args.n_layers, dropout=args.dropout, multi_class=args.multi_class).to(device)
else:
susage = GCN(nfeat=feat_data.shape[1], nhid=args.nhid, num_classes=args.num_classes,
layers=args.n_layers, dropout=args.dropout, multi_class=args.multi_class).to(device)
susage.to(device)
print(susage)
optimizer = optim.Adam(susage.parameters(), lr=0.01)
adjs_full, input_nodes_full, sampled_nodes_full = cluster_sampler_.full_batch(
train_nodes, len(feat_data), args.n_layers)
adjs_full = package_mxl(adjs_full, device)
best_model = copy.deepcopy(susage)
susage.zero_grad()
cur_test_loss = susage.calculate_loss_grad(
feat_data, adjs_full, labels, valid_nodes)
best_val, cnt = 0, 0
loss_train = [cur_test_loss]
loss_test = [cur_test_loss]
grad_variance_all = []
loss_train_all = [cur_test_loss]
times = []
data_prepare_times = []
for epoch in np.arange(args.epoch_num):
train_nodes_p = args.batch_size * \
np.ones_like(train_nodes)/len(train_nodes)
# prepare train data
tp0 = time.time()
pool = mp.Pool(args.pool_num)
jobs = prepare_data(pool, cluster_sampler_.mini_batch, process_ids, train_nodes, train_nodes_p, samp_num_list, len(feat_data),
adj_matrix, args.n_layers)
# fetch train data
train_data = [job.get() for job in jobs]
pool.close()
pool.join()
tp1 = time.time()
data_prepare_times += [tp1-tp0]
inner_loop_num = args.batch_num
t0 = time.time()
cur_train_loss, cur_train_loss_all, grad_variance = sgd_step(susage, optimizer, feat_data, labels,
train_nodes, valid_nodes,
adjs_full, train_data, inner_loop_num, device,
calculate_grad_vars=bool(args.show_grad_norm) if epoch<int(200/args.batch_num) else False)
t1 = time.time()
times += [t1-t0]
print('sgcn run time per epoch is %0.3f' % (t1-t0))
loss_train_all.extend(cur_train_loss_all)
grad_variance_all.extend(grad_variance)
# calculate test loss
susage.eval()
susage.zero_grad()
cur_test_loss = susage.calculate_loss_grad(
feat_data, adjs_full, labels, valid_nodes)
val_f1 = susage.calculate_f1(feat_data, adjs_full, labels, valid_nodes)
if val_f1 > best_val:
best_model = copy.deepcopy(susage)
if val_f1 > best_val + 1e-2:
best_val = val_f1
cnt = 0
else:
cnt += 1
if cnt == args.n_stops // args.batch_num:
break
loss_train.append(cur_train_loss)
loss_test.append(cur_test_loss)
# print progress
print('Epoch: ', epoch,
'| train loss: %.8f' % cur_train_loss,
'| test loss: %.8f' % cur_test_loss,
'| test f1: %.8f' % val_f1)
f1_score_test = best_model.calculate_f1(
feat_data, adjs_full, labels, test_nodes)
if bool(args.show_grad_norm):
times, data_prepare_times = times[int(200/args.batch_num):], data_prepare_times[int(200/args.batch_num):]
print('Average training time is %0.3f' % np.mean(times))
print('Average data prepare time is %0.3f'%np.mean(data_prepare_times))
return best_model, loss_train, loss_test, loss_train_all, f1_score_test, grad_variance_all
"""
GraphSaint
"""
def graphsaint(feat_data, labels, adj_matrix, train_nodes, valid_nodes, test_nodes, args, device, concat=False):
samp_num_list = np.array([args.samp_num for _ in range(args.n_layers)])
# use multiprocess sample data
process_ids = np.arange(args.batch_num)
graphsaint_sampler_ = graphsaint_sampler(adj_matrix, train_nodes, node_budget=args.batch_size)
if concat:
susage = GraphSageGCN(nfeat=feat_data.shape[1], nhid=args.nhid, num_classes=args.num_classes,
layers=args.n_layers, dropout=args.dropout, multi_class=args.multi_class).to(device)
else:
susage = GCN(nfeat=feat_data.shape[1], nhid=args.nhid, num_classes=args.num_classes,
layers=args.n_layers, dropout=args.dropout, multi_class=args.multi_class).to(device)
susage.to(device)
print(susage)
optimizer = optim.Adam(susage.parameters())
adjs_full, input_nodes_full, sampled_nodes_full = graphsaint_sampler_.full_batch(
train_nodes, len(feat_data), args.n_layers)
adjs_full = package_mxl(adjs_full, device)
best_model = copy.deepcopy(susage)
susage.zero_grad()
cur_test_loss = susage.calculate_loss_grad(
feat_data, adjs_full, labels, valid_nodes)
best_val, cnt = 0, 0
loss_train = [cur_test_loss]
loss_test = [cur_test_loss]
grad_variance_all = []
loss_train_all = [cur_test_loss]
times = []
data_prepare_times = []
for epoch in np.arange(args.epoch_num):
train_nodes_p = args.batch_size * \
np.ones_like(train_nodes)/len(train_nodes)
# prepare train data
tp0 = time.time()
pool = mp.Pool(args.pool_num)
jobs = prepare_data(pool, graphsaint_sampler_.mini_batch, process_ids, train_nodes, train_nodes_p, samp_num_list, len(feat_data),
adj_matrix, args.n_layers)
# fetch train data
train_data = [job.get() for job in jobs]
pool.close()
pool.join()
tp1 = time.time()
data_prepare_times += [tp1-tp0]
inner_loop_num = args.batch_num
t2 = time.time()
cur_train_loss, cur_train_loss_all, grad_variance = boost_step(susage, optimizer, feat_data, labels,
train_nodes, valid_nodes,
adjs_full, train_data, inner_loop_num, device,
calculate_grad_vars=bool(args.show_grad_norm) if epoch<int(200/args.batch_num) else False)
t3 = time.time()
times += [t3-t2]
print('mvs_gcn_plus run time per epoch is %0.3f' % (t3-t2))
loss_train_all.extend(cur_train_loss_all)
grad_variance_all.extend(grad_variance)
# calculate test loss
susage.eval()
susage.zero_grad()
cur_test_loss = susage.calculate_loss_grad(
feat_data, adjs_full, labels, valid_nodes)
val_f1 = susage.calculate_f1(feat_data, adjs_full, labels, valid_nodes)
if val_f1 > best_val:
best_model = copy.deepcopy(susage)
if val_f1 > best_val + 1e-2:
best_val = val_f1
cnt = 0
else:
cnt += 1
if cnt == args.n_stops // args.batch_num:
break
loss_train.append(cur_train_loss)
loss_test.append(cur_test_loss)
# print progress
print('Epoch: ', epoch,
'| train loss: %.8f' % cur_train_loss,
'| val loss: %.8f' % cur_test_loss,
'| val f1: %.8f' % val_f1)
if bool(args.show_grad_norm):
times, data_prepare_times = times[int(200/args.batch_num):], data_prepare_times[int(200/args.batch_num):]
print('Average training time is %0.3f' % np.mean(times))
print('Average data prepare time is %0.3f'%np.mean(data_prepare_times))
f1_score_test = best_model.calculate_f1(
feat_data, adjs_full, labels, test_nodes)
return best_model, loss_train, loss_test, loss_train_all, f1_score_test, grad_variance_all
"""
Variance Reduced Sampling GCN
"""
# def subgraph_gcn(feat_data, labels, adj_matrix, train_nodes, valid_nodes, test_nodes, args, device, concat=False):
# samp_num_list = np.array([args.samp_num for _ in range(args.n_layers)])
# wrapper = ForwardWrapper(n_nodes=len(
# feat_data), n_hid=args.nhid, n_layers=args.n_layers, n_classes=args.num_classes)
# # use multiprocess sample data
# process_ids = np.arange(args.batch_num)
# subgraph_sampler_ = subgraph_sampler(adj_matrix, train_nodes)
# if concat:
# susage = GraphSageGCN(nfeat=feat_data.shape[1], nhid=args.nhid, num_classes=args.num_classes,
# layers=args.n_layers, dropout=args.dropout, multi_class=args.multi_class).to(device)
# else:
# susage = GCN(nfeat=feat_data.shape[1], nhid=args.nhid, num_classes=args.num_classes,
# layers=args.n_layers, dropout=args.dropout, multi_class=args.multi_class).to(device)
# susage.to(device)
# print(susage)
# optimizer = optim.Adam(susage.parameters())
# adjs_full, input_nodes_full, sampled_nodes_full = subgraph_sampler_.full_batch(
# train_nodes, len(feat_data), args.n_layers)
# adjs_full = package_mxl(adjs_full, device)
# best_model = copy.deepcopy(susage)
# susage.zero_grad()
# cur_test_loss = susage.calculate_loss_grad(
# feat_data, adjs_full, labels, valid_nodes)
# best_val, cnt = 0, 0
# loss_train = [cur_test_loss]
# loss_test = [cur_test_loss]
# grad_variance_all = []
# loss_train_all = [cur_test_loss]
# times = []
# data_prepare_times = []
# for epoch in np.arange(args.epoch_num):
# train_nodes_p = args.batch_size * \
# np.ones_like(train_nodes)/len(train_nodes)
# susage.zero_grad()
# # prepare train data
# tp0 = time.time()
# pool = mp.Pool(args.pool_num)
# jobs = prepare_data(pool, subgraph_sampler_.mini_batch, process_ids, train_nodes, train_nodes_p, samp_num_list, len(feat_data),
# adj_matrix, args.n_layers)
# # fetch train data
# train_data = [job.get() for job in jobs]
# pool.close()
# pool.join()
# tp1 = time.time()
# data_prepare_times += [tp1-tp0]
# inner_loop_num = args.batch_num
# t2 = time.time()
# cur_train_loss, cur_train_loss_all, grad_variance = variance_reduced_step(susage, optimizer, feat_data, labels,
# train_nodes, valid_nodes,
# adjs_full, train_data, inner_loop_num, device, wrapper,
# calculate_grad_vars=bool(args.show_grad_norm) if epoch<int(200/args.batch_num) else False)
# t3 = time.time()
# times += [t3-t2]
# print('mvs_gcn_plus run time per epoch is %0.3f' % (t3-t2))
# loss_train_all.extend(cur_train_loss_all)
# grad_variance_all.extend(grad_variance)
# # calculate test loss
# susage.eval()
# susage.zero_grad()
# cur_test_loss = susage.calculate_loss_grad(
# feat_data, adjs_full, labels, valid_nodes)
# val_f1 = susage.calculate_f1(feat_data, adjs_full, labels, valid_nodes)
# if val_f1 > best_val:
# best_model = copy.deepcopy(susage)
# if val_f1 > best_val + 1e-2:
# best_val = val_f1
# cnt = 0
# else:
# cnt += 1
# if cnt == args.n_stops // args.batch_num:
# break
# loss_train.append(cur_train_loss)
# loss_test.append(cur_test_loss)
# # print progress
# print('Epoch: ', epoch,
# '| train loss: %.8f' % cur_train_loss,
# '| val loss: %.8f' % cur_test_loss,
# '| val f1: %.8f' % val_f1)
# print('Average time is %0.3f' % np.mean(times))
# print('Average data prepare time is %0.3f'%np.mean(data_prepare_times))
# f1_score_test = best_model.calculate_f1(
# feat_data, adjs_full, labels, test_nodes)
# return best_model, loss_train, loss_test, loss_train_all, f1_score_test, grad_variance_all