-
Notifications
You must be signed in to change notification settings - Fork 4
/
Run.py
executable file
·167 lines (151 loc) · 7.69 KB
/
Run.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
import argparse
import configparser
import os
from datetime import datetime
import torch
import torch.nn as nn
from lib.adj_matrix import get_adjacency_matrix, matrix_svd
from lib.dataloader import get_dataloader
from lib.TrainInits import init_seed, print_model_parameters
from model.ATGCN import ATGCN as Network
from model.BasicTrainer import Trainer
#*************************************************************************#
Mode = 'train'
DEBUG = 'True'
DATASET = 'PEMSD4' #PEMSD4 or PEMSD8
DEVICE = 'cuda:0'
MODEL = 'ATGCN'
#get configuration
config_file = './model/{}_{}.conf'.format(DATASET, MODEL)
#print('Read configuration file: %s' % (config_file))
config = configparser.ConfigParser()
config.read(config_file)
from lib.metrics import MAE_torch
def masked_mae_loss(scaler, mask_value):
def loss(preds, labels):
if scaler:
preds = scaler.inverse_transform(preds)
labels = scaler.inverse_transform(labels)
mae = MAE_torch(pred=preds, true=labels, mask_value=mask_value)
return mae
return loss
#parser
args = argparse.ArgumentParser(description='arguments')
args.add_argument('--dataset', default=DATASET, type=str)
args.add_argument('--mode', default=Mode, type=str)
args.add_argument('--device', default=DEVICE, type=str, help='indices of GPUs')
args.add_argument('--debug', default=DEBUG, type=eval)
args.add_argument('--model', default=MODEL, type=str)
args.add_argument('--cuda', default=True, type=bool)
#data
args.add_argument('--val_ratio', default=config['data']['val_ratio'], type=float)
args.add_argument('--test_ratio', default=config['data']['test_ratio'], type=float)
args.add_argument('--lag', default=config['data']['lag'], type=int)
args.add_argument('--horizon', default=config['data']['horizon'], type=int)
args.add_argument('--num_nodes', default=config['data']['num_nodes'], type=int)
args.add_argument('--tod', default=config['data']['tod'], type=eval)
args.add_argument('--normalizer', default=config['data']['normalizer'], type=str)
args.add_argument('--column_wise', default=config['data']['column_wise'], type=eval)
args.add_argument('--graph_path', default=config['data']['graph_path'], type=str)
#model
args.add_argument('--input_dim', default=config['model']['input_dim'], type=int)
args.add_argument('--output_dim', default=config['model']['output_dim'], type=int)
args.add_argument('--embed_dim', default=config['model']['embed_dim'], type=int)
args.add_argument('--rnn_units', default=config['model']['rnn_units'], type=int)
args.add_argument('--num_layers', default=config['model']['num_layers'], type=int)
args.add_argument('--cheb_k', default=config['model']['cheb_order'], type=int)
#train
args.add_argument('--loss_func', default=config['train']['loss_func'], type=str)
args.add_argument('--seed', default=config['train']['seed'], type=int)
args.add_argument('--batch_size', default=config['train']['batch_size'], type=int)
args.add_argument('--epochs', default=config['train']['epochs'], type=int)
args.add_argument('--lr_init', default=config['train']['lr_init'], type=float)
args.add_argument('--lr_decay', default=config['train']['lr_decay'], type=eval)
args.add_argument('--lr_decay_rate', default=config['train']['lr_decay_rate'], type=float)
args.add_argument('--lr_decay_step', default=config['train']['lr_decay_step'], type=str)
args.add_argument('--early_stop', default=config['train']['early_stop'], type=eval)
args.add_argument('--early_stop_patience', default=config['train']['early_stop_patience'], type=int)
args.add_argument('--grad_norm', default=config['train']['grad_norm'], type=eval)
args.add_argument('--max_grad_norm', default=config['train']['max_grad_norm'], type=int)
args.add_argument('--teacher_forcing', default=False, type=bool)
#args.add_argument('--tf_decay_steps', default=2000, type=int, help='teacher forcing decay steps')
args.add_argument('--real_value', default=config['train']['real_value'], type=eval, help = 'use real value for loss calculation')
#test
args.add_argument('--mae_thresh', default=config['test']['mae_thresh'], type=eval)
args.add_argument('--mape_thresh', default=config['test']['mape_thresh'], type=float)
#log
args.add_argument('--log_dir', default='./', type=str)
args.add_argument('--log_step', default=config['log']['log_step'], type=int)
args.add_argument('--plot', default=config['log']['plot'], type=eval)
args = args.parse_args()
init_seed(args.seed)
if torch.cuda.is_available():
torch.cuda.set_device(int(args.device[5]))
else:
args.device = 'cpu'
adj, _ = get_adjacency_matrix(args.graph_path, args.num_nodes, direction=1)
args.adj_matrix = matrix_svd(adj, args.embed_dim)
#init model
model = Network(args)
model = model.to(args.device)
for p in model.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
else:
nn.init.uniform_(p)
print_model_parameters(model, only_num=False)
#load dataset
train_loader, val_loader, test_loader, scaler = get_dataloader(args,
normalizer=args.normalizer,
tod=args.tod, dow=False,
weather=False, single=False)
#init loss function, optimizer
if args.loss_func == 'mask_mae':
loss = masked_mae_loss(scaler=None, mask_value=0.0)
elif args.loss_func == 'mae':
loss = torch.nn.L1Loss().to(args.device)
elif args.loss_func == 'mse':
loss = torch.nn.MSELoss().to(args.device)
elif args.loss_func == 'mask_smoothL1Loss':
def mask_smoothL1Loss(preds, labels):
mask = torch.gt(labels, 0)
pred = torch.masked_select(preds, mask)
true = torch.masked_select(labels, mask)
SmoothL1Loss = torch.nn.SmoothL1Loss(beta=1).to(args.device)
return SmoothL1Loss(pred, true)
loss = mask_smoothL1Loss
elif args.loss_func == 'SmoothL1Loss':
loss = torch.nn.SmoothL1Loss(beta=1).to(args.device)
else:
raise ValueError
optimizer = torch.optim.Adam(params=model.parameters(), lr=args.lr_init, eps=1.0e-8,
weight_decay=0, amsgrad=False)
#learning rate decay
lr_scheduler = None
if args.lr_decay:
print('Applying learning rate decay.')
lr_decay_steps = [int(i) for i in list(args.lr_decay_step.split(','))]
lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer=optimizer,
milestones=lr_decay_steps,
gamma=args.lr_decay_rate)
#lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer=optimizer, T_max=64)
#config log path
current_time = datetime.now().strftime('%Y%m%d%H%M%S')
logdir_name = 'cheb_k'+ str(args.cheb_k) + '_' + 'embed_dim' + str(args.embed_dim) + '_' + 'lr' + str(args.lr_init) + '_loss_func' + args.loss_func + 'current_time' +current_time
current_dir = os.path.dirname(os.path.realpath(__file__))
log_dir = os.path.join(current_dir,'experiments', args.dataset, logdir_name)
args.log_dir = log_dir
def criterion(pred, true):
return loss(pred, true)
return 1 / 2 * loss(pred, true) + 1 / 2 * loss(pred[:, 11, ...], true[:, 11, ...])
#start training
trainer = Trainer(model, criterion, optimizer, train_loader, val_loader, test_loader, scaler,
args, lr_scheduler=lr_scheduler)
if args.mode == 'train':
trainer.train()
elif args.mode == 'test':
path = 'experiments/PEMSD4/cheb_k2_embed_dim200_lr0.003_loss_funcSmoothL1Losscurrent_time20220501084731/best_model.pth'
print("Load saved model")
trainer.test(model, trainer.args, test_loader, scaler, trainer.logger, path)
else:
raise ValueError