|
from datasets import load_dataset |
|
from transformers import TrainingArguments |
|
from span_marker import SpanMarkerModel, Trainer |
|
|
|
|
|
def main() -> None: |
|
|
|
dataset = load_dataset("acronym_identification").rename_column("labels", "ner_tags") |
|
labels = dataset["train"].features["ner_tags"].feature.names |
|
|
|
|
|
model_name = "bert-base-cased" |
|
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"models/span_marker_bert_base_acronyms", |
|
run_name=f"bb_acronyms", |
|
|
|
learning_rate=5e-5, |
|
per_device_train_batch_size=32, |
|
per_device_eval_batch_size=32, |
|
num_train_epochs=2, |
|
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=200, |
|
save_total_limit=2, |
|
dataloader_num_workers=2, |
|
) |
|
|
|
|
|
trainer = Trainer( |
|
model=model, |
|
args=args, |
|
train_dataset=dataset["train"], |
|
eval_dataset=dataset["validation"], |
|
) |
|
trainer.train() |
|
trainer.save_model(f"models/span_marker_bert_base_acronyms/checkpoint-final") |
|
|
|
|
|
metrics = trainer.evaluate() |
|
trainer.save_metrics("validation", metrics) |
|
trainer.create_model_card() |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|