stefan-it commited on
Commit
c254154
1 Parent(s): 0c803dc

scripts: add training and evaluation helpers

Browse files
Files changed (2) hide show
  1. evaluator.py +44 -0
  2. trainer.py +62 -0
evaluator.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+
3
+ from datasets import load_dataset
4
+ from transformers import TrainingArguments
5
+ from span_marker import SpanMarkerModel, Trainer
6
+
7
+
8
+ # Load the dataset, ensure "tokens" and "ner_tags" columns, and get a list of labels
9
+ dataset = load_dataset("gwlms/germeval2014")
10
+ labels = dataset["train"].features["ner_tags"].feature.names
11
+
12
+ # Initialize a SpanMarker model using a pretrained BERT-style encoder
13
+ model_name = sys.argv[1]
14
+ model = SpanMarkerModel.from_pretrained(
15
+ model_name,
16
+ labels=labels,
17
+ # SpanMarker hyperparameters:
18
+ model_max_length=256,
19
+ marker_max_length=128,
20
+ entity_max_length=8,
21
+ )
22
+
23
+ args = TrainingArguments(
24
+ output_dir="/tmp",
25
+ per_device_eval_batch_size=64,
26
+ )
27
+
28
+ # Initialize the trainer using our model, training args & dataset, and train
29
+ trainer = Trainer(
30
+ model=model,
31
+ args=args,
32
+ train_dataset=dataset["train"],
33
+ eval_dataset=dataset["validation"],
34
+ )
35
+
36
+
37
+ print("Evaluating on development set...")
38
+ dev_metrics = trainer.evaluate(dataset["validation"], metric_key_prefix="eval")
39
+ print(dev_metrics)
40
+
41
+ print("Evaluating on test set...")
42
+ test_metrics = trainer.evaluate(dataset["test"], metric_key_prefix="test")
43
+ print(test_metrics)
44
+
trainer.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ from transformers import TrainingArguments
3
+ from span_marker import SpanMarkerModel, Trainer
4
+
5
+ def perform_training(learning_rate: float, seed: int) -> None:
6
+ # Load the dataset, ensure "tokens" and "ner_tags" columns, and get a list of labels
7
+ dataset = load_dataset("gwlms/germeval2014")
8
+ labels = dataset["train"].features["ner_tags"].feature.names
9
+
10
+ # Initialize a SpanMarker model using a pretrained BERT-style encoder
11
+ model_name = "deepset/gelectra-large"
12
+ model = SpanMarkerModel.from_pretrained(
13
+ model_name,
14
+ labels=labels,
15
+ # SpanMarker hyperparameters:
16
+ model_max_length=256,
17
+ marker_max_length=128,
18
+ entity_max_length=8,
19
+ )
20
+
21
+ # Prepare the 🤗 transformers training arguments
22
+ args = TrainingArguments(
23
+ output_dir=f"./span_marker-gelectra-large-bs16-lr{learning_rate}-{seed}",
24
+ # Training Hyperparameters:
25
+ learning_rate=learning_rate,
26
+ per_device_train_batch_size=16,
27
+ per_device_eval_batch_size=16,
28
+ num_train_epochs=3,
29
+ weight_decay=0.01,
30
+ warmup_ratio=0.1,
31
+ fp16=True, # Replace `bf16` with `fp16` if your hardware can't use bf16.
32
+ # Other Training parameters
33
+ logging_first_step=True,
34
+ logging_steps=50,
35
+ evaluation_strategy="epoch",
36
+ save_strategy="epoch",
37
+ save_total_limit=11,
38
+ dataloader_num_workers=2,
39
+ seed=seed,
40
+ load_best_model_at_end=True,
41
+ )
42
+
43
+ # Initialize the trainer using our model, training args & dataset, and train
44
+ trainer = Trainer(
45
+ model=model,
46
+ args=args,
47
+ train_dataset=dataset["train"],
48
+ eval_dataset=dataset["validation"],
49
+ )
50
+ trainer.train()
51
+ trainer.save_model(f"./span_marker-gelectra-large-bs16-lr{learning_rate}-{seed}/best-checkpoint")
52
+
53
+ # Compute & save the metrics on the test set
54
+ metrics = trainer.evaluate(dataset["test"], metric_key_prefix="test")
55
+ trainer.save_metrics("test", metrics)
56
+
57
+
58
+ if __name__ == "__main__":
59
+ for learning_rate in [5e-05]:
60
+ for seed in [1,2,3,4,5]:
61
+ perform_training(learning_rate, seed)
62
+