config dataclass for models
Browse files- src/schemas/config.py +72 -0
src/schemas/config.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from dataclasses import dataclass, fields
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from transformers import TrainingArguments
|
| 5 |
+
|
| 6 |
+
VALID_MODES = {"marker", "qa_m", "qa_b"}
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@dataclass
|
| 10 |
+
class TrainingConfig:
|
| 11 |
+
mode: str = "marker"
|
| 12 |
+
data_path: str = "data/data_augmented_256.jsonl"
|
| 13 |
+
output_dir: str = "models/"
|
| 14 |
+
model_name: str = "distilbert-base-uncased"
|
| 15 |
+
max_len: int = 256
|
| 16 |
+
num_train_epochs: int = 5
|
| 17 |
+
per_device_train_batch_size: int = 32
|
| 18 |
+
per_device_eval_batch_size: int = 64
|
| 19 |
+
gradient_accumulation_steps: int = 1
|
| 20 |
+
learning_rate: float = 2e-5
|
| 21 |
+
warmup_ratio: float = 0.1
|
| 22 |
+
weight_decay: float = 0.01
|
| 23 |
+
val_split: float = 0.1
|
| 24 |
+
test_split: float = 0.1
|
| 25 |
+
early_stopping_patience: int = 3
|
| 26 |
+
fp16: bool = True
|
| 27 |
+
seed: int = 42
|
| 28 |
+
logging_steps: int = 50
|
| 29 |
+
save_total_limit: int = 2
|
| 30 |
+
loss_fn: str = "cross_entropy"
|
| 31 |
+
focal_gamma: float = 2.0
|
| 32 |
+
|
| 33 |
+
def __post_init__(self):
|
| 34 |
+
if self.mode not in VALID_MODES:
|
| 35 |
+
raise ValueError(f"mode must be one of {VALID_MODES}, got '{self.mode}'")
|
| 36 |
+
if self.loss_fn not in ("cross_entropy", "focal"):
|
| 37 |
+
raise ValueError(f"loss_fn must be 'cross_entropy' or 'focal', got '{self.loss_fn}'")
|
| 38 |
+
|
| 39 |
+
@classmethod
|
| 40 |
+
def from_json(cls, path: str | Path) -> "TrainingConfig":
|
| 41 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 42 |
+
raw = json.load(f)
|
| 43 |
+
known = {f.name for f in fields(cls)}
|
| 44 |
+
unknown = set(raw.keys()) - known
|
| 45 |
+
if unknown:
|
| 46 |
+
print(f"Warning: unknown config keys ignored: {unknown}")
|
| 47 |
+
filtered = {k: v for k, v in raw.items() if k in known}
|
| 48 |
+
return cls(**filtered)
|
| 49 |
+
|
| 50 |
+
def to_training_arguments(self) -> TrainingArguments:
|
| 51 |
+
return TrainingArguments(
|
| 52 |
+
output_dir=self.output_dir,
|
| 53 |
+
num_train_epochs=self.num_train_epochs,
|
| 54 |
+
per_device_train_batch_size=self.per_device_train_batch_size,
|
| 55 |
+
per_device_eval_batch_size=self.per_device_eval_batch_size,
|
| 56 |
+
gradient_accumulation_steps=self.gradient_accumulation_steps,
|
| 57 |
+
learning_rate=self.learning_rate,
|
| 58 |
+
warmup_ratio=self.warmup_ratio,
|
| 59 |
+
weight_decay=self.weight_decay,
|
| 60 |
+
eval_strategy="epoch",
|
| 61 |
+
save_strategy="epoch",
|
| 62 |
+
load_best_model_at_end=True,
|
| 63 |
+
metric_for_best_model="macro_f1",
|
| 64 |
+
greater_is_better=True,
|
| 65 |
+
save_total_limit=self.save_total_limit,
|
| 66 |
+
logging_dir=f"{self.output_dir}/logs",
|
| 67 |
+
logging_steps=self.logging_steps,
|
| 68 |
+
report_to="none",
|
| 69 |
+
fp16=self.fp16,
|
| 70 |
+
seed=self.seed,
|
| 71 |
+
data_seed=self.seed,
|
| 72 |
+
)
|