Sync card and scripts from the monorepo
Browse files- data_generator.py +24 -4
- requirements.txt +1 -1
- train.py +1 -2
data_generator.py
CHANGED
|
@@ -851,19 +851,39 @@ def save_to_csv(dataset: list[dict], path: str) -> None:
|
|
| 851 |
|
| 852 |
|
| 853 |
def load_as_hf_dataset(dataset: list[dict]):
|
| 854 |
-
"""Convert to HuggingFace Dataset with train/val/test splits.
|
| 855 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 856 |
|
| 857 |
ds = Dataset.from_list(dataset)
|
|
|
|
|
|
|
| 858 |
|
| 859 |
-
# 80/10/10 split
|
| 860 |
train_test = ds.train_test_split(test_size=0.2, seed=42, stratify_by_column="label")
|
| 861 |
val_test = train_test["test"].train_test_split(test_size=0.5, seed=42, stratify_by_column="label")
|
| 862 |
|
| 863 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 864 |
"train": train_test["train"],
|
| 865 |
"validation": val_test["train"],
|
| 866 |
"test": val_test["test"],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 867 |
})
|
| 868 |
|
| 869 |
|
|
|
|
| 851 |
|
| 852 |
|
| 853 |
def load_as_hf_dataset(dataset: list[dict]):
|
| 854 |
+
"""Convert to HuggingFace Dataset with stratified train/val/test splits.
|
| 855 |
+
|
| 856 |
+
``train_test_split(stratify_by_column=...)`` only accepts a ``ClassLabel``
|
| 857 |
+
column, so the label is cast for the split and decoded back to its string
|
| 858 |
+
name afterwards -- ``train.py`` looks labels up by name via ``label2id``.
|
| 859 |
+
"""
|
| 860 |
+
from datasets import ClassLabel, Dataset, DatasetDict, Features, Value
|
| 861 |
|
| 862 |
ds = Dataset.from_list(dataset)
|
| 863 |
+
class_label = ClassLabel(names=sorted({row["label"] for row in dataset}))
|
| 864 |
+
ds = ds.cast_column("label", class_label)
|
| 865 |
|
| 866 |
+
# 80/10/10 split, stratified at both steps
|
| 867 |
train_test = ds.train_test_split(test_size=0.2, seed=42, stratify_by_column="label")
|
| 868 |
val_test = train_test["test"].train_test_split(test_size=0.5, seed=42, stratify_by_column="label")
|
| 869 |
|
| 870 |
+
# Decode ids back to names. Casting ClassLabel -> string does NOT do this
|
| 871 |
+
# (it stringifies the ids, giving "0"/"1"), and mapping into a column that
|
| 872 |
+
# is still typed ClassLabel silently re-encodes the names straight back to
|
| 873 |
+
# ids -- hence int2str plus an explicit output schema.
|
| 874 |
+
string_features = Features({"text": Value("string"), "label": Value("string")})
|
| 875 |
+
|
| 876 |
+
def to_label_name(batch):
|
| 877 |
+
return {"label": class_label.int2str(batch["label"])}
|
| 878 |
+
|
| 879 |
+
splits = {
|
| 880 |
"train": train_test["train"],
|
| 881 |
"validation": val_test["train"],
|
| 882 |
"test": val_test["test"],
|
| 883 |
+
}
|
| 884 |
+
return DatasetDict({
|
| 885 |
+
name: split.map(to_label_name, batched=True, features=string_features)
|
| 886 |
+
for name, split in splits.items()
|
| 887 |
})
|
| 888 |
|
| 889 |
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
transformers>=
|
| 2 |
datasets>=2.16.0,<4.0.0
|
| 3 |
torch>=2.1.0,<3.0.0
|
| 4 |
scikit-learn>=1.3.0,<2.0.0
|
|
|
|
| 1 |
+
transformers>=5.5.0,<6.0.0
|
| 2 |
datasets>=2.16.0,<4.0.0
|
| 3 |
torch>=2.1.0,<3.0.0
|
| 4 |
scikit-learn>=1.3.0,<2.0.0
|
train.py
CHANGED
|
@@ -218,7 +218,6 @@ def train(
|
|
| 218 |
|
| 219 |
training_args = TrainingArguments(
|
| 220 |
output_dir=output_dir,
|
| 221 |
-
overwrite_output_dir=True,
|
| 222 |
# Training hyperparameters
|
| 223 |
num_train_epochs=epochs,
|
| 224 |
per_device_train_batch_size=batch_size,
|
|
@@ -261,7 +260,7 @@ def train(
|
|
| 261 |
args=training_args,
|
| 262 |
train_dataset=tokenized_dataset["train"],
|
| 263 |
eval_dataset=tokenized_dataset["validation"],
|
| 264 |
-
|
| 265 |
data_collator=data_collator,
|
| 266 |
compute_metrics=build_compute_metrics(id2label),
|
| 267 |
callbacks=callbacks,
|
|
|
|
| 218 |
|
| 219 |
training_args = TrainingArguments(
|
| 220 |
output_dir=output_dir,
|
|
|
|
| 221 |
# Training hyperparameters
|
| 222 |
num_train_epochs=epochs,
|
| 223 |
per_device_train_batch_size=batch_size,
|
|
|
|
| 260 |
args=training_args,
|
| 261 |
train_dataset=tokenized_dataset["train"],
|
| 262 |
eval_dataset=tokenized_dataset["validation"],
|
| 263 |
+
processing_class=tokenizer,
|
| 264 |
data_collator=data_collator,
|
| 265 |
compute_metrics=build_compute_metrics(id2label),
|
| 266 |
callbacks=callbacks,
|