Spaces:
Configuration error
Configuration error
File size: 1,711 Bytes
ee140b6 | 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 | # scripts/train_classifier.py
import numpy as np, pandas as pd
from datasets import Dataset
from transformers import (
AutoTokenizer, AutoModelForSequenceClassification,
Trainer, TrainingArguments,
)
from sklearn.metrics import precision_recall_fscore_support, accuracy_score
def to_ds(df, tok):
ds = Dataset.from_pandas(df[["text", "label"]])
ds = ds.map(lambda x: tok(x["text"], truncation=True,
padding="max_length", max_length=128),
batched=True)
return ds
def metrics(ep):
logits, labels = ep
preds = np.argmax(logits, axis=-1)
p, r, f, _ = precision_recall_fscore_support(labels, preds,
average="binary",
zero_division=0)
return {"accuracy": accuracy_score(labels, preds),
"precision": p, "recall": r, "f1": f}
tok = AutoTokenizer.from_pretrained("distilbert-base-uncased")
model = AutoModelForSequenceClassification.from_pretrained(
"distilbert-base-uncased", num_labels=2)
train = to_ds(pd.read_csv("data/clf_train.csv"), tok)
val = to_ds(pd.read_csv("data/clf_val.csv"), tok)
args = TrainingArguments(
output_dir="models/domain-classifier",
num_train_epochs=3,
per_device_train_batch_size=16,
per_device_eval_batch_size=32,
eval_strategy="epoch",
save_strategy="epoch",
load_best_model_at_end=True,
metric_for_best_model="f1",
learning_rate=2e-5,
)
Trainer(model=model, args=args, train_dataset=train, eval_dataset=val,
compute_metrics=metrics).train()
model.save_pretrained("models/domain-classifier")
tok.save_pretrained("models/domain-classifier") |