-
Notifications
You must be signed in to change notification settings - Fork 0
/
ablation_gnns.py
611 lines (562 loc) · 23.7 KB
/
ablation_gnns.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import os
import sys
import argparse
import datetime
from typing import List, Optional, Tuple, Union
import pickle
import networkx as nx
import numpy as np
import pandas as pd
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
import torch
import torch.nn.functional as F
import torch_geometric as pyg
import torch_geometric.transforms as T
from subgraph_counting.config import parse_gossip, parse_neighborhood, parse_optimizer
from subgraph_counting.data import gen_query_ids, load_data
from subgraph_counting.lightning_model import (
GossipCountingModel,
NeighborhoodCountingModel,
)
from subgraph_counting.lightning_data import LightningDataLoader
from subgraph_counting.transforms import ToTconvHetero, ZeroNodeFeat
from subgraph_counting.workload import Workload
from subgraph_counting.utils import add_node_feat_to_networkx
from subgraph_counting.analysis import norm_mse, mae
def main(
args_neighborhood,
args_gossip,
args_opt,
train_neighborhood: bool = True,
train_gossip: bool = True,
test_gossip: bool = True,
neighborhood_checkpoint=None,
gossip_checkpoint=None,
nx_queries: List[nx.Graph] = None,
atlas_query_ids: List[int] = None,
output_dir: str = "results/raw",
):
"""
train the model and test accorrding to the config
"""
# define queries by atlas ids or networkx graphs
if nx_queries is None and atlas_query_ids is not None:
nx_queries = [nx.graph_atlas(i) for i in atlas_query_ids]
if args_neighborhood.use_node_feature:
# TODO: remove this in future implementations
nx_queries_with_node_feat = []
for query in nx_queries:
nx_queries_with_node_feat.extend(
add_node_feat_to_networkx(
query,
[t for t in np.eye(args_neighborhood.input_dim).tolist()],
"feat",
)
)
nx_queries = nx_queries_with_node_feat
query_ids = None
print("define queries with atlas ids:", query_ids)
print("query_ids set to None because node features are used")
else:
query_ids = atlas_query_ids
print("define queries with atlas ids:", query_ids)
elif nx_queries is not None and atlas_query_ids is None:
query_ids = None
print("define queries with nx graphs, number of query is", len(nx_queries))
print("length of nx_queries are: ", [len(q) for q in nx_queries])
print("query_ids set to None")
elif nx_queries is not None and atlas_query_ids is not None:
raise ValueError("nx_queries and atlas_query_ids cannot be both empty")
else:
raise ValueError("nx_queries and atlas_query_ids cannot be both None")
# define pre-transform
load_data_transform = [T.ToUndirected()]
if args_neighborhood.zero_node_feat:
load_data_transform.append(ZeroNodeFeat())
# neighborhood transformation
neighborhood_transform = ToTconvHetero() if args_neighborhood.use_tconv else None
assert args_neighborhood.use_hetero if args_neighborhood.use_tconv else True
if train_neighborhood or train_gossip:
# define training workload
train_dataset_name = args_opt.train_dataset
train_dataset = load_data(
train_dataset_name, transform=load_data_transform
) # TODO: add valid set mask support
train_workload = Workload(
train_dataset,
"data/" + train_dataset_name,
hetero_graph=False,
node_feat_len=args_neighborhood.input_dim
if args_neighborhood.use_node_feature
else -1,
)
if train_workload.exist_groundtruth(query_ids=query_ids, queries=nx_queries):
train_workload.canonical_count_truth = train_workload.load_groundtruth(
query_ids=query_ids, queries=nx_queries
)
else:
train_workload.canonical_count_truth = train_workload.compute_groundtruth(
query_ids=query_ids,
queries=nx_queries,
num_workers=args_opt.num_cpu,
save_to_file=True,
)
train_workload.generate_pipeline_datasets(
depth_neigh=args_neighborhood.depth,
neighborhood_transform=neighborhood_transform,
) # generate pipeline dataset, including neighborhood dataset and gossip dataset
# define validation workload
valid_dataset_name = args_opt.valid_dataset
valid_dataset = load_data(valid_dataset_name, transform=load_data_transform)
valid_workload = Workload(
valid_dataset,
"data/" + valid_dataset_name,
hetero_graph=False,
node_feat_len=args_neighborhood.input_dim
if args_neighborhood.use_node_feature
else -1,
)
if valid_workload.exist_groundtruth(query_ids=query_ids, queries=nx_queries):
valid_workload.canonical_count_truth = valid_workload.load_groundtruth(
query_ids=query_ids, queries=nx_queries
)
else:
valid_workload.canonical_count_truth = valid_workload.compute_groundtruth(
query_ids=query_ids,
queries=nx_queries,
num_workers=args_opt.num_cpu,
save_to_file=True,
)
valid_workload.generate_pipeline_datasets(
depth_neigh=args_neighborhood.depth,
neighborhood_transform=neighborhood_transform,
) # generate pipeline dataset, including neighborhood dataset and gossip dataset
# define testing workload
test_dataset_name = args_opt.test_dataset
test_dataset = load_data(test_dataset_name, transform=load_data_transform)
test_workload = Workload(
test_dataset,
"data/" + test_dataset_name,
hetero_graph=False,
node_feat_len=args_neighborhood.input_dim
if args_neighborhood.use_node_feature
else -1,
)
if test_workload.exist_groundtruth(query_ids=query_ids, queries=nx_queries):
test_workload.canonical_count_truth = test_workload.load_groundtruth(
query_ids=query_ids, queries=nx_queries
)
else:
test_workload.canonical_count_truth = test_workload.compute_groundtruth(
query_ids=query_ids,
queries=nx_queries,
num_workers=args_opt.num_cpu,
save_to_file=True,
) # compute ground truth if not any
test_workload.generate_pipeline_datasets(
depth_neigh=args_neighborhood.depth,
neighborhood_transform=neighborhood_transform,
) # generate pipeline dataset, including neighborhood dataset and gossip dataset
# define devices
if type(args_opt.gpu) == int: # single gpu
devices = [args_opt.gpu]
elif type(args_opt.gpu) == list: # multiple gpus
devices = args_opt.gpu
else:
Warning("gpu is not specified, using auto mode")
devices = ["auto"]
device = devices[0]
device = 0
########### begin neighborhood counting ###########
# define neighborhood counting dataset
neighborhood_dataloader = LightningDataLoader(
train_dataset=train_workload.neighborhood_dataset
if (train_neighborhood or train_gossip)
else None,
val_dataset=valid_workload.neighborhood_dataset
if (train_neighborhood or train_gossip)
else None,
test_dataset=test_workload.neighborhood_dataset,
batch_size=args_neighborhood.batch_size,
num_workers=args_opt.num_cpu,
shuffle=False,
)
# define neighborhood counting model
neighborhood_checkpoint_callback = ModelCheckpoint(
monitor="neighborhood_counting_val_loss",
mode="min",
save_top_k=1,
save_last=True,
)
neighborhood_trainer = pl.Trainer(
max_epochs=args_neighborhood.epoch_num,
accelerator="gpu",
devices=devices, # use only one gpu except for training
default_root_dir=args_neighborhood.model_path,
callbacks=[neighborhood_checkpoint_callback],
auto_lr_find=args_neighborhood.tune_lr,
auto_scale_batch_size=args_neighborhood.tune_bs,
)
if train_neighborhood and (neighborhood_checkpoint is None):
neighborhood_model = NeighborhoodCountingModel(
input_dim=args_neighborhood.input_dim,
hidden_dim=args_neighborhood.hidden_dim,
args=args_neighborhood,
)
else:
assert neighborhood_checkpoint is not None
print("loading neighborhood model from checkpoint: ", neighborhood_checkpoint)
neighborhood_model = NeighborhoodCountingModel.load_from_checkpoint(
neighborhood_checkpoint
) # to hetero is automatically done upon loading
neighborhood_model.set_queries(
query_ids=query_ids,
queries=nx_queries,
transform=neighborhood_transform,
hetero=False,
device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"),
)
# train neighborhood model
if train_neighborhood:
if args_neighborhood.tune_lr or args_neighborhood.tune_bs:
neighborhood_trainer.tune(
model=neighborhood_model, datamodule=neighborhood_dataloader
)
# multi-gpu training
if len(devices) > 1:
neighborhood_multigpu_trainer = pl.Trainer(
max_epochs=args_neighborhood.epoch_num,
accelerator="gpu",
devices=devices, # use multiple gpus for training
default_root_dir=args_neighborhood.model_path,
callbacks=[neighborhood_checkpoint_callback],
gpus=devices,
strategy="ddp",
)
neighborhood_multigpu_trainer.fit(
model=neighborhood_model,
datamodule=neighborhood_dataloader,
)
else:
neighborhood_trainer.fit(
model=neighborhood_model,
datamodule=neighborhood_dataloader,
)
neighborhood_best_model_path = neighborhood_checkpoint_callback.best_model_path
print("best neighborhood model path: ", neighborhood_best_model_path)
neighborhood_model = NeighborhoodCountingModel.load_from_checkpoint(
neighborhood_best_model_path
)
neighborhood_model.set_queries(
query_ids=query_ids,
queries=nx_queries,
transform=neighborhood_transform,
hetero=False,
device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"),
)
# test neighborhood counting model
neighborhood_trainer.test(
model=neighborhood_model, datamodule=neighborhood_dataloader
)
########### begin gossip counting ###########
skip_gossip = not (train_gossip or test_gossip)
# apply neighborhood count output to gossip dataset
if train_gossip:
neighborhood_count_train = torch.cat(
neighborhood_trainer.predict(
neighborhood_model, neighborhood_dataloader.train_dataloader()
),
dim=0,
) # size = (#neighborhood, #queries)
train_workload.apply_neighborhood_count(neighborhood_count_train)
neighborhood_count_valid = torch.cat(
neighborhood_trainer.predict(
neighborhood_model, neighborhood_dataloader.val_dataloader()
),
dim=0,
) # size = (#neighborhood, #queries)
valid_workload.apply_neighborhood_count(neighborhood_count_valid)
if test_gossip:
neighborhood_count_test = torch.cat(
neighborhood_trainer.predict(
neighborhood_model, neighborhood_dataloader.test_dataloader()
),
dim=0,
)
test_workload.apply_neighborhood_count(neighborhood_count_test)
# define gossip counting dataset
if not skip_gossip:
gossip_dataloader = LightningDataLoader(
train_dataset=train_workload.gossip_dataset if train_gossip else None,
val_dataset=valid_workload.gossip_dataset if train_gossip else None,
test_dataset=test_workload.gossip_dataset,
batch_size=args_gossip.batch_size,
num_workers=args_opt.num_cpu,
shuffle=False,
)
# define gossip counting model
input_dim = 1
args_gossip.use_hetero = False
if train_gossip and (gossip_checkpoint is None):
gossip_model = GossipCountingModel(
input_dim,
args_gossip.hidden_dim,
args_gossip,
emb_channels=args_neighborhood.hidden_dim,
input_pattern_emb=True,
)
elif test_gossip or (gossip_checkpoint is not None):
assert gossip_checkpoint is not None
print("loading gossip model from checkpoint: ", gossip_checkpoint)
gossip_model = GossipCountingModel.load_from_checkpoint(gossip_checkpoint)
else:
print("No gossip training or testing is specified, skip gossip counting.")
if not skip_gossip:
gossip_model.set_query_emb(neighborhood_model.get_query_emb())
gossip_checkpoint_callback = ModelCheckpoint(
monitor="gossip_counting_val_loss", mode="min", save_top_k=1, save_last=True
)
gossip_trainer = pl.Trainer(
max_epochs=args_gossip.epoch_num,
accelerator="gpu",
devices=[device], # use only one gpu except for training
default_root_dir=args_gossip.model_path,
detect_anomaly=True,
callbacks=[gossip_checkpoint_callback],
auto_lr_find=args_gossip.tune_lr,
auto_scale_batch_size="power" if args_gossip.tune_bs else None,
)
# train gossip model
if train_gossip:
if args_gossip.tune_lr or args_gossip.tune_bs:
gossip_trainer.tune(gossip_model, gossip_dataloader)
if len(devices) > 1:
raise NotImplementedError(
"multi-gpu training for gossip model is not recommended."
)
gossip_multigpu_trainer = pl.Trainer(
max_epochs=args_gossip.epoch_num,
accelerator="gpu",
devices=devices, # use multiple gpus for training
callbacks=[gossip_checkpoint_callback],
default_root_dir=args_gossip.model_path,
strategy="ddp",
)
gossip_multigpu_trainer.fit(
model=gossip_model,
datamodule=gossip_dataloader,
)
else:
gossip_trainer.fit(
model=gossip_model,
datamodule=gossip_dataloader,
)
gossip_best_model_path = gossip_checkpoint_callback.best_model_path
print("best gossip model path: ", gossip_best_model_path)
gossip_model = GossipCountingModel.load_from_checkpoint(gossip_best_model_path)
gossip_model.set_query_emb(neighborhood_model.get_query_emb())
elif test_gossip:
gossip_trainer.test(gossip_model, datamodule=gossip_dataloader)
########### output prediction results ###########
# configurations
file_name = "config_{}.txt".format(args_opt.test_dataset)
with open(os.path.join(output_dir, file_name), "w") as f:
f.write("args_opt: \n")
f.write(str(args_opt))
f.write("\nargs_neighborhood:\n")
f.write(str(args_neighborhood))
f.write("\nargs_gossip:\n")
f.write(str(args_gossip))
f.write("\ntime:\n")
f.write(str(datetime.datetime.now()))
neighborhood_count_test = torch.cat(
neighborhood_trainer.predict(
neighborhood_model, neighborhood_dataloader.test_dataloader()
),
dim=0,
)
graphlet_neighborhood_count_test = (
test_workload.neighborhood_dataset.aggregate_neighborhood_count(
neighborhood_count_test
)
) # user can get the graphlet count of each graph in this way
# graphlet count after neighborhood counting
file_name = "neighborhood_graphlet_{}.csv".format(args_opt.test_dataset)
pd.DataFrame(
torch.round(F.relu(graphlet_neighborhood_count_test)).detach().cpu().numpy()
).to_csv(
os.path.join(output_dir, file_name)
) # save the inferenced results to csv file
# graphlet count after gossip counting
if not skip_gossip:
file_name = "gossip_graphlet_{}.csv".format(args_opt.test_dataset)
gossip_count_test = torch.cat(
gossip_trainer.predict(gossip_model, gossip_dataloader.test_dataloader()),
dim=0,
)
graphlet_gossip_count_test = (
test_workload.gossip_dataset.aggregate_neighborhood_count(gossip_count_test)
) # user can get the graphlet count of each graph in this way
pd.DataFrame(
torch.round(F.relu(graphlet_gossip_count_test)).detach().cpu().numpy()
).to_csv(
os.path.join(output_dir, file_name)
) # save the inferenced results to csv file
# gossip gate value analysis
if not skip_gossip and args_gossip.conv_type == "GOSSIP":
file_name = "gossip_gate_{}.csv".format(args_opt.test_dataset)
gossip_gate_test = gossip_model._gate_value(gossip_model.query_emb).squeeze(
dim=-1
)
pd.DataFrame(gossip_gate_test.detach().cpu().numpy()).to_csv(
os.path.join(output_dir, file_name)
)
# node level count after neighborhood counting
file_name = "neighborhood_node_{}".format(args_opt.test_dataset)
pd.DataFrame(neighborhood_count_test.detach().cpu().numpy()).to_csv(
os.path.join(output_dir, file_name + "_results.csv")
) # save the inferenced results to csv file
pd.DataFrame(test_workload.neighborhood_dataset.nx_neighs_index).to_csv(
os.path.join(output_dir, file_name + "_index.csv")
) # save the inferenced results to csv file
# node level count after gossip counting
file_name = "gossip_node_{}".format(args_opt.test_dataset)
if not skip_gossip:
pd.DataFrame(gossip_count_test.detach().cpu().numpy()).to_csv(
os.path.join(output_dir, file_name + "_results.csv")
) # save the inferenced results to csv file
# save the test networkx graph
file_name = "test_nxgraph_{}.pk".format(args_opt.test_dataset)
with open(os.path.join(output_dir, file_name), "wb") as f:
pickle.dump(test_workload.to_networkx(), f)
########### analyze the output data ###########
# group the results by query graph size
query_size_dict = {i: len(g) for i, g in enumerate(nx_queries)}
size_order_dict = {
size: i for i, size in enumerate(sorted(set(query_size_dict.values())))
}
groupby_list = [[] for _ in range(len(size_order_dict))]
for i in query_size_dict.keys():
groupby_list[size_order_dict[query_size_dict[i]]].append(i)
# analyze the graphlet count for neighborhood counting
truth_graphlet = test_workload.gossip_dataset.aggregate_neighborhood_count(
test_workload.canonical_count_truth
).numpy() # shape (num_graphs, num_queries)
pred_graphlet_neighborhood = (
torch.round(F.relu(graphlet_neighborhood_count_test)).detach().cpu().numpy()
)
norm_mse_neighborhood = norm_mse(
pred=pred_graphlet_neighborhood, truth=truth_graphlet, groupby=groupby_list
)
mae_neighborhood = mae(
pred=pred_graphlet_neighborhood, truth=truth_graphlet, groupby=groupby_list
)
print("graphlet_norm_mse_neighborhood: {}".format(norm_mse_neighborhood))
print("graphlet_mae_neighborhood: {}".format(mae_neighborhood))
# analyze the graphlet count for gossip counting
if not skip_gossip:
pred_graphlet_gossip = (
torch.round(F.relu(graphlet_gossip_count_test)).detach().cpu().numpy()
)
norm_mse_gossip = norm_mse(
pred=pred_graphlet_gossip, truth=truth_graphlet, groupby=groupby_list
)
mae_gossip = mae(
pred=pred_graphlet_gossip, truth=truth_graphlet, groupby=groupby_list
)
print("graphlet_norm_mse_gossip: {}".format(norm_mse_gossip))
print("graphlet_mae_gossip: {}".format(mae_gossip))
# save the results
file_name = "analyze_results_{}.txt".format(args_opt.test_dataset)
with open(os.path.join(output_dir, file_name), "w") as f:
f.write("graphlet_norm_mse_neighborhood: {}\n".format(norm_mse_neighborhood))
f.write("graphlet_mae_neighborhood: {}\n".format(mae_neighborhood))
if not skip_gossip:
f.write("graphlet_norm_mse_gossip: {}\n".format(norm_mse_gossip))
f.write("graphlet_mae_gossip: {}\n".format(mae_gossip))
print("done")
if __name__ == "__main__":
# load parameters
parser = argparse.ArgumentParser(description="DeSCo argument parser")
# define optimizer arguments
_optimizer_actions = parse_optimizer(parser)
# define neighborhood counting model arguments
_neighbor_actions = parse_neighborhood(parser)
# define gossip counting model arguments
_gossip_actions = parse_gossip(parser)
# assign the args to args_neighborhood, args_gossip, and args_opt without the prefix 'neigh_' and 'gossip_'
args = parser.parse_args()
print(args)
args_neighborhood = argparse.Namespace()
args_gossip = argparse.Namespace()
args_opt = argparse.Namespace()
for action in _optimizer_actions:
setattr(args_opt, action.dest, getattr(args, action.dest))
for action in _neighbor_actions:
prefix = "neigh_"
setattr(
args_neighborhood,
action.dest[len(prefix) :]
if action.dest.startswith(prefix)
else action.dest,
getattr(args, action.dest),
)
for action in _gossip_actions:
prefix = "gossip_"
setattr(
args_gossip,
action.dest[len(prefix) :]
if action.dest.startswith(prefix)
else action.dest,
getattr(args, action.dest),
)
# args for ablation study model
args_neighborhood.use_hetero = False
args_neighborhood.use_tconv = False
args_opt.test_gossip = False
args_opt.train_gossip = False
args_neighborhood.conv_type = "SAGE"
# noqa: the following restrictions are added because of the limited implemented senarios
# assert args_neighborhood.use_hetero == True
# noqa: if need to load from checkpoint, please specify the checkpoint path
neighborhood_checkpoint = args_opt.neigh_checkpoint
gossip_checkpoint = args_opt.gossip_checkpoint
# define the query graphs
query_ids = gen_query_ids(query_size=[3, 4, 5])
# query_ids = [6]
nx_queries = [nx.graph_atlas(i) for i in query_ids]
if args_neighborhood.use_node_feature:
nx_queries_with_node_feat = []
for query in nx_queries:
nx_queries_with_node_feat.extend(
add_node_feat_to_networkx(
query,
[t for t in np.eye(args_neighborhood.input_dim).tolist()],
"feat",
)
)
nx_queries = nx_queries_with_node_feat
query_ids = None
# define the output directory
if args_opt.output_dir is None:
output_dir = "results/kdd23/raw"
time = datetime.datetime.now().strftime("%Y%m%d_%H:%M:%S")
output_dir = os.path.join(output_dir, time)
else:
output_dir = args_opt.output_dir
if not os.path.exists(output_dir):
os.makedirs(output_dir)
main(
args_neighborhood,
args_gossip,
args_opt,
train_neighborhood=args_opt.train_neigh,
train_gossip=args_opt.train_gossip,
test_gossip=args_opt.test_gossip,
neighborhood_checkpoint=neighborhood_checkpoint,
gossip_checkpoint=gossip_checkpoint,
nx_queries=nx_queries,
atlas_query_ids=query_ids,
output_dir=output_dir,
)