-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
147 lines (122 loc) · 4.02 KB
/
train.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Fine-tune a Distilbert model on a profanity dataset.
"""
from codecarbon import track_emissions
import argparse
import torch
import evaluate
from datasets import load_dataset
import numpy as np
from transformers import DataCollatorWithPadding
from transformers import (
DistilBertTokenizer,
BertTokenizer,
DistilBertForSequenceClassification,
BertForSequenceClassification,
Trainer,
TrainingArguments,
)
from transformers.trainer_callback import EarlyStoppingCallback
BASE_MODEL = "distilbert-base-uncased"
TINY_BASE_MODEL = "huawei-noah/TinyBERT_General_4L_312D"
DATASET = "tarekziade/profanity"
ID2LABEL = {0: "NOT_OFFENSIVE", 1: "OFFENSIVE"}
LABEL2ID = {"NOT_OFFENSIVE": 0, "OFFENSIVE": 1}
MODEL_PATH = "./pardonmyai"
accuracy = evaluate.load("accuracy")
if torch.cuda.is_available():
device = torch.device("cuda")
print("Using CUDA (Nvidia GPU).")
elif torch.backends.mps.is_available():
device = torch.device("mps")
print("Using MPS (Apple Silicon GPU).")
else:
device = torch.device("cpu")
print("Using CPU.")
def get_datasets(args, tokenizer):
if args.fine_tune:
dataset = load_dataset("tarekziade/animal_descriptions", split="train")
else:
dataset = load_dataset("tarekziade/profanity-clean", split="train")
dataset = dataset.rename_column("is_offensive", "label")
def tokenize_function(examples):
return tokenizer(
examples["text"],
padding="max_length",
truncation=True,
max_length=512,
)
tokenized_datasets = dataset.map(tokenize_function, batched=True)
return tokenized_datasets.train_test_split(test_size=0.2)
def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=1)
return accuracy.compute(predictions=predictions, references=labels)
training_args = TrainingArguments(
output_dir="pardonmycaption",
num_train_epochs=3,
learning_rate=2e-5,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
warmup_steps=500,
weight_decay=0.01,
evaluation_strategy="epoch",
save_strategy="epoch",
load_best_model_at_end=True,
logging_dir="./logs",
)
@track_emissions(project_name="PardonMyAI")
def train(args):
if args.tiny:
model_name = TINY_BASE_MODEL
model_path = MODEL_PATH + "-tiny"
tokenizer_klass = BertTokenizer
model_klass = BertForSequenceClassification
elif args.fine_tune:
model_name = "tarekziade/pardonmyai"
model_path = MODEL_PATH + "-fine-tune"
tokenizer_klass = DistilBertTokenizer
model_klass = DistilBertForSequenceClassification
else:
model_name = BASE_MODEL
model_path = MODEL_PATH
tokenizer_klass = DistilBertTokenizer
model_klass = DistilBertForSequenceClassification
tokenizer = tokenizer_klass.from_pretrained(model_name)
model = model_klass.from_pretrained(
model_name, num_labels=2, id2label=ID2LABEL, label2id=LABEL2ID
)
model.to(device)
datasets = get_datasets(args, tokenizer)
trainer = Trainer(
tokenizer=tokenizer,
model=model,
args=training_args,
data_collator=DataCollatorWithPadding(tokenizer=tokenizer),
train_dataset=datasets["train"],
eval_dataset=datasets["test"],
compute_metrics=compute_metrics,
callbacks=[EarlyStoppingCallback(early_stopping_patience=3)],
)
try:
trainer.train()
finally:
model.save_pretrained(model_path)
tokenizer.save_pretrained(model_path)
trainer.push_to_hub("tarekziade/pardonmycaption")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Train the profanity model")
parser.add_argument(
"--tiny",
action="store_true",
help="Enable the tiny mode.",
default=False,
)
parser.add_argument(
"--fine-tune",
action="store_true",
help="Second pass",
default=False,
)
args = parser.parse_args()
train(args)