-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.py
90 lines (81 loc) · 2.4 KB
/
main.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
import warnings
warnings.filterwarnings('ignore')
import sys
sys.path.append('.')
sys.path.append('..')
import yaml
import argparse
import traceback
import time
import torch
from model.models import STSSL
from model.trainer import Trainer
from lib.dataloader import get_dataloader
from lib.utils import (
init_seed,
get_model_params,
load_graph,
)
def model_supervisor(args):
init_seed(args.seed)
if not torch.cuda.is_available():
args.device = 'cpu'
## load dataset
dataloader = get_dataloader(
data_dir=args.data_dir,
dataset=args.dataset,
batch_size=args.batch_size,
test_batch_size=args.test_batch_size,
)
graph = load_graph(args.graph_file, device=args.device)
args.num_nodes = len(graph)
## init model and set optimizer
model = STSSL(args).to(args.device)
model_parameters = get_model_params([model])
optimizer = torch.optim.Adam(
params=model_parameters,
lr=args.lr_init,
eps=1.0e-8,
weight_decay=0,
amsgrad=False
)
## start training
trainer = Trainer(
model=model,
optimizer=optimizer,
dataloader=dataloader,
graph=graph,
args=args
)
results = None
try:
if args.mode == 'train':
results = trainer.train() # best_eval_loss, best_epoch
elif args.mode == 'test':
# test
state_dict = torch.load(
args.best_path,
map_location=torch.device(args.device)
)
model.load_state_dict(state_dict['model'])
print("Load saved model")
results = trainer.test(model, dataloader['test'], dataloader['scaler'],
graph, trainer.logger, trainer.args)
else:
raise ValueError
except:
trainer.logger.info(traceback.format_exc())
return results
if __name__=='__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config_filename', default='configs/NYCBike1.yaml',
type=str, help='the configuration to use')
args = parser.parse_args()
print(f'Starting experiment with configurations in {args.config_filename}...')
time.sleep(3)
configs = yaml.load(
open(args.config_filename),
Loader=yaml.FullLoader
)
args = argparse.Namespace(**configs)
model_supervisor(args)