-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
284 lines (245 loc) · 9.21 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
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
import argparse
from pytorch_lightning import Trainer
from pytorch_lightning.loggers import WandbLogger
from pytorch_lightning.callbacks import ModelCheckpoint
import gc
import pandas as pd
import torch
from os import remove
import datasets
import architectures
from utils.losses import get_loss
from utils.plotting_functions import generate_prediction_html, generate_prediction_data
from architectures import AR, TGCN, LSTM, GRU, ARNet, ATGCN
def get_trained_model(args, dm):
artifact_dir = args.pretrained
# If we're loading an artifact from wandb, we need to download it first
if ":" in args.pretrained:
assert (
args.logger == True
), "If you're loading a model from wandb, you must use the wandb logger"
run = wandb.init(
job_type="predict",
)
artifact = run.use_artifact(artifact_dir, type="model")
artifact_dir = artifact.download() + "/model.ckpt"
if args.model_name == "TGCN":
model = getattr(architectures, temp_args.model_name).load_from_checkpoint(
artifact_dir,
edge_index=dm.edge_index,
edge_weight=dm.edge_weight,
loss_fn=get_loss(args.loss),
node_features=dm.X_train.shape[1],
)
else:
model = getattr(architectures, temp_args.model_name).load_from_checkpoint(
artifact_dir, loss_fn=get_loss(args.loss)
)
return model
def get_model(args, dm):
model = None
if args.pretrained:
return get_trained_model(args, dm)
loss_fn = get_loss(args.loss)
if args.model_name == "TGCN":
model = TGCN(
edge_index=dm.edge_index,
edge_weight=dm.edge_weight,
node_features=dm.X_train.shape[1],
loss_fn=loss_fn,
**vars(args),
)
elif args.model_name == "ATGCN":
model = ATGCN(
edge_index=dm.edge_index,
edge_weight=dm.edge_weight,
node_features=dm.X_train.shape[1],
loss_fn=loss_fn,
**vars(args),
)
elif args.model_name == "AR":
model = AR(
input_dim=args.sequence_length, output_dim=1, loss_fn=loss_fn, **vars(args)
)
elif args.model_name == "ARNet":
model = ARNet(input_dim=args.sequence_length, loss_fn=loss_fn, **vars(args))
elif args.model_name == "LSTM":
model = LSTM(input_dim=dm.input_dimensions, loss_fn=loss_fn, **vars(args))
elif args.model_name == "GRU":
model = GRU(input_dim=dm.input_dimensions, loss_fn=loss_fn, **vars(args))
else:
raise ValueError(f"{args.model_name} not implemented yet!")
return model
def validate_args(parser):
args = parser.parse_args()
train_start = pd.Timestamp(args.train_start)
train_end = pd.Timestamp(args.train_end)
val_end = pd.Timestamp(args.val_end)
test_end = pd.Timestamp(args.test_end)
if train_start >= train_end:
parser.error("Training start date must be before training end date")
if train_end >= test_end:
parser.error("Training end date must be before test end date")
if not test_end > val_end > train_end:
parser.error(
"Test end date must be after validation end date, which must be after training end date"
)
if args.loss == "PNLL" and args.censored:
parser.error("PNLL loss cannot be used with censoring")
if not args.censored and args.loss == 'CPNLL':
parser.error("CPNLL loss cannot be used when data is not censored")
if args.covariates and ("AR" in args.model_name):
parser.error("AR models cannot include covariates")
if not args.logger and args.save_predictions:
parser.error("If you're saving predictions, you must use a logger")
return args
if __name__ == "__main__":
print("Starting at: ", pd.Timestamp.now())
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
parser = Trainer.add_argparse_args(parser)
# Model and data related arguments
parser.add_argument("--mode", choices=("train", "test", "predict"), default="train")
parser.add_argument(
"--save_predictions",
help="Store predictions after training",
default=False,
action="store_true",
)
parser.add_argument(
"--model_name",
type=str,
help="The name of the model",
choices=("AR", "ARNet", "LSTM", "TGCN", "GRU", "ATGCN"),
required=True,
)
parser.add_argument(
"--dataloader",
type=str,
help="Name of dataloader",
choices=("EVChargersDatasetSpatial", "EVChargersDataset"),
required=True,
)
parser.add_argument(
"--pretrained", type=str, help="Path to pretrained model", default=None
)
parser.add_argument(
"--loss",
type=str,
help="Loss function to use",
default="PNLL",
choices=("MSE", "PNLL", "CPNLL", "CPNLL_TGCN"),
)
# Common dataset arguments
parser.add_argument("--cluster", type=str, help="Which cluster to fit model to")
parser.add_argument(
"--covariates",
help="Add covariates to the dataset",
default=False,
action="store_true",
)
parser.add_argument(
"--censored", action="store_true", default=False, help="Censor data at cap. tau"
)
parser.add_argument("--censor_level", default=1, help="Choose censorship level")
parser.add_argument(
"--censor_dynamic",
default=False,
help="Use dynamic censoring scheme",
action="store_true",
)
parser.add_argument(
"--forecast_lead",
type=int,
default=1,
help="How many time steps ahead to predict",
)
parser.add_argument(
"--forecast_horizon", type=int, default=1, help="How many time steps to predict"
)
parser.add_argument("--sequence_length", type=int, default=336)
parser.add_argument("--batch_size", type=int, default=32)
parser.add_argument("--train_start", type=str, required=True)
parser.add_argument("--train_end", type=str, required=True)
parser.add_argument("--test_end", type=str, required=True)
parser.add_argument("--val_end", type=str, required=False)
# Parse known arguments to get dataloader and model name
temp_args, _ = parser.parse_known_args()
parser = getattr(datasets, temp_args.dataloader).add_data_specific_arguments(parser)
parser = getattr(architectures, temp_args.model_name).add_model_specific_arguments(
parser
)
# Validate and parse arguments
args = validate_args(parser)
if "T00:00:00Z" in args.train_start:
args.train_start = args.train_start.replace("T00:00:00Z", "")
if "T00:00:00Z" in args.train_end:
args.train_end = args.train_end.replace("T00:00:00Z", "")
if "T00:00:00Z" in args.test_end:
args.test_end = args.test_end.replace("T00:00:00Z", "")
if "T00:00:00Z" in args.val_end:
args.val_end = args.val_end.replace("T00:00:00Z", "")
# Initialize datamodule
dm = getattr(datasets, temp_args.dataloader)(**vars(args))
# Print arguments
print(args)
# Setup logger
if args.logger:
import wandb
wandb_logger = WandbLogger(
project="Thesis", log_model="all", job_type=args.mode
)
run_name = wandb.run.id
else:
wandb_logger = None
run_name = "local"
# Initialize model
model = get_model(args, dm)
# Setup checkpoint
checkpoint_callback = ModelCheckpoint(monitor="val_loss", mode="min")
# Initialize trainer
trainer = Trainer.from_argparse_args(
args, logger=wandb_logger, callbacks=[checkpoint_callback]
)
# Train, test, and predict
predictions = []
if args.mode == "train":
trainer.fit(model, dm, ckpt_path=args.pretrained)
trainer.test(model, datamodule=dm, ckpt_path="best")
# Save local model
# trainer.save_checkpoint(f"trained_models/best_model_{run_name}.ckpt")
if args.save_predictions:
predictions = generate_prediction_data(dm, model)
for tup in predictions:
cluster, prediction = tup[0], tup[1]
if args.logger:
html_path = generate_prediction_html(prediction, run_name)
wandb.log(
{
f"test_predictions_{cluster}": wandb.Html(
open(html_path), inject=False
)
}
)
remove(html_path)
elif args.mode == "predict":
trainer.test(model, datamodule=dm, ckpt_path="best")
predictions = generate_prediction_data(dm, model)
# Log predictions
for tup in predictions:
cluster, prediction = tup[0], tup[1]
prediction.to_csv(
f"predictions/predictions_{args.model_name}_{cluster}_{run_name}_{args.censor_level}.csv"
)
html_path = generate_prediction_html(prediction, run_name)
wandb.log(
{f"test_predictions_{cluster}": wandb.Html(open(html_path), inject=False)}
)
remove(html_path)
if args.logger:
wandb.finish()
del model
del dm
del trainer
del predictions
gc.collect()
torch.cuda.empty_cache()