-
Notifications
You must be signed in to change notification settings - Fork 81
/
training.py
253 lines (203 loc) · 8.94 KB
/
training.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
import os
import time
import numpy as np
import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
class Trainer:
"""Trainer class for MTAD-GAT model.
:param model: MTAD-GAT model
:param optimizer: Optimizer used to minimize the loss function
:param window_size: Length of the input sequence
:param n_features: Number of input features
:param target_dims: dimension of input features to forecast and reconstruct
:param n_epochs: Number of iterations/epochs
:param batch_size: Number of windows in a single batch
:param init_lr: Initial learning rate of the module
:param forecast_criterion: Loss to be used for forecasting.
:param recon_criterion: Loss to be used for reconstruction.
:param boolean use_cuda: To be run on GPU or not
:param dload: Download directory where models are to be dumped
:param log_dir: Directory where SummaryWriter logs are written to
:param print_every: At what epoch interval to print losses
:param log_tensorboard: Whether to log loss++ to tensorboard
:param args_summary: Summary of args that will also be written to tensorboard if log_tensorboard
"""
def __init__(
self,
model,
optimizer,
window_size,
n_features,
target_dims=None,
n_epochs=200,
batch_size=256,
init_lr=0.001,
forecast_criterion=nn.MSELoss(),
recon_criterion=nn.MSELoss(),
use_cuda=True,
dload="",
log_dir="output/",
print_every=1,
log_tensorboard=True,
args_summary="",
):
self.model = model
self.optimizer = optimizer
self.window_size = window_size
self.n_features = n_features
self.target_dims = target_dims
self.n_epochs = n_epochs
self.batch_size = batch_size
self.init_lr = init_lr
self.forecast_criterion = forecast_criterion
self.recon_criterion = recon_criterion
self.device = "cuda" if use_cuda and torch.cuda.is_available() else "cpu"
self.dload = dload
self.log_dir = log_dir
self.print_every = print_every
self.log_tensorboard = log_tensorboard
self.losses = {
"train_total": [],
"train_forecast": [],
"train_recon": [],
"val_total": [],
"val_forecast": [],
"val_recon": [],
}
self.epoch_times = []
if self.device == "cuda":
self.model.cuda()
if self.log_tensorboard:
self.writer = SummaryWriter(f"{log_dir}")
self.writer.add_text("args_summary", args_summary)
def fit(self, train_loader, val_loader=None):
"""Train model for self.n_epochs.
Train and validation (if validation loader given) losses stored in self.losses
:param train_loader: train loader of input data
:param val_loader: validation loader of input data
"""
init_train_loss = self.evaluate(train_loader)
print(f"Init total train loss: {init_train_loss[2]:5f}")
if val_loader is not None:
init_val_loss = self.evaluate(val_loader)
print(f"Init total val loss: {init_val_loss[2]:.5f}")
print(f"Training model for {self.n_epochs} epochs..")
train_start = time.time()
for epoch in range(self.n_epochs):
epoch_start = time.time()
self.model.train()
forecast_b_losses = []
recon_b_losses = []
for x, y in train_loader:
x = x.to(self.device)
y = y.to(self.device)
self.optimizer.zero_grad()
preds, recons = self.model(x)
if self.target_dims is not None:
x = x[:, :, self.target_dims]
y = y[:, :, self.target_dims].squeeze(-1)
if preds.ndim == 3:
preds = preds.squeeze(1)
if y.ndim == 3:
y = y.squeeze(1)
forecast_loss = torch.sqrt(self.forecast_criterion(y, preds))
recon_loss = torch.sqrt(self.recon_criterion(x, recons))
loss = forecast_loss + recon_loss
loss.backward()
self.optimizer.step()
forecast_b_losses.append(forecast_loss.item())
recon_b_losses.append(recon_loss.item())
forecast_b_losses = np.array(forecast_b_losses)
recon_b_losses = np.array(recon_b_losses)
forecast_epoch_loss = np.sqrt((forecast_b_losses ** 2).mean())
recon_epoch_loss = np.sqrt((recon_b_losses ** 2).mean())
total_epoch_loss = forecast_epoch_loss + recon_epoch_loss
self.losses["train_forecast"].append(forecast_epoch_loss)
self.losses["train_recon"].append(recon_epoch_loss)
self.losses["train_total"].append(total_epoch_loss)
# Evaluate on validation set
forecast_val_loss, recon_val_loss, total_val_loss = "NA", "NA", "NA"
if val_loader is not None:
forecast_val_loss, recon_val_loss, total_val_loss = self.evaluate(val_loader)
self.losses["val_forecast"].append(forecast_val_loss)
self.losses["val_recon"].append(recon_val_loss)
self.losses["val_total"].append(total_val_loss)
if total_val_loss <= self.losses["val_total"][-1]:
self.save(f"model.pt")
if self.log_tensorboard:
self.write_loss(epoch)
epoch_time = time.time() - epoch_start
self.epoch_times.append(epoch_time)
if epoch % self.print_every == 0:
s = (
f"[Epoch {epoch + 1}] "
f"forecast_loss = {forecast_epoch_loss:.5f}, "
f"recon_loss = {recon_epoch_loss:.5f}, "
f"total_loss = {total_epoch_loss:.5f}"
)
if val_loader is not None:
s += (
f" ---- val_forecast_loss = {forecast_val_loss:.5f}, "
f"val_recon_loss = {recon_val_loss:.5f}, "
f"val_total_loss = {total_val_loss:.5f}"
)
s += f" [{epoch_time:.1f}s]"
print(s)
if val_loader is None:
self.save(f"model.pt")
train_time = int(time.time() - train_start)
if self.log_tensorboard:
self.writer.add_text("total_train_time", str(train_time))
print(f"-- Training done in {train_time}s.")
def evaluate(self, data_loader):
"""Evaluate model
:param data_loader: data loader of input data
:return forecasting loss, reconstruction loss, total loss
"""
self.model.eval()
forecast_losses = []
recon_losses = []
with torch.no_grad():
for x, y in data_loader:
x = x.to(self.device)
y = y.to(self.device)
preds, recons = self.model(x)
if self.target_dims is not None:
x = x[:, :, self.target_dims]
y = y[:, :, self.target_dims].squeeze(-1)
if preds.ndim == 3:
preds = preds.squeeze(1)
if y.ndim == 3:
y = y.squeeze(1)
forecast_loss = torch.sqrt(self.forecast_criterion(y, preds))
recon_loss = torch.sqrt(self.recon_criterion(x, recons))
forecast_losses.append(forecast_loss.item())
recon_losses.append(recon_loss.item())
forecast_losses = np.array(forecast_losses)
recon_losses = np.array(recon_losses)
forecast_loss = np.sqrt((forecast_losses ** 2).mean())
recon_loss = np.sqrt((recon_losses ** 2).mean())
total_loss = forecast_loss + recon_loss
return forecast_loss, recon_loss, total_loss
def save(self, file_name):
"""
Pickles the model parameters to be retrieved later
:param file_name: the filename to be saved as,`dload` serves as the download directory
"""
PATH = self.dload + "/" + file_name
if os.path.exists(self.dload):
pass
else:
os.mkdir(self.dload)
torch.save(self.model.state_dict(), PATH)
def load(self, PATH):
"""
Loads the model's parameters from the path mentioned
:param PATH: Should contain pickle file
"""
self.model.load_state_dict(torch.load(PATH, map_location=self.device))
def write_loss(self, epoch):
for key, value in self.losses.items():
if len(value) != 0:
self.writer.add_scalar(key, value[-1], epoch)