-
Notifications
You must be signed in to change notification settings - Fork 12
/
run.py
175 lines (149 loc) · 6.66 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
import os
import json
import torch
import logging
from probts.data import ProbTSDataModule
from probts.model.forecast_module import ProbTSForecastModule
from probts.callbacks import MemoryCallback, TimeCallback
from lightning.pytorch.cli import LightningCLI
from lightning.pytorch.loggers import CSVLogger, TensorBoardLogger
from lightning.pytorch.callbacks import ModelCheckpoint
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"
]
data_to_forecaster_link_args = [
"target_dim",
"history_length",
"context_length",
"prediction_length",
"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()
self.tag = "_".join([
self.datamodule.data_manager.dataset,
str(self.datamodule.data_manager.context_length),
str(self.datamodule.data_manager.prediction_length),
self.model.forecaster.name,
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)
if self.model.load_from_ckpt is not None:
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,
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,
)
# Set callbacks
self.checkpoint_callback = ModelCheckpoint(
dirpath=f'{self.trainer.default_root_dir}/ckpt/{self.tag}',
filename='{epoch}-{val_CRPS:.3f}',
every_n_epochs=1,
monitor='val_CRPS',
save_top_k=-1,
save_last=True,
enable_version_counter=False
)
self.memory_callback = MemoryCallback()
self.time_callback = TimeCallback()
callbacks = [
self.checkpoint_callback,
self.memory_callback,
self.time_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.trainer.default_root_dir}/logs',
name=self.tag,
version='fit'
)
def set_test_mode(self):
self.trainer.logger = CSVLogger(
save_dir=f'{self.trainer.default_root_dir}/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,
)
def save_exp_summary(self):
exp_summary = {}
model_summary = self.model_summary_callback._summary(self.trainer, self.model)
exp_summary['total_parameters'] = model_summary.total_parameters
exp_summary['trainable_parameters'] = model_summary.trainable_parameters
exp_summary['model_size'] = model_summary.model_size
memory_summary = self.memory_callback.memory_summary
exp_summary['memory_summary'] = memory_summary
time_summary = self.time_callback.time_summary
exp_summary['time_summary'] = time_summary
for batch_key, batch_time in time_summary.items():
if len(batch_time) > 0:
exp_summary[f'mean_{batch_key}'] = sum(batch_time) / len(batch_time)
summary_save_path = f"{self.trainer.default_root_dir}/logs/{self.tag}/summary.json"
with open(summary_save_path, 'w') as f:
json.dump(exp_summary, f, indent=4)
log.info(f"Summary saved to {summary_save_path}")
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)
self.set_test_mode()
self.trainer.test(model=self.model, datamodule=self.datamodule)
if not self.model.forecaster.no_training:
self.save_exp_summary()
if __name__ == '__main__':
cli = ProbTSCli(
datamodule_class=ProbTSDataModule,
model_class=ProbTSForecastModule,
save_config_kwargs={"overwrite": True},
run=False
)
cli.run()