tomaarsen HF staff commited on
Commit
0e3ac4c
1 Parent(s): b8834dd

Upload train.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train.py +94 -0
train.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import random
3
+ import shutil
4
+ from datasets import load_dataset
5
+ from transformers import TrainingArguments
6
+ from span_marker import SpanMarkerModel, Trainer
7
+ from span_marker.model_card import SpanMarkerModelCardData
8
+ from huggingface_hub import upload_folder, upload_file
9
+
10
+
11
+
12
+ def main() -> None:
13
+ # Load the dataset, ensure "tokens" and "ner_tags" columns, and get a list of labels
14
+ labels = ["O", "B-ORG", "I-ORG"]
15
+ dataset_id = "tomaarsen/ner-orgs"
16
+ dataset = load_dataset(dataset_id)
17
+
18
+ train_dataset = dataset["train"]
19
+ eval_dataset = dataset["validation"]
20
+ eval_dataset = eval_dataset.select(random.sample(range(len(eval_dataset)), k=3000))
21
+ test_dataset = dataset["test"]
22
+
23
+ # Initialize a SpanMarker model using a pretrained BERT-style encoder
24
+ encoder_id = "prajjwal1/bert-small"
25
+ model_id = f"tomaarsen/span-marker-bert-small-orgs"
26
+ model = SpanMarkerModel.from_pretrained(
27
+ encoder_id,
28
+ labels=labels,
29
+ # SpanMarker hyperparameters:
30
+ model_max_length=256,
31
+ marker_max_length=128,
32
+ entity_max_length=8,
33
+ # Model card variables
34
+ model_card_data=SpanMarkerModelCardData(
35
+ model_id=model_id,
36
+ dataset_id=dataset_id,
37
+ encoder_id=encoder_id,
38
+ dataset_name="FewNERD, CoNLL2003, and OntoNotes v5",
39
+ license="cc-by-sa-4.0",
40
+ language=["en"],
41
+ ),
42
+ )
43
+
44
+ # Prepare the 🤗 transformers training arguments
45
+ output_dir = Path("models") / model_id
46
+ args = TrainingArguments(
47
+ output_dir=output_dir,
48
+ run_name=model_id,
49
+ # Training Hyperparameters:
50
+ learning_rate=5e-5,
51
+ per_device_train_batch_size=128,
52
+ per_device_eval_batch_size=128,
53
+ num_train_epochs=3,
54
+ weight_decay=0.01,
55
+ warmup_ratio=0.1,
56
+ bf16=True, # Replace `bf16` with `fp16` if your hardware can't use bf16.
57
+ # Other Training parameters
58
+ logging_first_step=True,
59
+ logging_steps=100,
60
+ evaluation_strategy="steps",
61
+ save_strategy="steps",
62
+ eval_steps=600,
63
+ save_total_limit=1,
64
+ dataloader_num_workers=4,
65
+ )
66
+
67
+ # Initialize the trainer using our model, training args & dataset, and train
68
+ trainer = Trainer(
69
+ model=model,
70
+ args=args,
71
+ train_dataset=train_dataset,
72
+ eval_dataset=eval_dataset,
73
+ )
74
+ trainer.train()
75
+
76
+ # Compute & save the metrics on the test set
77
+ metrics = trainer.evaluate(test_dataset, metric_key_prefix="test")
78
+ trainer.save_metrics("test", metrics)
79
+
80
+ # Save the model & training script locally
81
+ trainer.save_model(output_dir / "checkpoint-final")
82
+ shutil.copy2(__file__, output_dir / "checkpoint-final" / "train.py")
83
+
84
+ # Upload everything to the Hub
85
+ breakpoint()
86
+ model.push_to_hub(model_id, private=True)
87
+ upload_folder(folder_path=output_dir / "runs", path_in_repo="runs", repo_id=model_id)
88
+ upload_file(path_or_fileobj=__file__, path_in_repo="train.py", repo_id=model_id)
89
+ upload_file(path_or_fileobj=output_dir / "all_results.json", path_in_repo="all_results.json", repo_id=model_id)
90
+ upload_file(path_or_fileobj=output_dir / "emissions.csv", path_in_repo="emissions.csv", repo_id=model_id)
91
+
92
+
93
+ if __name__ == "__main__":
94
+ main()