File size: 11,633 Bytes
edf3081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
models/deep_learning/trainer.py
────────────────────────────────
LSTMTrainer: training loop, validation loop, early stopping,
checkpointing, and training history for the LSTM sentiment classifier.

Implements BaseTrainer for integration with the comparative system.
"""

import json
import time
import logging
import pathlib

import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from sklearn.metrics import f1_score
import matplotlib
matplotlib.use("Agg")   # non-interactive backend for Windows
import matplotlib.pyplot as plt

from models.base import BaseTrainer
from models.deep_learning.config import (
    DEVICE, TRAIN_CONFIG,
    LSTM_CHECKPOINT_BEST, LSTM_CHECKPOINT_LAST,
    LSTM_TRAINING_HISTORY, LSTM_RESULTS_DIR,
)

logger = logging.getLogger(__name__)


class LSTMTrainer(BaseTrainer):
    """
    Training orchestrator for LSTMClassifier.

    Usage:
        trainer = LSTMTrainer(model)
        history = trainer.train(train_loader, val_loader)
        trainer.load_best_checkpoint()
        y_pred  = trainer.predict(test_loader)
        y_prob  = trainer.predict_proba(test_loader)
    """

    name: str = "LSTM"
    tier: str = "deep_learning"

    def __init__(
        self,
        model:       nn.Module,
        device:      torch.device    = DEVICE,
        lr:          float           = TRAIN_CONFIG["lr"],
        weight_decay:float           = TRAIN_CONFIG["weight_decay"],
        clip_norm:   float           = TRAIN_CONFIG["clip_grad_norm"],
        patience:    int             = TRAIN_CONFIG["patience"],
    ):
        self.model       = model.to(device)
        self.device      = device
        self.clip_norm   = clip_norm
        self.patience    = patience
        self._trained    = False

        self.optimizer  = torch.optim.Adam(
            model.parameters(), lr=lr, weight_decay=weight_decay
        )
        self.scheduler  = torch.optim.lr_scheduler.ReduceLROnPlateau(
            self.optimizer, mode="max", patience=1, factor=0.5
        )
        self.criterion  = nn.CrossEntropyLoss()

    # ── Main training loop ────────────────────────────────────────────────────

    def train(
        self,
        train_loader: DataLoader,
        val_loader:   DataLoader,
        num_epochs:   int = TRAIN_CONFIG["num_epochs"],
    ) -> dict:
        """
        Train with early stopping and checkpoint saving.

        Returns:
            history dict with per-epoch train/val loss and metrics.
        """
        logger.info("[LSTM] Starting training | device=%s  epochs=%d  patience=%d",
                    self.device, num_epochs, self.patience)

        history = {
            "train_loss": [], "train_acc": [],
            "val_loss":   [], "val_acc":   [], "val_f1": [],
        }
        best_val_f1    = 0.0
        no_improve     = 0
        total_train_time = 0.0

        for epoch in range(1, num_epochs + 1):
            t0 = time.perf_counter()

            tr_loss, tr_acc             = self._train_epoch(train_loader)
            val_loss, val_acc, val_f1   = self._validate_epoch(val_loader)
            self.scheduler.step(val_f1)

            epoch_time = time.perf_counter() - t0
            total_train_time += epoch_time

            history["train_loss"].append(round(tr_loss,  4))
            history["train_acc"].append(round(tr_acc,    4))
            history["val_loss"].append(round(val_loss,   4))
            history["val_acc"].append(round(val_acc,     4))
            history["val_f1"].append(round(val_f1,       4))

            improved = val_f1 > best_val_f1
            if improved:
                best_val_f1 = val_f1
                no_improve  = 0
                self._save_checkpoint(LSTM_CHECKPOINT_BEST, epoch, val_f1)
                tag = "  [NEW BEST]"
            else:
                no_improve += 1
                tag = ""

            logger.info(
                "Epoch %2d/%d | train_loss=%.4f acc=%.4f | "
                "val_loss=%.4f acc=%.4f f1=%.4f | %.1fs%s",
                epoch, num_epochs, tr_loss, tr_acc,
                val_loss, val_acc, val_f1, epoch_time, tag,
            )

            self._save_checkpoint(LSTM_CHECKPOINT_LAST, epoch, val_f1)

            if no_improve >= self.patience:
                logger.info("[LSTM] Early stopping triggered after %d epochs with no improvement.", epoch)
                break

        history["best_val_f1"]     = round(best_val_f1, 4)
        history["total_train_time_s"] = round(total_train_time, 2)
        self._trained = True

        self._save_history(history)
        self._plot_curves(history)
        return history

    # ── Epoch helpers ─────────────────────────────────────────────────────────

    def _train_epoch(self, loader: DataLoader) -> tuple[float, float]:
        self.model.train()
        total_loss, correct, total = 0.0, 0, 0

        for x, y in loader:
            x, y = x.to(self.device), y.to(self.device)
            self.optimizer.zero_grad()
            logits = self.model(x)
            loss   = self.criterion(logits, y)
            loss.backward()
            nn.utils.clip_grad_norm_(self.model.parameters(), self.clip_norm)
            self.optimizer.step()

            total_loss += loss.item() * y.size(0)
            correct    += (logits.argmax(1) == y).sum().item()
            total      += y.size(0)

        return total_loss / total, correct / total

    @torch.no_grad()
    def _validate_epoch(self, loader: DataLoader) -> tuple[float, float, float]:
        self.model.eval()
        total_loss, all_preds, all_labels = 0.0, [], []

        for x, y in loader:
            x, y   = x.to(self.device), y.to(self.device)
            logits  = self.model(x)
            loss    = self.criterion(logits, y)
            total_loss += loss.item() * y.size(0)
            all_preds  += logits.argmax(1).cpu().tolist()
            all_labels += y.cpu().tolist()

        n   = len(all_labels)
        acc = sum(p == l for p, l in zip(all_preds, all_labels)) / n
        f1  = f1_score(all_labels, all_preds, average="macro", zero_division=0)
        return total_loss / n, acc, f1

    # ── BaseTrainer interface ─────────────────────────────────────────────────

    def predict(self, loader: DataLoader) -> np.ndarray:
        """Run inference on a DataLoader; return integer predictions."""
        self._assert_trained()
        return self._run_inference(loader)[0]

    def predict_proba(self, loader: DataLoader) -> np.ndarray:
        """
        Run inference on a DataLoader; return (N, 2) probability array.
        Column 1 = P(positive).
        """
        self._assert_trained()
        _, proba = self._run_inference(loader)
        return proba

    @torch.no_grad()
    def _run_inference(self, loader: DataLoader) -> tuple[np.ndarray, np.ndarray]:
        """Returns (y_pred, proba) arrays."""
        self.model.eval()
        all_preds, all_proba = [], []

        for x, *_ in loader:   # labels may or may not be present
            x      = x.to(self.device)
            logits = self.model(x)
            proba  = torch.softmax(logits, dim=1)
            all_preds  += logits.argmax(1).cpu().tolist()
            all_proba.append(proba.cpu().numpy())

        return (
            np.array(all_preds, dtype=np.int32),
            np.vstack(all_proba),
        )

    # ── Checkpointing ─────────────────────────────────────────────────────────

    def _save_checkpoint(
        self,
        path:    pathlib.Path,
        epoch:   int,
        val_f1:  float,
    ) -> None:
        path.parent.mkdir(parents=True, exist_ok=True)
        torch.save(
            {
                "epoch":               epoch,
                "val_f1":              val_f1,
                "model_state_dict":    self.model.state_dict(),
                "optimizer_state_dict":self.optimizer.state_dict(),
            },
            path,
        )

    def load_best_checkpoint(self) -> dict:
        """Load the best checkpoint back into the model (call before test evaluation)."""
        if not LSTM_CHECKPOINT_BEST.exists():
            raise FileNotFoundError(f"No checkpoint found at {LSTM_CHECKPOINT_BEST}")
        ckpt = torch.load(LSTM_CHECKPOINT_BEST, map_location=self.device, weights_only=False)
        self.model.load_state_dict(ckpt["model_state_dict"])
        self.optimizer.load_state_dict(ckpt["optimizer_state_dict"])
        logger.info("[LSTM] Best checkpoint loaded (epoch=%d  val_f1=%.4f)",
                    ckpt["epoch"], ckpt["val_f1"])
        self._trained = True
        return ckpt

    def save(self, directory: pathlib.Path | None = None) -> pathlib.Path:
        """Alias: save the best checkpoint (satisfies BaseTrainer interface)."""
        return LSTM_CHECKPOINT_BEST

    @classmethod
    def load(cls, directory: pathlib.Path | None = None) -> "LSTMTrainer":
        """Not used directly β€” use LSTMPredictor.from_saved() for inference."""
        raise NotImplementedError("Use LSTMPredictor.from_saved() for inference loading.")

    # ── History and plots ─────────────────────────────────────────────────────

    def _save_history(self, history: dict) -> None:
        LSTM_TRAINING_HISTORY.parent.mkdir(parents=True, exist_ok=True)
        with open(LSTM_TRAINING_HISTORY, "w") as f:
            json.dump(history, f, indent=2)
        logger.info("[LSTM] Training history saved -> %s", LSTM_TRAINING_HISTORY)

    def _plot_curves(self, history: dict) -> None:
        epochs = range(1, len(history["train_loss"]) + 1)
        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(11, 4))
        fig.suptitle("LSTM Training Curves β€” amazon_polarity", weight="bold")

        ax1.plot(epochs, history["train_loss"], label="Train Loss", marker="o", markersize=3)
        ax1.plot(epochs, history["val_loss"],   label="Val Loss",   marker="o", markersize=3)
        ax1.set_xlabel("Epoch"); ax1.set_ylabel("Loss")
        ax1.set_title("Loss"); ax1.legend(); ax1.grid(alpha=0.3)

        ax2.plot(epochs, history["val_acc"], label="Val Accuracy", marker="o", markersize=3)
        ax2.plot(epochs, history["val_f1"],  label="Val F1 Macro", marker="s", markersize=3)
        ax2.set_xlabel("Epoch"); ax2.set_ylabel("Score")
        ax2.set_title("Metrics"); ax2.legend(); ax2.grid(alpha=0.3)

        plt.tight_layout()
        out = LSTM_RESULTS_DIR / "training_curves.png"
        out.parent.mkdir(parents=True, exist_ok=True)
        fig.savefig(out, dpi=150)
        plt.close(fig)
        logger.info("[LSTM] Training curves saved -> %s", out)

    # ── Internal ──────────────────────────────────────────────────────────────

    def _assert_trained(self) -> None:
        if not self._trained:
            raise RuntimeError(
                "LSTMTrainer: model is not trained. "
                "Call train() or load_best_checkpoint() first."
            )