|
from datasets import load_dataset |
|
from transformers import TrainingArguments |
|
from span_marker import SpanMarkerModel, Trainer |
|
|
|
def perform_training(learning_rate: float, seed: int) -> None: |
|
|
|
dataset = load_dataset("gwlms/germeval2014") |
|
labels = dataset["train"].features["ner_tags"].feature.names |
|
|
|
|
|
model_name = "gwlms/span-marker-bert-germeval14" |
|
model = SpanMarkerModel.from_pretrained( |
|
model_name, |
|
labels=labels, |
|
|
|
model_max_length=256, |
|
marker_max_length=128, |
|
entity_max_length=8, |
|
) |
|
|
|
|
|
args = TrainingArguments( |
|
output_dir=f"./span_marker-{model_name}-bs16-lr{learning_rate}-{seed}", |
|
|
|
learning_rate=learning_rate, |
|
per_device_train_batch_size=16, |
|
per_device_eval_batch_size=16, |
|
num_train_epochs=3, |
|
weight_decay=0.01, |
|
warmup_ratio=0.1, |
|
fp16=True, |
|
|
|
logging_first_step=True, |
|
logging_steps=50, |
|
evaluation_strategy="epoch", |
|
save_strategy="epoch", |
|
save_total_limit=11, |
|
dataloader_num_workers=2, |
|
seed=seed, |
|
load_best_model_at_end=True, |
|
) |
|
|
|
|
|
trainer = Trainer( |
|
model=model, |
|
args=args, |
|
train_dataset=dataset["train"], |
|
eval_dataset=dataset["validation"], |
|
) |
|
trainer.train() |
|
trainer.save_model(f"./span_marker-{model_name}-bs16-lr{learning_rate}-{seed}/best-checkpoint") |
|
|
|
|
|
metrics = trainer.evaluate(dataset["test"], metric_key_prefix="test") |
|
trainer.save_metrics("test", metrics) |
|
|
|
|
|
if __name__ == "__main__": |
|
for learning_rate in [5e-05]: |
|
for seed in [1,2,3,4,5]: |
|
perform_training(learning_rate, seed) |
|
|