-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
377 lines (314 loc) · 12.8 KB
/
train.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 4 20:42:47 2023.
@author: Panagiotis Doupidis
"""
import torch
from torch import nn
from tqdm import tqdm
from typing import Tuple, Type, Union
import numpy as np
import os.path
import json
from datetime import datetime
from sklearn.metrics import mean_squared_error as MSE
from sklearn.metrics import mean_absolute_error as MAE
from utils import smape as sMAPE
from functools import singledispatchmethod
from model import FusedRTRNN
class RTRNNTrainer:
"""
A class for training and evaluating a RTRNN model.
Parameters
----------
model : nn.Module
The fused RTRNN model to train and evaluate.
criterion : callable
The loss function to use during training.
optimizer : torch.optim.Optimizer
The optimizer to use during training.
patience : int
The patience for early stopping.
threshold : float
The threshold for early stopping.
num_epochs : int
The number of epochs to train the model for.
"""
def __init__(self, model: FusedRTRNN, criterion: nn.Module, optimizer: torch.optim.Optimizer,
patience: int, threshold: float, num_epochs: int):
self.model = model
self.criterion = criterion
self.optimizer = optimizer
self.patience = patience
self.threshold = threshold
self.num_epochs = num_epochs
self.patience_counter = 0
self.prev_loss = float('inf')
self.fitted = False
self.es = EarlyStopping(patience, threshold)
def early_stopping(self, loss: float) -> bool:
"""
Check if training should be stopped early based on the current loss.
Parameters
----------
loss : float
The current loss.
Returns
-------
bool
A boolean indicating whether to stop training early.
"""
if abs(self.prev_loss - loss) < self.threshold or self.prev_loss < loss:
self.patience_counter += 1
if self.patience_counter == self.patience:
return True
else:
self.patience_counter = 0
return False
@singledispatchmethod
def train(self, *args, **kwargs):
"""Generic train method."""
raise NotImplementedError("Invalid number of arguments provided")
def _(self, x_train: torch.Tensor, y_train: torch.Tensor) -> None:
"""
Train the TRNN model on the provided training data.
Parameters
----------
x_train : torch.Tensor
The training data inputs.
y_train : torch.Tensor
The training data targets.
"""
# Set the model to training mode
self.model.train()
for epoch in range(self.num_epochs):
outputs = self.model(x_train)
loss = self.criterion(outputs.squeeze(), y_train)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
if (epoch+1) % 10 == 0:
print(
f'Epoch: {epoch+1:4d}/{self.num_epochs}, Loss: {loss.item():.4f}')
stop = self.early_stopping(loss.item())
if stop:
self.fitted = True
print(
f'Early stopping at epoch {epoch+1}. Loss has not decreased by more than {self.threshold} for {self.patience} consecutive epochs.')
break
self.prev_loss = loss.item()
self.fitted = True
@train.register
def _(self, x_train: torch.Tensor, y_train: torch.Tensor,
x_val: torch.Tensor, y_val: torch.Tensor, pretrain_epochs: Union[int, None]) -> None:
"""
Train the TRNN model on the provided training data.
Parameters
----------
x_train : torch.Tensor
The training data inputs.
y_train : torch.Tensor
The training data targets.
x_val : torch.Tensor
The validation data inputs.
y_val : torch.Tensor
The validation data targets.
pretrain_epochs : int
The number of pre-training epochs.
"""
# Set the model to training mode
self.model.train()
if pretrain_epochs is not None:
# Pre-training stage
with tqdm(total=pretrain_epochs, desc='Pre-training', unit='epoch',
ascii=True, bar_format='{desc}: {n_fmt}/{total_fmt} {postfix}',
initial=0) as pbar:
for epoch in range(pretrain_epochs):
outputs = self.model(x_train)
loss = self.criterion(outputs.squeeze(), y_train)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
pbar.update(1)
pbar.set_postfix_str('done')
# Main training loop
for epoch in range(self.num_epochs):
outputs = self.model(x_train)
loss = self.criterion(outputs.squeeze(), y_train)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
# Evaluate the model on the validation set
with torch.no_grad():
val_outputs = self.model(x_val)
val_loss = self.criterion(val_outputs.squeeze(), y_val)
if (epoch + 1) % 10 == 0:
print(
f'Epoch: {epoch+1:4d}/{self.num_epochs}, Loss: {loss.item():.4f}, Val. Loss: {val_loss.item():.4f}')
if self.es(val_loss):
self.fitted = True
print(
f'Early stopping at epoch {epoch+1}. Validation loss has not decreased by more than {self.threshold} for {self.patience} consecutive epochs.')
break
self.fitted = True
def evaluate(self, x_test: torch.Tensor, y_test: torch.Tensor) -> Tuple[
torch.Tensor, torch.Tensor]:
"""
Evaluate the fused RTRNN model on the provided testing data and
calculate evaluation metrics.
Parameters
----------
x_test : torch.Tensor
The testing data inputs.
y_test : torch.Tensor
The testing data targets.
Returns
-------
Tuple[torch.Tensor, torch.Tensor]
A tuple containing the model's predictions on the testing data and
the testing data targets as numpy arrays.
"""
# Set the model to evaluation mode
self.model.eval()
with torch.no_grad():
predictions = self.model(x_test)
predictions = predictions.detach().cpu().numpy()
y_test_np = y_test.detach().cpu().numpy()
# Calculate evaluation metrics
mse = MSE(y_test_np, predictions)
rmse = MSE(y_test_np, predictions, squared=False)
mae = MAE(y_test_np, predictions)
smape = sMAPE(y_test_np, predictions)
print('=' * 35)
print(
f'Performance metrics for an evaluation period of {len(y_test)} timesteps')
print('=' * 35)
print(f'MSE:{mse:10.5f}')
print(f'RMSE:{rmse:9.5f}')
print(f'MAE:{mae:10.5f}')
print(f'sMAPE:{smape:6.3f} %')
return predictions, y_test_np
@torch.no_grad()
def predict_single(self, x_test: torch.Tensor) -> float:
"""Get single one-step-ahead prediction."""
return self.model(x_test).detach().item()
def save_model(self, dirname, overwrite: bool = False) -> None:
"""
Save the model to a file.
Parameters
----------
dirname : str
The directory to save the model and its parameters.
overwrite : bool
Whether to overwrite the file if it already exists.
"""
# Check if the model is fitted before saving it
if not self.fitted:
raise ValueError("The model must be fitted before it can be saved")
# Append the current timestamp to the directory name
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
dirname = f'{dirname}_{timestamp}'
# Create the directory
os.makedirs(dirname, exist_ok=overwrite)
# Check if the file already exists
filename = os.path.join(dirname, 'model.pt')
if os.path.exists(filename) and not overwrite:
raise FileExistsError(
f"The file {filename} already exists. Set overwrite=True to overwrite it.")
# Save the model state dictionary to the specified file
torch.save(self.model.state_dict(), filename)
# Save a JSON file containing the path to the saved model file
with open(os.path.join(dirname, 'model_params.json'), 'w') as f:
json.dump({'model_state_dict': os.path.abspath(filename),
'model_params': self.model.params,
'lr': self.optimizer.param_groups[0]['lr'],
'patience': self.patience,
'threshold': self.threshold,
'num_epochs': self.num_epochs}, f,
indent=2, sort_keys=True)
print('Model and params saved to', dirname)
@classmethod
def from_file(cls: Type['RTRNNTrainer'], json_file: str) -> 'RTRNNTrainer':
"""
Load a RTRNNTrainer instance from a JSON file.
The JSON file should have the following structure:
{
"model_params": {...},
"model_state_dict": "path/to/state_dict/file",
"lr": ...,
"patience": ...,
"threshold": ...,
"num_epochs": ...
}
where `model_params` is a dictionary containing the parameters for
initializing the RTRNN model, `model_state_dict` is the path to a file
containing the saved state dictionary of the model, `lr` is the learning
rate for the optimizer, `patience` is the patience for early stopping,
`threshold` is the threshold for early stopping, and `num_epochs`
is the number of epochs to train the model for.
Parameters
----------
json_file : str
Path to the JSON file containing the model parameters.
Returns
-------
RTRNNTrainer
Instance of the RTRNNTrainer class.
"""
with open(json_file, 'r') as f:
params = json.load(f)
# Load the model from the saved state dict
model = FusedRTRNN(**params['model_params'])
model.load_state_dict(torch.load(params['model_state_dict'],
map_location=torch.device(
params['model_params']['device']))
)
# Create the criterion, optimizer, and other trainer parameters
criterion = nn.MSELoss()
optimizer = torch.optim.AdamW(model.parameters(), lr=params['lr'],
foreach=True)
patience = params['patience']
threshold = params['threshold']
num_epochs = params['num_epochs']
# Create and return the trainer instance using the class constructor
return cls(model, criterion, optimizer, patience, threshold, num_epochs)
class EarlyStopping:
"""Early stopping w/ validation loss."""
def __init__(self, patience: int, delta: float):
"""
Initialize the early stopping object.
Parameters
----------
patience : int
The number of epochs to wait before stopping training if the
validation loss does not improve.
delta : float
The minimum change in validation loss required to consider it as
an improvement.
"""
self.patience = patience
self.delta = delta
self.best_loss = float('inf')
self.counter = 0
def __call__(self, val_loss: float) -> bool:
"""
Check if training should be stopped early based on current val. loss.
Parameters
----------
val_loss : float
The current validation loss.
Returns
-------
bool
A boolean indicating whether to stop training early.
"""
if val_loss < self.best_loss - self.delta:
self.best_loss = val_loss
self.counter = 0
return False
else:
self.counter += 1
if self.counter >= self.patience:
return True
else:
return False