-
Notifications
You must be signed in to change notification settings - Fork 15
/
run.py
204 lines (172 loc) · 8.03 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
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
import os
import torch
import logging
from probts.data import ProbTSDataModule
from probts.model.forecast_module import ProbTSForecastModule
from probts.callbacks import MemoryCallback, TimeCallback
from probts.utils import find_best_epoch
from lightning.pytorch.cli import LightningCLI
from lightning.pytorch.loggers import CSVLogger, TensorBoardLogger
from lightning.pytorch.callbacks import ModelCheckpoint
from probts.utils.save_utils import save_exp_summary, save_csv
MULTI_HOR_MODEL = ['ElasTST', 'Autoformer']
import warnings
warnings.filterwarnings('ignore')
torch.set_float32_matmul_precision('high')
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
class ProbTSCli(LightningCLI):
def add_arguments_to_parser(self, parser):
data_to_model_link_args = [
"scaler",
"train_pred_len_list",
]
data_to_forecaster_link_args = [
"target_dim",
"history_length",
"context_length",
"prediction_length",
"train_pred_len_list",
"lags_list",
"freq",
"time_feat_dim",
"global_mean",
"dataset"
]
for arg in data_to_model_link_args:
parser.link_arguments(f"data.data_manager.{arg}", f"model.{arg}", apply_on="instantiate")
for arg in data_to_forecaster_link_args:
parser.link_arguments(f"data.data_manager.{arg}", f"model.forecaster.init_args.{arg}", apply_on="instantiate")
def init_exp(self):
config_args = self.parser.parse_args()
if self.datamodule.data_manager.multi_hor:
assert self.model.forecaster.name in MULTI_HOR_MODEL, f"Only support multi-horizon setting for {MULTI_HOR_MODEL}"
self.tag = "_".join([
self.datamodule.data_manager.dataset,
self.model.forecaster.name,
'TrainCTX','-'.join([str(i) for i in self.datamodule.data_manager.train_ctx_len_list]),
'TrainPRED','-'.join([str(i) for i in self.datamodule.data_manager.train_pred_len_list]),
'ValCTX','-'.join([str(i) for i in self.datamodule.data_manager.val_ctx_len_list]),
'ValPRED','-'.join([str(i) for i in self.datamodule.data_manager.val_pred_len_list]),
'seed' + str(config_args.seed_everything)
])
else:
self.tag = "_".join([
self.datamodule.data_manager.dataset,
self.model.forecaster.name,
'CTX' + str(self.datamodule.data_manager.context_length),
'PRED' + str(self.datamodule.data_manager.prediction_length),
'seed' + str(config_args.seed_everything)
])
log.info(f"Root dir is {self.trainer.default_root_dir}, exp tag is {self.tag}")
if not os.path.exists(self.trainer.default_root_dir):
os.makedirs(self.trainer.default_root_dir)
self.save_dict = f'{self.trainer.default_root_dir}/{self.tag}'
if not os.path.exists(self.save_dict):
os.makedirs(self.save_dict)
if self.model.load_from_ckpt is not None:
# if the checkpoint file is not assigned, find the best epoch in the current folder
if '.ckpt' not in self.model.load_from_ckpt:
_, best_ckpt = find_best_epoch(self.model.load_from_ckpt)
print("find best ckpt ", best_ckpt)
self.model.load_from_ckpt = os.path.join(self.model.load_from_ckpt, best_ckpt)
log.info(f"Loading pre-trained checkpoint from {self.model.load_from_ckpt}")
self.model = ProbTSForecastModule.load_from_checkpoint(
self.model.load_from_ckpt,
learning_rate=config_args.model.learning_rate,
scaler=self.datamodule.data_manager.scaler,
context_length=self.datamodule.data_manager.context_length,
target_dim=self.datamodule.data_manager.target_dim,
freq=self.datamodule.data_manager.freq,
prediction_length=self.datamodule.data_manager.prediction_length,
train_pred_len_list=self.datamodule.data_manager.train_pred_len_list,
lags_list=self.datamodule.data_manager.lags_list,
time_feat_dim=self.datamodule.data_manager.time_feat_dim,
no_training=self.model.forecaster.no_training,
sampling_weight_scheme=self.model.sampling_weight_scheme,
)
# Set callbacks
self.memory_callback = MemoryCallback()
self.time_callback = TimeCallback()
callbacks = [
self.memory_callback,
self.time_callback
]
if not self.model.forecaster.no_training:
# not using reweighting scheme for loss
if self.model.sampling_weight_scheme in ['none', 'fix']:
monitor = 'val_CRPS'
else:
monitor = 'val_weighted_ND'
# Set callbacks
self.checkpoint_callback = ModelCheckpoint(
dirpath=f'{self.save_dict}/ckpt',
filename='{epoch}-{val_CRPS:.6f}',
every_n_epochs=1,
monitor=monitor,
save_top_k=-1,
save_last=True,
enable_version_counter=False
)
callbacks.append(self.checkpoint_callback)
self.set_callbacks(callbacks)
def set_callbacks(self, callbacks):
# Replace built-in callbacks with custom callbacks
custom_callbacks_name = [c.__class__.__name__ for c in callbacks]
for c in self.trainer.callbacks:
if c.__class__.__name__ in custom_callbacks_name:
self.trainer.callbacks.remove(c)
for c in callbacks:
self.trainer.callbacks.append(c)
for c in self.trainer.callbacks:
if c.__class__.__name__ == "ModelSummary":
self.model_summary_callback = c
def set_fit_mode(self):
self.trainer.logger = TensorBoardLogger(
save_dir=f'{self.save_dict}/logs',
name=self.tag,
version='fit'
)
def set_test_mode(self):
self.trainer.logger = CSVLogger(
save_dir=f'{self.save_dict}/logs',
name=self.tag,
version='test'
)
if not self.model.forecaster.no_training:
self.ckpt = self.checkpoint_callback.best_model_path
log.info(f"Loading best checkpoint from {self.ckpt}")
self.model = ProbTSForecastModule.load_from_checkpoint(
self.ckpt,
scaler=self.datamodule.data_manager.scaler,
context_length=self.datamodule.data_manager.context_length,
target_dim=self.datamodule.data_manager.target_dim,
freq=self.datamodule.data_manager.freq,
prediction_length=self.datamodule.data_manager.prediction_length,
lags_list=self.datamodule.data_manager.lags_list,
time_feat_dim=self.datamodule.data_manager.time_feat_dim,
sampling_weight_scheme=self.model.sampling_weight_scheme,
)
def run(self):
self.init_exp()
if not self.model.forecaster.no_training:
self.set_fit_mode()
self.trainer.fit(model=self.model, datamodule=self.datamodule)
inference=False
else:
inference=True
self.set_test_mode()
self.trainer.test(model=self.model, datamodule=self.datamodule)
save_exp_summary(self, inference=inference)
ctx_len = self.datamodule.data_manager.context_length
if self.datamodule.data_manager.multi_hor:
ctx_len = ctx_len[0]
save_csv(self.save_dict, self.model, ctx_len)
if __name__ == '__main__':
cli = ProbTSCli(
datamodule_class=ProbTSDataModule,
model_class=ProbTSForecastModule,
save_config_kwargs={"overwrite": True},
run=False
)
cli.run()