|
from datasets import load_dataset |
|
from span_marker import SpanMarkerModel, Trainer |
|
from transformers import TrainingArguments |
|
|
|
|
|
def main() -> None: |
|
|
|
|
|
dataset = load_dataset("tomaarsen/conll2003") |
|
labels = dataset["train"].features["ner_tags"].feature.names |
|
|
|
|
|
model_name = "xlm-roberta-large" |
|
model = SpanMarkerModel.from_pretrained( |
|
model_name, |
|
labels=labels, |
|
|
|
model_max_length=512, |
|
marker_max_length=128, |
|
entity_max_length=8, |
|
) |
|
|
|
|
|
args = TrainingArguments( |
|
output_dir="models/span_marker_xlm_roberta_large_conll03_doc_context", |
|
|
|
learning_rate=1e-5, |
|
per_device_train_batch_size=4, |
|
per_device_eval_batch_size=4, |
|
gradient_accumulation_steps=2, |
|
num_train_epochs=3, |
|
weight_decay=0.01, |
|
warmup_ratio=0.1, |
|
bf16=True, |
|
|
|
logging_first_step=True, |
|
logging_steps=50, |
|
evaluation_strategy="steps", |
|
save_strategy="steps", |
|
eval_steps=1000, |
|
dataloader_num_workers=2, |
|
) |
|
|
|
|
|
trainer = Trainer( |
|
model=model, |
|
args=args, |
|
train_dataset=dataset["train"], |
|
eval_dataset=dataset["validation"], |
|
) |
|
trainer.train() |
|
trainer.save_model("models/span_marker_xlm_roberta_large_conll03_doc_context/checkpoint-final") |
|
|
|
|
|
metrics = trainer.evaluate(dataset["test"], metric_key_prefix="test") |
|
trainer.save_metrics("test", metrics) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |