-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.py
513 lines (500 loc) · 13.4 KB
/
args.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
import argparse
def str2bool(string):
if string == "true" or string == "True":
return True
else:
return False
def get_args():
parser = argparse.ArgumentParser(
"Train GraphS4mer on multivariate signals."
)
# General args
parser.add_argument(
"--save_dir",
type=str,
default=None,
help="Directory to save the outputs and checkpoints.",
)
parser.add_argument(
"--save_output",
type=str2bool,
default="true",
help="Whether to save model outputs.",
)
parser.add_argument(
"--save_attn_weights",
type=str2bool,
default="false",
help="Whether to save model outputs.",
)
parser.add_argument(
"--load_model_path",
type=str,
default=None,
help="Model checkpoint to start training/testing from.",
)
parser.add_argument(
"--do_train", default=True, type=str2bool, help="Whether perform training."
)
parser.add_argument(
"--freeze_s4",
default=False,
type=str2bool,
help="Whether to freeze pretrained S4."
)
parser.add_argument(
"--s4_pretrained_dir",
type=str,
default=None,
help="Dir to pretrained S4 model."
)
parser.add_argument(
"--gpus", type=int, default=1, help="Number of GPUs for training."
)
parser.add_argument(
"--gpu_id", default=[0], type=int, nargs="+", help="List of GPU IDs."
)
## Input args
parser.add_argument(
"--dataset",
type=str,
default="tuh",
choices=(
"tuh",
"pems_bay",
"dodh",
"icbeb",
),
)
parser.add_argument(
"--raw_data_dir", type=str, default=None, help="Dir to raw data."
)
parser.add_argument(
"--adj_mat_dir",
type=str,
default=None,
help="Dir to prior knowledge-based adj mat.",
)
parser.add_argument(
"--preproc_dir",
type=str,
default=None,
help="Dir to preprocessed freatures.",
)
parser.add_argument(
"--max_seq_len",
type=int,
default=None,
help="Maximum sequence length.",
)
parser.add_argument(
"--output_seq_len", type=int, default=1, help="Output sequence length."
)
parser.add_argument(
"--horizon",
type=int,
default=12,
help="Forecasting horizon. Only for forecasting tasks.",
)
parser.add_argument(
"--sampling_freq",
type=int,
default=100,
help="Sampling frequency for the dataset.",
)
parser.add_argument("--rand_seed", type=int, default=123, help="Random seed.")
## Model args
parser.add_argument(
"--model_name",
type=str,
default="graphs4mer",
choices=(
"graphs4mer",
"s4",
"temporal_gnn",
"lstm",
),
help="Name of model.",
)
### General model args
parser.add_argument(
"--num_nodes", type=int, default=19, help="Number of nodes in graph."
)
parser.add_argument(
"--num_gcn_layers", type=int, default=1, help="Number of graph conv layers."
)
parser.add_argument(
"--num_temporal_layers",
type=int,
default=4,
help="Number of temporal layers.",
)
parser.add_argument(
"--input_dim", type=int, default=1, help="Input seq feature dim."
)
parser.add_argument(
"--output_dim", type=int, default=1, help="Output seq feature dim."
)
parser.add_argument("--hidden_dim", type=int, default=128, help="Hidden dimension.")
parser.add_argument(
"--graph_pool",
type=str,
default=None,
choices=("max", "mean", "sum", None),
help="Graph pooling operation.",
)
parser.add_argument(
"--g_conv",
type=str,
default="gine",
choices=("graphsage", "gat", "gine", "gcn"),
help="Name of graph conv layer.",
)
parser.add_argument(
"--activation_fn",
type=str,
default="leaky_relu",
choices=("relu", "elu", "leaky_relu", "gelu"),
help="Activation function name.",
)
### self-attention GNN model args
parser.add_argument(
"--gin_mlp", type=str2bool, default=True, help="Whether to use MLP in GIN."
)
parser.add_argument(
"--train_eps",
type=str2bool,
default=True,
help="Whether to train episolon in GIN.",
)
parser.add_argument(
"--edge_top_perc",
type=float,
default=0.2,
help="Top fraction of edges to be kept.",
)
parser.add_argument(
"--prune_method",
type=str,
default="thresh",
choices=("thresh", "knn", "thresh_abs"),
help="Pruning method for graph.",
)
parser.add_argument(
"--undirected_graph",
type=str2bool,
default=True,
help="Whether make the graph undirected."
)
parser.add_argument(
"--thresh",
type=float,
default=None,
help="Absolute threshold for graph pruning.",
)
parser.add_argument(
"--temporal_model",
type=str,
default="s4",
choices=("gru", "s4"),
help="Name of temporal model.",
)
parser.add_argument(
"--temporal_pool",
type=str,
default=None,
choices=("adaptive", "last", "mean", "pool", "first", "sum", None),
help="Temporal pooling method",
)
parser.add_argument(
"--resolution",
type=int,
default=None,
help="Temporal resolution. Must be divisible by max_seq_len.",
)
parser.add_argument(
"--use_prior",
type=str2bool,
default=False,
help="Whether to use prior adj mat as a guide.",
)
parser.add_argument(
"--negative_slope",
type=float,
default=0.2,
help="Negative slope for LeakyReLU in GAT.",
)
parser.add_argument(
"--knn",
type=int,
default=2,
help="KNN neighbor to initialize the graph structure.",
)
parser.add_argument(
"--graph_learn_metric",
type=str,
default="self_attention",
choices=("self_attention", "adaptive"),
help="Metric to learn graph structure.",
)
parser.add_argument(
"--adj_embed_dim", type=int, default=16, help="Embedding dim for adaptive GSL."
)
parser.add_argument(
"--regularizations",
type=str,
nargs="+",
default=["feature_smoothing", "degree", "sparse"],
choices=("feature_smoothing", "degree", "sparse"),
help="List of regularizations to include in loss.",
)
parser.add_argument(
"--residual_weight",
type=float,
default=0.0,
help="Weight for residual connection in graph structure learning.",
)
parser.add_argument(
"--decay_residual_weight",
type=str2bool,
default=False,
help="Whether to decay the residual weight in graph structure learning."
)
parser.add_argument(
"--feature_smoothing_weight",
type=float,
default=0.0,
help="Loss weight for feature smoothing regularization.",
)
parser.add_argument(
"--degree_weight",
type=float,
default=0.0,
help="Loss weight for degree regularization.",
)
parser.add_argument(
"--sparse_weight",
type=float,
default=0.0,
help="Loss weight for sparsity regularization.",
)
# S4 model args
parser.add_argument(
"--bidirectional",
type=str2bool,
default="false",
help="Whether or not to use bidirectional temporal model.",
)
parser.add_argument(
"--state_dim", type=int, default=64, help="State dimension for S4."
)
parser.add_argument(
"--prenorm",
type=str2bool,
default="false",
help="Whether to add norm before S4 layer.",
)
parser.add_argument(
"--postact",
type=str,
default=None,
choices=(None, "glu"),
help="Post activation in S4.",
)
parser.add_argument(
"--channels", type=int, default=1, help="Channel size for S4 kernel."
)
## Training/test args
parser.add_argument(
"--task",
type=str,
default="classification",
choices=("classification", "regression"),
help="Model task.",
)
parser.add_argument(
"--train_batch_size", type=int, default=50, help="Training batch size."
)
parser.add_argument(
"--num_workers",
type=int,
default=4,
help="Number of sub-processes to use per data loader.",
)
parser.add_argument(
"--scheduler",
type=str,
default="timm_cosine",
choices=("cosine", "one_cycle", "timm_cosine"),
help="LR scheduler.",
)
parser.add_argument(
"--t_initial",
type=int,
default=100,
help="t_initial for timm_cosine scheduler",
)
parser.add_argument(
"--lr_min",
type=float,
default=1e-5,
help="lr_min for timm_cosine scheduler",
)
parser.add_argument(
"--cycle_decay",
type=float,
default=0.1,
help="cycle_decay for timm_cosine scheduler",
)
parser.add_argument(
"--warmup_lr_init",
type=float,
default=1e-6,
help="warmup_lr_init for timm_cosine scheduler",
)
parser.add_argument(
"--warmup_t",
type=int,
default=5,
help="warmup_t for timm_cosine scheduler",
)
parser.add_argument(
"--cycle_limit",
type=int,
default=1,
help="cycle_limit for timm_cosine scheduler",
)
parser.add_argument(
"--optimizer",
type=str,
default="adamw",
choices=("adam", "adamw"),
help="Optimizer name.",
)
parser.add_argument(
"--dropout",
type=float,
default=0.0,
help="Dropout rate.",
)
parser.add_argument(
"--metric_name",
type=str,
default="auroc",
choices=(
"F1",
"acc",
"loss",
"auroc",
"mae",
"rmse",
"mse",
"auprc",
"kappa",
"precision",
"recall",
"fbeta",
"gbeta",
),
help="Name of dev metric to determine best checkpoint.",
)
parser.add_argument(
"--eval_metrics",
type=str,
default=[],
nargs="+",
choices=(
"F1",
"acc",
"auroc",
"auprc",
"kappa",
"rmse",
"mae",
"mse",
"mape",
"precision",
"recall",
"fbeta",
"gbeta",
),
help="List of metrics for evaluation of classification problems",
)
parser.add_argument(
"--find_threshold_on",
type=str,
default=None,
help="Which metric to maximize for cutoff thresholding."
)
parser.add_argument(
"--lr_init", type=float, default="0.01", help="Initial learning rate."
)
parser.add_argument("--l2_wd", type=float, default=5e-3, help="L2 weight decay.")
parser.add_argument(
"--num_epochs",
type=int,
default=100,
help="Number of epochs for which to train.",
)
parser.add_argument(
"--max_grad_norm",
type=float,
default=5.0,
help="Maximum gradient norm for gradient clipping.",
)
parser.add_argument(
"--test_batch_size", type=int, default=128, help="Dev/test batch size."
)
parser.add_argument(
"--metric_avg",
type=str,
default="macro",
help="weighted, micro, macro or binary.",
)
parser.add_argument(
"--patience",
type=int,
default=20,
help="Number of evaluations when eval loss is not decreasing before early stopping.",
)
parser.add_argument(
"--balanced_sampling",
default=False,
type=str2bool,
help="Whether to perform balanced_sampling.",
)
parser.add_argument(
"--accumulate_grad_batches",
default=1,
type=int,
help="Gradient accumulation batches.",
)
parser.add_argument(
"--pos_weight",
default=None,
type=float,
nargs='+',
help="Weight for positive class in BCE loss.",
)
parser.add_argument(
"--use_class_weight",
default=False,
type=str2bool,
help="Whether to use class weight for cross-entropy loss.",
)
args = parser.parse_args()
# which metric to maximize
if args.metric_name in ("loss", "mae", "rmse"):
# Best checkpoint is the one that minimizes loss
args.maximize_metric = False
elif args.metric_name in ("F1", "acc", "auroc", "auprc", "kappa"):
# Best checkpoint is the one that maximizes F1 or acc
args.maximize_metric = True
else:
raise ValueError('Unrecognized metric name: "{}"'.format(args.metric_name))
# must provide load_model_path if testing only
if (args.load_model_path is None) and not (args.do_train):
raise ValueError(
"For prediction only, please provide trained model checkpoint in argument load_model_path."
)
return args