Spaces:
Sleeping
Sleeping
File size: 5,979 Bytes
fe030dd |
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 |
"""
Core Training Engine for the POLYMEROS project.
This module contains the primary logic for model training and validation,
encapsulated in a reusable `TrainingEngine` class. It is designed to be
called by different interfaces, such as the command-line script
(train_model.py) and the web UI's TrainingManager.
This approach ensures that the core training process is consistent,
maintainable, and follows the DRY (Don't Repeat Yourself) principle.
"""
import torch
import torch.nn as nn
import numpy as np
from torch.utils.data import TensorDataset, DataLoader
from sklearn.metrics import confusion_matrix, accuracy_score
from .training_types import (
TrainingConfig,
TrainingProgress,
get_cv_splitter,
augment_spectral_data,
)
from models.registry import build as build_model
class TrainingEngine:
"""Encapsulates the core model training and validation logic."""
def __init__(self, config: TrainingConfig):
"""
Initializes the TrainingEngine with a given configuration.
Args:
config (TrainingConfig): The configuration object for the training run.
"""
self.config = config
self.device = self._get_device()
def _get_device(self) -> torch.device:
"""Selects the appropriate compute device."""
if self.config.device == "auto":
return torch.device("cuda" if torch.cuda.is_available() else "cpu")
return torch.device(self.config.device)
def run(
self, X: np.ndarray, y: np.ndarray, progress_callback: callable = None
) -> dict:
"""
Executes the full cross-validation training and evaluation loop.
Args:
X (np.ndarray): Feature data.
y (np.ndarray): Label data.
progress_callback (callable, optional): A function to call with
progress updates. Defaults to None.
Returns:
dict: A dictionary containing the final results and metrics.
"""
cv_splitter = get_cv_splitter(self.config.cv_strategy, self.config.num_folds)
fold_accuracies = []
all_conf_matrices = []
final_model_state = None
for fold, (train_idx, val_idx) in enumerate(cv_splitter.split(X, y), 1):
if progress_callback:
progress_callback(
{
"type": "fold_start",
"fold": fold,
"total_folds": self.config.num_folds,
}
)
X_train, X_val = X[train_idx], X[val_idx]
y_train, y_val = y[train_idx], y[val_idx]
# Apply data augmentation if enabled
if self.config.enable_augmentation:
X_train, y_train = augment_spectral_data(
X_train, y_train, noise_level=self.config.noise_level
)
train_loader = DataLoader(
TensorDataset(
torch.tensor(X_train, dtype=torch.float32),
torch.tensor(y_train, dtype=torch.long),
),
batch_size=self.config.batch_size,
shuffle=True,
)
val_loader = DataLoader(
TensorDataset(
torch.tensor(X_val, dtype=torch.float32),
torch.tensor(y_val, dtype=torch.long),
)
)
model = build_model(self.config.model_name, self.config.target_len).to(
self.device
)
optimizer = torch.optim.Adam(
model.parameters(), lr=self.config.learning_rate
)
criterion = nn.CrossEntropyLoss()
for epoch in range(self.config.epochs):
model.train()
running_loss = 0.0
for inputs, labels in train_loader:
inputs = inputs.unsqueeze(1).to(self.device)
labels = labels.to(self.device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if progress_callback:
progress_callback(
{
"type": "epoch_end",
"fold": fold,
"epoch": epoch + 1,
"total_epochs": self.config.epochs,
"loss": running_loss / len(train_loader),
}
)
# Validation
model.eval()
all_true, all_pred = [], []
with torch.no_grad():
for inputs, labels in val_loader:
inputs = inputs.unsqueeze(1).to(self.device)
outputs = model(inputs)
_, predicted = torch.max(outputs, 1)
all_true.extend(labels.cpu().numpy())
all_pred.extend(predicted.cpu().numpy())
acc = accuracy_score(all_true, all_pred)
fold_accuracies.append(acc)
all_conf_matrices.append(confusion_matrix(all_true, all_pred).tolist())
final_model_state = model.state_dict()
if progress_callback:
progress_callback({"type": "fold_end", "fold": fold, "accuracy": acc})
return {
"fold_accuracies": fold_accuracies,
"confusion_matrices": all_conf_matrices,
"mean_accuracy": np.mean(fold_accuracies),
"std_accuracy": np.std(fold_accuracies),
"model_state_dict": final_model_state,
}
|