Add training script
Browse files
train.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
from span_marker import SpanMarkerModel, Trainer
|
3 |
+
from transformers import TrainingArguments
|
4 |
+
|
5 |
+
|
6 |
+
def main() -> None:
|
7 |
+
# Load the dataset, ensure "tokens" and "ner_tags" columns, and get a list of labels
|
8 |
+
dataset = load_dataset("DFKI-SLT/few-nerd", "supervised")
|
9 |
+
dataset = dataset.remove_columns("ner_tags")
|
10 |
+
dataset = dataset.rename_column("fine_ner_tags", "ner_tags")
|
11 |
+
labels = dataset["train"].features["ner_tags"].feature.names
|
12 |
+
|
13 |
+
# Initialize a SpanMarker model using a pretrained BERT-style encoder
|
14 |
+
model_name = "roberta-large"
|
15 |
+
model = SpanMarkerModel.from_pretrained(
|
16 |
+
model_name,
|
17 |
+
labels=labels,
|
18 |
+
# SpanMarker hyperparameters:
|
19 |
+
model_max_length=256,
|
20 |
+
marker_max_length=128,
|
21 |
+
entity_max_length=8,
|
22 |
+
)
|
23 |
+
|
24 |
+
# Prepare the 🤗 transformers training arguments
|
25 |
+
args = TrainingArguments(
|
26 |
+
output_dir="models/span_marker_roberta_large_fewnerd_fine_super",
|
27 |
+
# Training Hyperparameters:
|
28 |
+
learning_rate=1e-5,
|
29 |
+
per_device_train_batch_size=8,
|
30 |
+
per_device_eval_batch_size=8,
|
31 |
+
num_train_epochs=3,
|
32 |
+
weight_decay=0.01,
|
33 |
+
warmup_ratio=0.1,
|
34 |
+
bf16=True,
|
35 |
+
# Other Training parameters
|
36 |
+
logging_first_step=True,
|
37 |
+
logging_steps=50,
|
38 |
+
evaluation_strategy="steps",
|
39 |
+
save_strategy="steps",
|
40 |
+
eval_steps=3000,
|
41 |
+
dataloader_num_workers=2,
|
42 |
+
)
|
43 |
+
|
44 |
+
# Initialize the trainer using our model, training args & dataset, and train
|
45 |
+
trainer = Trainer(
|
46 |
+
model=model,
|
47 |
+
args=args,
|
48 |
+
train_dataset=dataset["train"],
|
49 |
+
eval_dataset=dataset["validation"],
|
50 |
+
)
|
51 |
+
trainer.train()
|
52 |
+
trainer.save_model("models/span_marker_roberta_large_fewnerd_fine_super/checkpoint-final")
|
53 |
+
|
54 |
+
# Compute & save the metrics on the test set
|
55 |
+
metrics = trainer.evaluate(dataset["test"], metric_key_prefix="test")
|
56 |
+
trainer.save_metrics("test", metrics)
|
57 |
+
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
main()
|