-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathconll03_context.py
73 lines (64 loc) · 2.41 KB
/
conll03_context.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from datasets import load_dataset
from transformers import TrainingArguments
from span_marker import SpanMarkerModel, SpanMarkerModelCardData, Trainer
def main() -> None:
# Load the dataset, ensure "tokens", "ner_tags", "document_id" and "sentence_id" columns,
# and get a list of labels
dataset_id = "conll2003"
dataset_name = "CoNLL 2003"
dataset = load_dataset(dataset_id)
labels = dataset["train"].features["ner_tags"].feature.names
# Initialize a SpanMarker model using a pretrained BERT-style encoder
encoder_id = "xlm-roberta-large"
model = SpanMarkerModel.from_pretrained(
encoder_id,
labels=labels,
# SpanMarker hyperparameters:
model_max_length=512,
marker_max_length=128,
entity_max_length=8,
# Model card arguments
model_card_data=SpanMarkerModelCardData(
model_id="tomaarsen/span-marker-xlm-roberta-large-conll03-doc-context",
encoder_id=encoder_id,
dataset_name=dataset_name,
dataset_id=dataset_id,
license="other",
language="en",
),
)
# Prepare the 🤗 transformers training arguments
args = TrainingArguments(
output_dir="models/span_marker_xlm_roberta_large_conll03_doc_context",
# Training Hyperparameters:
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, # Replace `bf16` with `fp16` if your hardware can't use bf16.
# Other Training parameters
logging_first_step=True,
logging_steps=50,
evaluation_strategy="steps",
save_strategy="steps",
eval_steps=1000,
save_total_limit=2,
dataloader_num_workers=2,
)
# Initialize the trainer using our model, training args & dataset, and train
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")
# Compute & save the metrics on the test set
metrics = trainer.evaluate(dataset["test"], metric_key_prefix="test")
trainer.save_metrics("test", metrics)
if __name__ == "__main__":
main()