-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining_utils.py
More file actions
364 lines (316 loc) · 14.1 KB
/
Copy pathtraining_utils.py
File metadata and controls
364 lines (316 loc) · 14.1 KB
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
# training_utils.py
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim.lr_scheduler import StepLR, ReduceLROnPlateau
from torch_geometric.loader import DataLoader
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score, explained_variance_score
import numpy as np
import matplotlib.pyplot as plt
from models import MGModel
from config_loader import Config
import logging
from exceptions import TrainingError, TestingError, EarlyStoppingError, PlottingError
from typing import Tuple, List, Dict
logger = logging.getLogger(__name__)
class EarlyStopping:
"""
Early stopping to prevent overfitting.
"""
def __init__(self, patience: int = 7, verbose: bool = False, delta: float = 0.0, path: str = 'chk_learn.pt'):
"""
Initializes EarlyStopping.
Args:
patience (int): How many epochs to wait after last validation loss improvement.
verbose (bool): If True, prints a message for each validation loss improvement.
delta (float): Minimum change in the monitored quantity to qualify as an improvement.
path (str): Path for the checkpoint to save the best model.
"""
self.patience = patience
self.verbose = verbose
self.delta = delta
self.path = path
self.counter = 0
self.best_score: float = None
self.early_stop: bool = False
logger.debug("EarlyStopping initialized.")
def __call__(self, valid_loss: float, model: nn.Module) -> None:
"""
Checks if the validation loss has improved.
Args:
valid_loss (float): Current validation loss.
model (nn.Module): Model to save if the validation loss has improved.
Raises:
EarlyStoppingError: If an error occurs during early stopping.
"""
try:
if self.best_score is None:
self.best_score = valid_loss
self.save_model_state(valid_loss, model)
elif valid_loss > self.best_score + self.delta:
self.counter += 1
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = valid_loss
self.save_model_state(valid_loss, model)
self.counter = 0
except Exception as e:
logger.error(f"Error in EarlyStopping: {e}")
raise EarlyStoppingError(f"EarlyStopping failed: {e}")
def save_model_state(self, valid_loss: float, model: nn.Module) -> None:
"""
Saves the model state if the validation loss has improved.
Args:
valid_loss (float): Current validation loss.
model (nn.Module): Model to save.
"""
if self.verbose:
logger.info(f"Validation loss decreased ({self.best_score:.6f} --> {valid_loss:.6f}). Saving model...")
torch.save(model.state_dict(), self.path)
def calculate_metrics(targets: np.ndarray, predictions: np.ndarray) -> Dict[str, float]:
"""
Calculates evaluation metrics.
Args:
targets (np.ndarray): True target values.
predictions (np.ndarray): Predicted values.
Returns:
Dict[str, float]: Dictionary containing evaluation metrics.
"""
return {
"mae": mean_absolute_error(targets, predictions),
"mse": mean_squared_error(targets, predictions),
"r2": r2_score(targets, predictions),
"explained_variance": explained_variance_score(targets, predictions),
}
class TrainingLoop:
"""
Encapsulates the training loop logic.
"""
def __init__(self, model: nn.Module, criterion: nn.Module, optimizer: optim.Optimizer, step_lr: StepLR, device: torch.device, l1_lambda: float):
"""
Initializes TrainingLoop.
Args:
model (nn.Module): Model to train.
criterion (nn.Module): Loss function.
optimizer (optim.Optimizer): Optimizer.
step_lr (StepLR): Learning rate scheduler.
device (torch.device): Device to use for training.
l1_lambda (float): L1 regularization lambda.
"""
self.model = model
self.criterion = criterion
self.optimizer = optimizer
self.step_lr = step_lr
self.device = device
self.l1_lambda = l1_lambda
logger.debug("TrainingLoop initialized.")
def _process_epoch(self, data_loader: DataLoader, train: bool = False) -> Tuple[float, Dict[str, float], List[np.ndarray], List[np.ndarray]]:
"""
Processes a single epoch (train, validate, or test).
Args:
data_loader (DataLoader): DataLoader for the epoch.
train (bool): If True, performs training; otherwise, evaluation.
Returns:
Tuple[float, Dict[str, float], List[np.ndarray], List[np.ndarray]]: Average loss, metrics, all targets, and all predictions.
"""
if train:
self.model.train()
else:
self.model.eval()
total_loss = 0.0
num_graphs = 0
all_targets: List[np.ndarray] = []
all_predictions: List[np.ndarray] = []
with torch.set_grad_enabled(train):
for batch in data_loader:
batch = batch.to(self.device)
if train:
self.optimizer.zero_grad()
out, l1_reg = self.model(batch.x, batch.edge_index, batch.batch)
target = batch.y
loss = self.criterion(out, target)
if train:
loss += l1_reg * self.l1_lambda
loss.backward()
nn.utils.clip_grad_norm_(self.model.parameters(), max_norm=1.0)
self.optimizer.step()
total_loss += loss.item() * batch.num_graphs
num_graphs += batch.num_graphs
all_targets.append(target.detach().cpu().numpy())
all_predictions.append(out.detach().cpu().numpy())
avg_loss = total_loss / num_graphs
all_targets = np.concatenate(all_targets, axis=0)
all_predictions = np.concatenate(all_predictions, axis=0)
metrics = calculate_metrics(all_targets, all_predictions)
return avg_loss, metrics, all_targets, all_predictions
def validate_epoch(self, valid_loader: DataLoader) -> Tuple[float, Dict[str, float]]:
"""
Validates the model for one epoch.
Args:
valid_loader (DataLoader): DataLoader for validation data.
Returns:
Tuple[float, Dict[str, float]]: Average validation loss and metrics.
"""
avg_loss, metrics, _, _ = self._process_epoch(valid_loader)
logger.debug(f"Validation Epoch Loss: {avg_loss:.4f}, {metrics}")
return avg_loss, metrics
def test_epoch(self, test_loader: DataLoader, return_predictions: bool = False) -> Tuple[float, Dict[str, float], List[np.ndarray], List[np.ndarray]]:
"""
Tests the model for one epoch.
Args:
test_loader (DataLoader): DataLoader for test data.
return_predictions (bool): If True, returns predictions and targets.
Returns:
Tuple[float, Dict[str, float], List[np.ndarray], List[np.ndarray]]: Average test loss, metrics, all targets, and all predictions.
"""
avg_loss, metrics, all_targets, all_predictions = self._process_epoch(test_loader)
logger.info(f"Test Epoch Loss: {avg_loss:.4f}, {metrics}")
if return_predictions:
return avg_loss, metrics, all_targets, all_predictions
return avg_loss, metrics, [], []
class Trainer:
"""
Manages the training and validation process.
"""
def __init__(self, model: nn.Module, criterion: nn.Module, optimizer: optim.Optimizer, step_lr: StepLR, red_lr: ReduceLROnPlateau, early_stopping: EarlyStopping, config: Config, device: torch.device):
"""
Initializes Trainer.
Args:
model (nn.Module): Model to train.
criterion (nn.Module): Loss function.
optimizer (optim.Optimizer): Optimizer.
step_lr (StepLR): Step learning rate scheduler.
red_lr (ReduceLROnPlateau): Reduce learning rate on plateau scheduler.
early_stopping (EarlyStopping): Early stopping object.
config (Config): Configuration object.
device (torch.device): Device to use for training.
"""
self.model = model.to(device)
self.criterion = criterion
self.optimizer = optimizer
self.step_lr = step_lr
self.red_lr = red_lr
self.early_stopping = early_stopping
self.config = config
self.device = device
self.training_loop = TrainingLoop(self.model, self.criterion, self.optimizer, self.step_lr, self.device, self.config.model.l1_regularization_lambda)
logger.debug("Trainer initialized.")
def train_and_validate(self, train_loader: DataLoader, valid_loader: DataLoader) -> Tuple[List[float], List[float], List[float], List[float], List[float], List[float]]:
"""
Trains and validates the model.
Args:
train_loader (DataLoader): DataLoader for training data.
valid_loader (DataLoader): DataLoader for validation data.
Returns:
Tuple[List[float], List[float], List[float], List[float], List[float], List[float]]: Lists of training losses, validation losses, MAEs, MSEs, R2s, and explained variances.
Raises:
TrainingError: If an error occurs during training and validation.
"""
try:
train_losses: List[float] = []
valid_losses: List[float] = []
maes: List[float] = []
mses: List[float] = []
r2s: List[float] = []
explained_variances: List[float] = []
for epoch in range(self.config.model.early_stopping_patience * 2):
avg_loss, metrics, _, _ = self.training_loop._process_epoch(train_loader, train=True)
self.training_loop.step_lr.step()
train_loss = avg_loss
valid_loss, metrics = self.training_loop.validate_epoch(valid_loader)
train_losses.append(train_loss)
valid_losses.append(valid_loss)
maes.append(metrics['mae'])
mses.append(metrics['mse'])
r2s.append(metrics['r2'])
explained_variances.append(metrics['explained_variance'])
self.red_lr.step(valid_loss)
self.early_stopping(valid_loss, self.model)
if self.early_stopping.early_stop:
logger.info("Early stopping triggered.")
break
return train_losses, valid_losses, maes, mses, r2s, explained_variances
except Exception as e:
logger.error(f"Error during training and validation: {e}")
raise TrainingError(f"Training and validation failed: {e}")
def test_epoch(self, test_loader: DataLoader, return_predictions: bool = False) -> Tuple[float, Dict[str, float], List[np.ndarray], List[np.ndarray]]:
"""
Tests the model for one epoch.
Args:
test_loader (DataLoader): DataLoader for test data.
return_predictions (bool): If True, returns predictions and targets.
Returns:
Tuple[float, Dict[str, float], List[np.ndarray], List[np.ndarray]]: Average test loss, metrics, all targets, and all predictions.
"""
return self.training_loop.test_epoch(test_loader, return_predictions)
class Plot:
"""
Handles plotting of training and validation results.
"""
@staticmethod
def plot_losses(train_losses: List[float], valid_losses: List[float]) -> None:
"""
Plots training and validation losses.
Args:
train_losses (List[float]): List of training losses.
valid_losses (List[float]): List of validation losses.
Raises:
PlottingError: If an error occurs during plotting.
"""
try:
plt.figure(figsize=(10, 5))
plt.plot(train_losses, label='Training Loss')
plt.plot(valid_losses, label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training and Validation Losses')
plt.legend()
plt.show()
except Exception as e:
logger.error(f"Error plotting losses: {e}")
raise PlottingError(f"Failed to plot losses: {e}")
@staticmethod
def plot_metrics_vs_epoch(maes: List[float], mses: List[float], r2s: List[float], explained_variances: List[float]) -> None:
"""
Plots metrics against epochs.
Args:
maes (List[float]): List of MAEs.
mses (List[float]): List of MSEs.
r2s (List[float]): List of R2s.
explained_variances (List[float]): List of explained variances.
Raises:
PlottingError: If an error occurs during plotting.
"""
try:
epochs = range(1, len(maes) + 1)
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.plot(epochs, maes, label='MAE')
plt.xlabel('Epoch')
plt.ylabel('MAE')
plt.title('MAE vs. Epoch')
plt.legend()
plt.subplot(2, 2, 2)
plt.plot(epochs, mses, label='MSE')
plt.xlabel('Epoch')
plt.ylabel('MSE')
plt.title('MSE vs. Epoch')
plt.legend()
plt.subplot(2, 2, 3)
plt.plot(epochs, r2s, label='R2')
plt.xlabel('Epoch')
plt.ylabel('R2')
plt.title('R2 vs. Epoch')
plt.legend()
plt.subplot(2, 2, 4)
plt.plot(epochs, explained_variances, label='Explained Variance')
plt.xlabel('Epoch')
plt.ylabel('Explained Variance')
plt.title('Explained Variance vs. Epoch')
plt.legend()
plt.tight_layout()
plt.show()
except Exception as e:
logger.error(f"Error plotting metrics: {e}")
raise PlottingError(f"Failed to plot metrics: {e}")