File size: 4,648 Bytes
cde10f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json

import joblib
import numpy as np
import pandas as pd
import trackio
from sentence_transformers import SentenceTransformer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
    accuracy_score,
    confusion_matrix,
    f1_score,
    precision_score,
    recall_score,
)
from train import ARTIFACT_DIR, DATA_DIR

BASE_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
OUTPUT_DIR = ARTIFACT_DIR.parent / "protocol-guardian-minilm-linear"


def load(name: str) -> tuple[list[str], np.ndarray]:
    frame = pd.read_parquet(DATA_DIR / f"{name}.parquet")
    return (
        frame["text"].tolist(),
        frame["label"].to_numpy(dtype=np.int64, copy=True),
    )


def metrics(labels: np.ndarray, predictions: np.ndarray) -> dict:
    return {
        "accuracy": float(accuracy_score(labels, predictions)),
        "precision": float(precision_score(labels, predictions, zero_division=0)),
        "recall": float(recall_score(labels, predictions, zero_division=0)),
        "f1": float(f1_score(labels, predictions, zero_division=0)),
        "confusion_matrix": confusion_matrix(labels, predictions).tolist(),
        "examples": len(labels),
    }


def main() -> None:
    train_text, train_labels = load("train")
    validation_text, validation_labels = load("validation")
    test_text, test_labels = load("test")
    encoder = SentenceTransformer(BASE_MODEL, device="cpu")
    embeddings = {
        "train": encoder.encode(
            train_text,
            batch_size=128,
            normalize_embeddings=True,
            show_progress_bar=True,
        ),
        "validation": encoder.encode(
            validation_text,
            batch_size=128,
            normalize_embeddings=True,
            show_progress_bar=True,
        ),
        "test": encoder.encode(
            test_text,
            batch_size=128,
            normalize_embeddings=True,
            show_progress_bar=True,
        ),
    }
    trackio.init(
        project="protocol-guardian",
        name="minilm-embedding-linear-v1",
        config={
            "base_model": BASE_MODEL,
            "embedding_dimensions": embeddings["train"].shape[1],
            "train_examples": len(train_labels),
            "validation_template_family": "held-out from train",
            "test_template_family": "held-out from train and validation",
        },
    )
    candidates = []
    for regularization in [0.03, 0.1, 0.3, 1.0, 3.0, 10.0]:
        classifier = LogisticRegression(
            C=regularization,
            class_weight="balanced",
            max_iter=2000,
            random_state=2026,
        )
        classifier.fit(embeddings["train"], train_labels)
        predictions = classifier.predict(embeddings["validation"])
        score = f1_score(validation_labels, predictions)
        candidates.append((score, regularization, classifier))
        trackio.log(
            {
                "regularization_c": regularization,
                "validation_f1": score,
                "validation_accuracy": accuracy_score(
                    validation_labels,
                    predictions,
                ),
            }
        )
    _, best_c, classifier = max(candidates, key=lambda item: item[0])
    validation_predictions = classifier.predict(embeddings["validation"])
    test_predictions = classifier.predict(embeddings["test"])
    results = {
        "model": "Protocol Guardian MiniLM Linear",
        "base_model": BASE_MODEL,
        "embedding_dimensions": int(embeddings["train"].shape[1]),
        "linear_head_parameters": int(classifier.coef_.size + classifier.intercept_.size),
        "selected_c": best_c,
        "validation": metrics(validation_labels, validation_predictions),
        "test": metrics(test_labels, test_predictions),
        "test_split": "1200 examples from a third, unseen template family",
    }
    trackio.log(
        {
            "test_accuracy": results["test"]["accuracy"],
            "test_f1": results["test"]["f1"],
            "test_recall": results["test"]["recall"],
        }
    )
    trackio.finish()
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    joblib.dump(
        {
            "classifier": classifier,
            "base_model": BASE_MODEL,
            "labels": {0: "ROUTINE", 1: "HAZARDOUS"},
        },
        OUTPUT_DIR / "classifier.joblib",
        compress=3,
    )
    (OUTPUT_DIR / "training_summary.json").write_text(
        json.dumps(results, indent=2),
        encoding="utf-8",
    )
    print(json.dumps(results, indent=2))


if __name__ == "__main__":
    main()