-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
131 lines (103 loc) · 4.13 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
import wandb
from datasets import load_from_disk
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer, DataCollatorWithPadding
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
from custom_trainer import CustomTrainer
import json
import sys
import math
import torch
torch.manual_seed(42)
MODEL_NAME = 'xlm-roberta-large'
DEVICE = 'cuda'
dataset_test = load_from_disk('./data/emotion_dataset_test_ready.jsonl')
dataset_train = load_from_disk('./data/emotion_dataset_train_ready.jsonl')
tokenizer = AutoTokenizer.from_pretrained(
MODEL_NAME, use_fast=True, add_prefix_space=True, truncation=True, padding=False)
data_collator = DataCollatorWithPadding(
tokenizer=tokenizer, max_length=512
)
with open('./data/class_name_to_labels.json', 'r') as fh:
class_name_to_labels = json.load(fh)
num_classes = len(class_name_to_labels)
id2label = {v: k for k, v in class_name_to_labels.items()}
label2id = class_name_to_labels
def compute_metrics(pred):
labels = pred.label_ids
preds = pred.predictions.argmax(-1)
precision_macro, recall_macro, f1_macro, _ = precision_recall_fscore_support(
labels, preds, average='macro')
precision_micro, recall_micro, f1_micro, _ = precision_recall_fscore_support(
labels, preds, average='micro')
acc = accuracy_score(labels, preds)
wandb.log({
'accuracy': acc,
'f1_macro': f1_macro,
'precision_macro': precision_macro,
'recall_macro': recall_macro,
'f1_micro': f1_micro,
'precision_micro': precision_micro,
'recall_micro': recall_micro
})
return {
'accuracy': acc,
'f1_macro': f1_macro,
'precision_macro': precision_macro,
'recall_macro': recall_macro,
'f1_micro': f1_micro,
'precision_micro': precision_micro,
'recall_micro': recall_micro
}
if __name__ == '__main__':
wandb.init(project='labcafe-finetuning-emotion')
if len(sys.argv) == 1:
wandb.config.epochs = 5
wandb.config.batch_size = 16
wandb.config.learning_rate = 1e-5
wandb.config.gradient_accumulation = 1
wandb.config.weight_decay = 0.3
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=num_classes,
id2label=id2label,
label2id=label2id).to(DEVICE)
run_name = f'{MODEL_NAME}-lr_{wandb.config.learning_rate}-bs_{wandb.config.batch_size}-epochs_{wandb.config.epochs}-grad_acc_{wandb.config.gradient_accumulation}-weight_decay_{wandb.config.weight_decay}'
training_args = TrainingArguments(
output_dir=f'./results/{run_name}',
num_train_epochs=wandb.config.epochs,
per_device_train_batch_size=wandb.config.batch_size,
per_device_eval_batch_size=wandb.config.batch_size,
warmup_steps=math.ceil(len(dataset_train) *
wandb.config.epochs / wandb.config.batch_size * 0.1),
logging_dir='./logs',
load_best_model_at_end=True,
logging_steps=100,
evaluation_strategy="steps",
eval_steps=1000,
learning_rate=wandb.config.learning_rate,
weight_decay=wandb.config.weight_decay,
save_steps=1000,
save_total_limit=2,
metric_for_best_model='f1_macro',
gradient_accumulation_steps=wandb.config.gradient_accumulation,
# fp16=True
)
# Do not use custom class weights
# trainer = Trainer(
# model=model,
# args=training_args,
# train_dataset=dataset_train,
# eval_dataset=dataset_test,
# data_collator=data_collator,
# compute_metrics=compute_metrics,
# )
# Use custom class weights
trainer = CustomTrainer(
model=model,
args=training_args,
train_dataset=dataset_train,
eval_dataset=dataset_test,
data_collator=data_collator,
compute_metrics=compute_metrics,
)
trainer.train()
trainer.evaluate()
trainer.save_model(f'./models/{run_name}_sota')