File size: 10,606 Bytes
ad57885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Evaluate trained complexity classifier."""

import json
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import torch
from sklearn.metrics import (
    accuracy_score,
    classification_report,
    confusion_matrix,
    f1_score,
    precision_recall_curve,
    roc_auc_score,
    roc_curve,
)
from transformers import AutoModelForSequenceClassification, AutoTokenizer

# Add parent directory to path for imports
import sys
sys.path.insert(0, str(Path(__file__).parent.parent.parent))

from ml.data.load_dataset import load_arc_dataset, load_easy2hard_bench


def evaluate_model(
    model_dir: str = "ml/artifacts/complexity-classifier",
    dataset_type: str = "arc",
    max_samples: int | None = None,
    output_dir: str | None = None,
    seed: int = 42,
) -> dict:
    """
    Evaluate a trained complexity classifier.

    Args:
        model_dir: Directory containing trained model
        dataset_type: "easy2hard" or "arc"
        max_samples: Maximum samples to evaluate
        output_dir: Directory to save evaluation results (defaults to model_dir)
        seed: Random seed

    Returns:
        Dictionary with evaluation metrics
    """
    model_dir = Path(model_dir)
    output_dir = Path(output_dir or model_dir)
    output_dir.mkdir(parents=True, exist_ok=True)

    print(f"Evaluating model from: {model_dir}")

    # Load model and tokenizer
    tokenizer = AutoTokenizer.from_pretrained(model_dir)
    model = AutoModelForSequenceClassification.from_pretrained(model_dir)
    model.eval()

    # Use GPU if available
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model.to(device)
    print(f"Using device: {device}")

    # Load test data
    if dataset_type == "easy2hard":
        dataset = load_easy2hard_bench(max_samples=max_samples, seed=seed)
    else:
        dataset = load_arc_dataset(max_samples=max_samples, seed=seed)

    test_data = dataset["test"]
    print(f"Test set size: {len(test_data)}")

    # Run predictions
    all_labels = []
    all_predictions = []
    all_probabilities = []

    print("\nRunning predictions...")
    batch_size = 32

    for i in range(0, len(test_data), batch_size):
        batch = test_data.select(range(i, min(i + batch_size, len(test_data))))
        texts = batch["text"]
        labels = batch["label"]

        # Tokenize
        inputs = tokenizer(
            texts,
            padding=True,
            truncation=True,
            max_length=128,
            return_tensors="pt",
        ).to(device)

        # Predict
        with torch.no_grad():
            outputs = model(**inputs)
            logits = outputs.logits
            probs = torch.softmax(logits, dim=-1)
            preds = torch.argmax(logits, dim=-1)

        all_labels.extend(labels)
        all_predictions.extend(preds.cpu().numpy().tolist())
        all_probabilities.extend(probs[:, 1].cpu().numpy().tolist())

        if (i // batch_size) % 10 == 0:
            print(f"  Processed {min(i + batch_size, len(test_data))}/{len(test_data)}")

    # Convert to numpy
    labels = np.array(all_labels)
    predictions = np.array(all_predictions)
    probabilities = np.array(all_probabilities)

    # Calculate metrics
    accuracy = accuracy_score(labels, predictions)
    f1 = f1_score(labels, predictions, average="binary")
    roc_auc = roc_auc_score(labels, probabilities)

    print("\n" + "=" * 50)
    print("Evaluation Results")
    print("=" * 50)
    print(f"\nAccuracy: {accuracy:.4f}")
    print(f"F1 Score: {f1:.4f}")
    print(f"ROC AUC:  {roc_auc:.4f}")

    # Classification report
    print("\nClassification Report:")
    report = classification_report(
        labels,
        predictions,
        target_names=["simple", "complex"],
    )
    print(report)

    # Confusion matrix
    cm = confusion_matrix(labels, predictions)
    print("\nConfusion Matrix:")
    print(cm)

    # Save results
    metrics = {
        "accuracy": float(accuracy),
        "f1": float(f1),
        "roc_auc": float(roc_auc),
        "confusion_matrix": cm.tolist(),
        "classification_report": classification_report(
            labels, predictions, target_names=["simple", "complex"], output_dict=True
        ),
    }

    with open(output_dir / "evaluation_metrics.json", "w") as f:
        json.dump(metrics, f, indent=2)
    print(f"\nMetrics saved to: {output_dir / 'evaluation_metrics.json'}")

    # Generate plots
    _plot_confusion_matrix(cm, output_dir)
    _plot_roc_curve(labels, probabilities, output_dir)
    _plot_precision_recall_curve(labels, probabilities, output_dir)

    return metrics


def _plot_confusion_matrix(cm: np.ndarray, output_dir: Path) -> None:
    """Plot and save confusion matrix."""
    plt.figure(figsize=(8, 6))
    sns.heatmap(
        cm,
        annot=True,
        fmt="d",
        cmap="Blues",
        xticklabels=["simple", "complex"],
        yticklabels=["simple", "complex"],
    )
    plt.xlabel("Predicted")
    plt.ylabel("Actual")
    plt.title("Confusion Matrix")
    plt.tight_layout()
    plt.savefig(output_dir / "confusion_matrix.png", dpi=150)
    plt.close()
    print(f"Saved: {output_dir / 'confusion_matrix.png'}")


def _plot_roc_curve(labels: np.ndarray, probabilities: np.ndarray, output_dir: Path) -> None:
    """Plot and save ROC curve."""
    fpr, tpr, _ = roc_curve(labels, probabilities)
    roc_auc = roc_auc_score(labels, probabilities)

    plt.figure(figsize=(8, 6))
    plt.plot(fpr, tpr, color="blue", lw=2, label=f"ROC curve (AUC = {roc_auc:.3f})")
    plt.plot([0, 1], [0, 1], color="gray", lw=1, linestyle="--")
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel("False Positive Rate")
    plt.ylabel("True Positive Rate")
    plt.title("ROC Curve")
    plt.legend(loc="lower right")
    plt.tight_layout()
    plt.savefig(output_dir / "roc_curve.png", dpi=150)
    plt.close()
    print(f"Saved: {output_dir / 'roc_curve.png'}")


def _plot_precision_recall_curve(
    labels: np.ndarray, probabilities: np.ndarray, output_dir: Path
) -> None:
    """Plot and save precision-recall curve."""
    precision, recall, _ = precision_recall_curve(labels, probabilities)

    plt.figure(figsize=(8, 6))
    plt.plot(recall, precision, color="blue", lw=2)
    plt.xlabel("Recall")
    plt.ylabel("Precision")
    plt.title("Precision-Recall Curve")
    plt.tight_layout()
    plt.savefig(output_dir / "precision_recall_curve.png", dpi=150)
    plt.close()
    print(f"Saved: {output_dir / 'precision_recall_curve.png'}")


def analyze_errors(
    model_dir: str = "ml/artifacts/complexity-classifier",
    dataset_type: str = "arc",
    max_samples: int | None = None,
    num_examples: int = 10,
    seed: int = 42,
) -> None:
    """
    Analyze misclassified examples.

    Args:
        model_dir: Directory containing trained model
        dataset_type: "easy2hard" or "arc"
        max_samples: Maximum samples to evaluate
        num_examples: Number of error examples to show
        seed: Random seed
    """
    model_dir = Path(model_dir)

    # Load model and tokenizer
    tokenizer = AutoTokenizer.from_pretrained(model_dir)
    model = AutoModelForSequenceClassification.from_pretrained(model_dir)
    model.eval()

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model.to(device)

    # Load test data
    if dataset_type == "easy2hard":
        dataset = load_easy2hard_bench(max_samples=max_samples, seed=seed)
    else:
        dataset = load_arc_dataset(max_samples=max_samples, seed=seed)

    test_data = dataset["test"]

    # Find errors
    false_positives = []  # Predicted complex, actually simple
    false_negatives = []  # Predicted simple, actually complex

    for example in test_data:
        text = example["text"]
        label = example["label"]

        inputs = tokenizer(
            text,
            padding=True,
            truncation=True,
            max_length=128,
            return_tensors="pt",
        ).to(device)

        with torch.no_grad():
            outputs = model(**inputs)
            pred = torch.argmax(outputs.logits, dim=-1).item()
            prob = torch.softmax(outputs.logits, dim=-1)[0, 1].item()

        if pred != label:
            error_info = {
                "text": text,
                "true_label": "complex" if label == 1 else "simple",
                "pred_label": "complex" if pred == 1 else "simple",
                "confidence": prob if pred == 1 else 1 - prob,
            }

            if pred == 1 and label == 0:
                false_positives.append(error_info)
            else:
                false_negatives.append(error_info)

    # Print analysis
    print("\n" + "=" * 60)
    print("Error Analysis")
    print("=" * 60)

    print(f"\nTotal errors: {len(false_positives) + len(false_negatives)}")
    print(f"  False positives (predicted complex, actually simple): {len(false_positives)}")
    print(f"  False negatives (predicted simple, actually complex): {len(false_negatives)}")

    print("\n--- False Positives (thought complex, was simple) ---")
    for i, error in enumerate(false_positives[:num_examples]):
        print(f"\n[{i+1}] Confidence: {error['confidence']:.2f}")
        print(f"    Text: {error['text'][:150]}...")

    print("\n--- False Negatives (thought simple, was complex) ---")
    for i, error in enumerate(false_negatives[:num_examples]):
        print(f"\n[{i+1}] Confidence: {error['confidence']:.2f}")
        print(f"    Text: {error['text'][:150]}...")


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Evaluate complexity classifier")
    parser.add_argument(
        "--model-dir",
        type=str,
        default="ml/artifacts/complexity-classifier",
        help="Model directory",
    )
    parser.add_argument(
        "--dataset",
        choices=["easy2hard", "arc"],
        default="arc",
        help="Dataset to use",
    )
    parser.add_argument(
        "--max-samples",
        type=int,
        default=None,
        help="Maximum samples",
    )
    parser.add_argument(
        "--analyze-errors",
        action="store_true",
        help="Show error analysis",
    )

    args = parser.parse_args()

    evaluate_model(
        model_dir=args.model_dir,
        dataset_type=args.dataset,
        max_samples=args.max_samples,
    )

    if args.analyze_errors:
        analyze_errors(
            model_dir=args.model_dir,
            dataset_type=args.dataset,
            max_samples=args.max_samples,
        )