File size: 7,899 Bytes
06b9814
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Train DistilBERT for complexity classification."""

import json
import os
from pathlib import Path

import numpy as np
import torch
from datasets import DatasetDict
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
from transformers import (
    AutoModelForSequenceClassification,
    AutoTokenizer,
    DataCollatorWithPadding,
    EarlyStoppingCallback,
    Trainer,
    TrainingArguments,
)

# 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 compute_metrics(eval_pred) -> dict:
    """Compute evaluation metrics."""
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)

    return {
        "accuracy": accuracy_score(labels, predictions),
        "f1": f1_score(labels, predictions, average="binary"),
        "precision": precision_score(labels, predictions, average="binary"),
        "recall": recall_score(labels, predictions, average="binary"),
    }


def tokenize_dataset(
    dataset: DatasetDict,
    tokenizer: AutoTokenizer,
    max_length: int = 128,
) -> DatasetDict:
    """Tokenize the dataset."""

    def tokenize_function(examples):
        return tokenizer(
            examples["text"],
            padding=False,  # Will be handled by data collator
            truncation=True,
            max_length=max_length,
        )

    tokenized = dataset.map(
        tokenize_function,
        batched=True,
        remove_columns=["text", "difficulty_score"],
        desc="Tokenizing",
    )

    return tokenized


def train_complexity_classifier(
    model_name: str = "distilbert-base-uncased",
    dataset_type: str = "arc",
    max_samples: int | None = 5000,
    output_dir: str = "ml/artifacts/complexity-classifier",
    num_epochs: int = 5,
    batch_size: int = 16,
    learning_rate: float = 2e-5,
    max_length: int = 128,
    seed: int = 42,
) -> dict:
    """
    Train a DistilBERT model for complexity classification.

    Args:
        model_name: HuggingFace model name
        dataset_type: "easy2hard" or "arc"
        max_samples: Maximum training samples (None for all)
        output_dir: Directory to save model
        num_epochs: Number of training epochs
        batch_size: Training batch size
        learning_rate: Learning rate
        max_length: Maximum sequence length
        seed: Random seed

    Returns:
        Dictionary with training metrics
    """
    # Set seed for reproducibility
    torch.manual_seed(seed)
    np.random.seed(seed)

    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)

    print(f"Training complexity classifier")
    print(f"  Model: {model_name}")
    print(f"  Dataset: {dataset_type}")
    print(f"  Output: {output_dir}")
    print()

    # Load dataset
    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)

    # Load tokenizer and model
    print(f"\nLoading model: {model_name}")
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSequenceClassification.from_pretrained(
        model_name,
        num_labels=2,
        id2label={0: "simple", 1: "complex"},
        label2id={"simple": 0, "complex": 1},
    )

    # Tokenize dataset
    print("\nTokenizing dataset...")
    tokenized_dataset = tokenize_dataset(dataset, tokenizer, max_length)

    # Data collator for dynamic padding
    data_collator = DataCollatorWithPadding(tokenizer=tokenizer)

    # Training arguments
    training_args = TrainingArguments(
        output_dir=str(output_dir / "checkpoints"),
        eval_strategy="epoch",
        save_strategy="epoch",
        learning_rate=learning_rate,
        per_device_train_batch_size=batch_size,
        per_device_eval_batch_size=batch_size,
        num_train_epochs=num_epochs,
        weight_decay=0.01,
        load_best_model_at_end=True,
        metric_for_best_model="f1",
        greater_is_better=True,
        logging_dir=str(output_dir / "logs"),
        logging_steps=50,
        seed=seed,
        report_to="none",  # Disable wandb/tensorboard
    )

    # Create trainer
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=tokenized_dataset["train"],
        eval_dataset=tokenized_dataset["validation"],
        tokenizer=tokenizer,
        data_collator=data_collator,
        compute_metrics=compute_metrics,
        callbacks=[EarlyStoppingCallback(early_stopping_patience=2)],
    )

    # Train
    print("\nStarting training...")
    train_result = trainer.train()

    # Evaluate on test set
    print("\nEvaluating on test set...")
    test_metrics = trainer.evaluate(tokenized_dataset["test"])

    # Save the model
    print(f"\nSaving model to {output_dir}")
    trainer.save_model(str(output_dir))
    tokenizer.save_pretrained(str(output_dir))

    # Save metrics
    metrics = {
        "train": {
            "loss": train_result.training_loss,
            "epochs": train_result.metrics.get("epoch", num_epochs),
        },
        "test": {
            "accuracy": test_metrics["eval_accuracy"],
            "f1": test_metrics["eval_f1"],
            "precision": test_metrics["eval_precision"],
            "recall": test_metrics["eval_recall"],
            "loss": test_metrics["eval_loss"],
        },
        "config": {
            "model_name": model_name,
            "dataset_type": dataset_type,
            "max_samples": max_samples,
            "num_epochs": num_epochs,
            "batch_size": batch_size,
            "learning_rate": learning_rate,
            "max_length": max_length,
        },
    }

    with open(output_dir / "metrics.json", "w") as f:
        json.dump(metrics, f, indent=2)

    print("\n" + "=" * 50)
    print("Training complete!")
    print("=" * 50)
    print(f"\nTest Results:")
    print(f"  Accuracy:  {test_metrics['eval_accuracy']:.4f}")
    print(f"  F1 Score:  {test_metrics['eval_f1']:.4f}")
    print(f"  Precision: {test_metrics['eval_precision']:.4f}")
    print(f"  Recall:    {test_metrics['eval_recall']:.4f}")
    print(f"\nModel saved to: {output_dir}")

    return metrics


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Train complexity classifier")
    parser.add_argument(
        "--model",
        type=str,
        default="distilbert-base-uncased",
        help="HuggingFace model name",
    )
    parser.add_argument(
        "--dataset",
        choices=["easy2hard", "arc"],
        default="arc",
        help="Dataset to use",
    )
    parser.add_argument(
        "--max-samples",
        type=int,
        default=5000,
        help="Maximum samples (None for all)",
    )
    parser.add_argument(
        "--output-dir",
        type=str,
        default="ml/artifacts/complexity-classifier",
        help="Output directory",
    )
    parser.add_argument(
        "--epochs",
        type=int,
        default=5,
        help="Number of epochs",
    )
    parser.add_argument(
        "--batch-size",
        type=int,
        default=16,
        help="Batch size",
    )
    parser.add_argument(
        "--lr",
        type=float,
        default=2e-5,
        help="Learning rate",
    )
    parser.add_argument(
        "--max-length",
        type=int,
        default=128,
        help="Maximum sequence length",
    )

    args = parser.parse_args()

    train_complexity_classifier(
        model_name=args.model,
        dataset_type=args.dataset,
        max_samples=args.max_samples,
        output_dir=args.output_dir,
        num_epochs=args.epochs,
        batch_size=args.batch_size,
        learning_rate=args.lr,
        max_length=args.max_length,
    )