File size: 10,196 Bytes
31f6bcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Training loop for AspectBERT.

- Optimizer: AdamW (lr=2e-5, weight_decay=0.01)
- Scheduler: OneCycleLR (10% warmup, cosine decay)
- Epochs: 4, batch size 16 (CPU) / 32 (GPU) by default
- Metric: macro F1, best checkpoint saved by val F1
- Logs per-epoch history to results/training_history.json
- Final test evaluation: macro F1, accuracy, per-class F1, confusion matrix,
  plus a VADER baseline comparison.
"""

import argparse
import json
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

import torch
from torch.optim import AdamW
from torch.optim.lr_scheduler import OneCycleLR
from torch.utils.data import DataLoader, Dataset
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, f1_score
from transformers import DistilBertModel, DistilBertTokenizerFast

from constants import ID2LABEL, LABEL2ID, MAX_LENGTH, MODEL_NAME, format_input  # noqa: E402
from model import AspectBERT  # noqa: E402


class AspectDataset(Dataset):
    """Loads a jsonl file of {text, aspect, label, ...} rows."""

    def __init__(self, path, tokenizer, max_length=MAX_LENGTH):
        self.examples = []
        with open(path, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if line:
                    self.examples.append(json.loads(line))
        self.tokenizer = tokenizer
        self.max_length = max_length

    def __len__(self):
        return len(self.examples)

    def __getitem__(self, idx):
        row = self.examples[idx]
        text = format_input(row["text"], row["aspect"])
        enc = self.tokenizer(
            text,
            truncation=True,
            padding="max_length",
            max_length=self.max_length,
            return_tensors="pt",
        )
        item = {k: v.squeeze(0) for k, v in enc.items()}
        item["labels"] = torch.tensor(LABEL2ID[row["label"]], dtype=torch.long)
        return item


@torch.no_grad()
def evaluate(model, loader, device, criterion=None):
    model.eval()
    all_preds, all_labels = [], []
    total_loss = 0.0

    for batch in loader:
        input_ids = batch["input_ids"].to(device)
        attention_mask = batch["attention_mask"].to(device)
        labels = batch["labels"].to(device)

        logits = model(input_ids, attention_mask)
        if criterion is not None:
            loss = criterion(logits, labels)
            total_loss += loss.item() * labels.size(0)

        preds = torch.argmax(logits, dim=-1)
        all_preds.extend(preds.cpu().tolist())
        all_labels.extend(labels.cpu().tolist())

    avg_loss = total_loss / len(loader.dataset) if criterion is not None else None
    f1 = f1_score(all_labels, all_preds, average="macro", zero_division=0)
    acc = accuracy_score(all_labels, all_preds)
    return {"loss": avg_loss, "f1": f1, "accuracy": acc, "preds": all_preds, "labels": all_labels}


def save_checkpoint(model, tokenizer, output_dir):
    """Save backbone + tokenizer (HF format) and the classifier head separately."""
    os.makedirs(output_dir, exist_ok=True)
    model.distilbert.save_pretrained(output_dir)
    tokenizer.save_pretrained(output_dir)
    torch.save(model.classifier.state_dict(), os.path.join(output_dir, "classifier_head.pt"))


def load_checkpoint(output_dir, device):
    model = AspectBERT()
    model.distilbert = DistilBertModel.from_pretrained(output_dir)
    state_dict = torch.load(os.path.join(output_dir, "classifier_head.pt"), map_location="cpu")
    model.classifier.load_state_dict(state_dict)
    model.to(device)
    return model


def run_vader_baseline(test_file):
    from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

    analyzer = SentimentIntensityAnalyzer()
    labels, preds = [], []

    with open(test_file, "r", encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            row = json.loads(line)
            compound = analyzer.polarity_scores(row["text"])["compound"]
            if compound >= 0.05:
                pred = "positive"
            elif compound <= -0.05:
                pred = "negative"
            else:
                pred = "neutral"
            labels.append(LABEL2ID[row["label"]])
            preds.append(LABEL2ID[pred])

    return {
        "macro_f1": f1_score(labels, preds, average="macro", zero_division=0),
        "accuracy": accuracy_score(labels, preds),
    }


def run_test_evaluation(args, tokenizer, device, batch_size):
    print("\nLoading best checkpoint for test evaluation...")
    model = load_checkpoint(args.output_dir, device)

    test_ds = AspectDataset(args.test_file, tokenizer)
    test_loader = DataLoader(test_ds, batch_size=batch_size)

    metrics = evaluate(model, test_loader, device)

    labels_present = sorted(set(metrics["labels"]) | set(metrics["preds"]))
    per_class_f1 = f1_score(metrics["labels"], metrics["preds"], average=None,
                             labels=[0, 1, 2], zero_division=0)
    cm = confusion_matrix(metrics["labels"], metrics["preds"], labels=[0, 1, 2]).tolist()
    report = classification_report(
        metrics["labels"], metrics["preds"],
        labels=[0, 1, 2], target_names=[ID2LABEL[i] for i in range(3)],
        output_dict=True, zero_division=0,
    )

    results = {
        "macro_f1": metrics["f1"],
        "accuracy": metrics["accuracy"],
        "per_class_f1": {ID2LABEL[i]: float(per_class_f1[i]) for i in range(3)},
        "confusion_matrix": cm,
        "confusion_matrix_labels": [ID2LABEL[i] for i in range(3)],
        "classification_report": report,
    }

    try:
        results["vader_baseline"] = run_vader_baseline(args.test_file)
    except ImportError:
        print("vaderSentiment not installed; skipping VADER baseline comparison.")

    os.makedirs("results", exist_ok=True)
    with open("results/test_metrics.json", "w", encoding="utf-8") as f:
        json.dump(results, f, indent=2)

    print(f"\nTest macro F1: {results['macro_f1']:.4f}")
    print(f"Test accuracy: {results['accuracy']:.4f}")
    print(f"Per-class F1: {results['per_class_f1']}")
    if "vader_baseline" in results:
        print(f"VADER baseline macro F1: {results['vader_baseline']['macro_f1']:.4f} "
              f"(AspectBERT vs VADER on the same test set)")
    print("Saved detailed results to results/test_metrics.json")

    if labels_present:
        pass  # labels_present kept for potential debugging/inspection


def train(args):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    batch_size = args.batch_size or (32 if device.type == "cuda" else 16)
    print(f"Device: {device}, batch size: {batch_size}")

    tokenizer = DistilBertTokenizerFast.from_pretrained(MODEL_NAME)

    train_ds = AspectDataset(args.train_file, tokenizer)
    val_ds = AspectDataset(args.val_file, tokenizer)
    print(f"Train examples: {len(train_ds)}, Val examples: {len(val_ds)}")

    train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
    val_loader = DataLoader(val_ds, batch_size=batch_size)

    model = AspectBERT().to(device)

    trainable_params = [p for p in model.parameters() if p.requires_grad]
    optimizer = AdamW(trainable_params, lr=2e-5, weight_decay=0.01)

    total_steps = max(1, len(train_loader) * args.epochs)
    scheduler = OneCycleLR(
        optimizer,
        max_lr=2e-5,
        total_steps=total_steps,
        pct_start=0.1,
        anneal_strategy="cos",
    )

    criterion = torch.nn.CrossEntropyLoss()

    history = []
    best_f1 = -1.0
    os.makedirs(os.path.dirname(args.history_file) or ".", exist_ok=True)

    for epoch in range(1, args.epochs + 1):
        model.train()
        epoch_loss = 0.0

        for batch in train_loader:
            input_ids = batch["input_ids"].to(device)
            attention_mask = batch["attention_mask"].to(device)
            labels = batch["labels"].to(device)

            optimizer.zero_grad()
            logits = model(input_ids, attention_mask)
            loss = criterion(logits, labels)
            loss.backward()
            optimizer.step()
            scheduler.step()

            epoch_loss += loss.item() * labels.size(0)

        train_loss = epoch_loss / len(train_loader.dataset)
        val_metrics = evaluate(model, val_loader, device, criterion)

        print(f"Epoch {epoch}/{args.epochs} - "
              f"train_loss: {train_loss:.4f} - "
              f"val_loss: {val_metrics['loss']:.4f} - "
              f"val_f1: {val_metrics['f1']:.4f} - "
              f"val_acc: {val_metrics['accuracy']:.4f}")

        history.append({
            "epoch": epoch,
            "train_loss": train_loss,
            "val_loss": val_metrics["loss"],
            "val_f1": val_metrics["f1"],
            "val_accuracy": val_metrics["accuracy"],
            "lr": scheduler.get_last_lr()[0],
        })

        if val_metrics["f1"] > best_f1:
            best_f1 = val_metrics["f1"]
            save_checkpoint(model, tokenizer, args.output_dir)
            print(f"  -> New best val F1: {best_f1:.4f}, checkpoint saved to {args.output_dir}")

    with open(args.history_file, "w", encoding="utf-8") as f:
        json.dump(history, f, indent=2)
    print(f"\nSaved training history to {args.history_file}")

    if args.test_file and os.path.exists(args.test_file) and best_f1 >= 0:
        run_test_evaluation(args, tokenizer, device, batch_size)


def parse_args():
    parser = argparse.ArgumentParser(description="Train AspectBERT.")
    parser.add_argument("--train_file", default="data/train.jsonl")
    parser.add_argument("--val_file", default="data/val.jsonl")
    parser.add_argument("--test_file", default="data/test.jsonl")
    parser.add_argument("--output_dir", default="models/aspectbert")
    parser.add_argument("--history_file", default="results/training_history.json")
    parser.add_argument("--epochs", type=int, default=4)
    parser.add_argument("--batch_size", type=int, default=None,
                         help="Defaults to 32 on GPU, 16 on CPU.")
    return parser.parse_args()


if __name__ == "__main__":
    train(parse_args())