-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbert_finetune_OLID.py
255 lines (184 loc) · 7.13 KB
/
bert_finetune_OLID.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import torch
import os, sys, argparse
import numpy as np
from torch.utils.data import Dataset, DataLoader
from transformers import AutoModel, AutoTokenizer, AutoModelForSequenceClassification, AutoConfig
from sklearn.metrics import f1_score, precision_score, recall_score
from data_utils_OLID import OLID
parser = argparse.ArgumentParser(description='BERT')
parser.add_argument("--logs_dir", default='english', type=str)
parser.add_argument("--language", default='english', type=str)
parser.add_argument("--batch_size", default=64, type=int)
parser.add_argument("--learning_rate", default=2e-5, type=float)
parser.add_argument("--weight_decay", default=1e-2, type=float)
parser.add_argument("--epochs", default=50, type=int)
parser.add_argument("--subtask", default='subtask_a', type=str)
args = parser.parse_args()
args.logs_dir += f'/{args.language}_{args.subtask}'
if not os.path.exists(args.logs_dir):
os.mkdir(args.logs_dir)
print(f'{args.logs_dir} created!')
logs_file = os.path.join(args.logs_dir, 'logs.txt')
def logprint(log):
print(log, end='')
with open(logs_file, 'a') as f:
f.write(log)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Define the model repo
# model_name = "xlm-roberta-base"
model_names = {
'english': "bert-base-uncased",
'danish': "Maltehb/danish-bert-botxo",
'turkish': "dbmdz/bert-base-turkish-cased",
'arabic': "asafaya/bert-base-arabic",
'greek': "nlpaueb/bert-base-greek-uncased-v1"
}
model_name = model_names[args.language]
if args.subtask in ['subtask_a', 'subtask_b']:
num_labels = 2
elif args.subtask == 'subtask_c':
num_labels = 3
else:
raise Exception()
# tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name)
config = AutoConfig.from_pretrained(model_name, num_labels=num_labels)
model = AutoModelForSequenceClassification.from_pretrained(model_name, config=config)
# Download pytorch model
# model = XLMRobertaForSequenceClassification.from_pretrained(model_name,
# num_labels = 2, # The number of output labels.
# output_attentions = False, # Whether the model returns attentions weights.
# output_hidden_states = False, # Whether the model returns all hidden-states.
# )
model.cuda()
def warmp_up(lr, epoch, warmup_epochs):
lr = lr * (epoch / warmup_epochs)
return lr
def step_down(lr, epoch, step_down_epochs):
lr = (1 - epoch / step_down_epochs) * lr
return lr
dataroot = '/content/drive/MyDrive/data/'
lang = args.language
batch_size = args.batch_size
dataset = OLID(dataroot, 'train', lang, subtask=args.subtask)
trainDataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
dataset = OLID(dataroot, 'test', lang, subtask=args.subtask)
testDataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
for idx, item in enumerate(trainDataloader):
inputs = item['input']
labels = item['label']
print('inputs:', inputs)
print('labels:', labels)
break
weight_decay = args.weight_decay
learning_rate = args.learning_rate
optimizer = torch.optim.AdamW(model.parameters(),
lr = learning_rate,
eps = 1e-8,
weight_decay=weight_decay,
)
epochs = args.epochs
lr_warmup_epochs = 0.1 * epochs
init_lr = 0
def validate_model(dataloader, subtask):
model.eval()
running_loss = 0.
matches = 0.
samples = 0.
running_labels = []
running_preds = []
for idx, item in enumerate(dataloader):
inputs = item['input']
labels = item['label']
input_ids = tokenizer(inputs, padding=True, truncation=True, max_length=64, return_tensors='pt')
model_input = input_ids['input_ids'].cuda()
attention_masks = input_ids['attention_mask'].cuda()
labels = labels.to(device)
with torch.no_grad():
outputs = model(model_input,
token_type_ids=None,
attention_mask=attention_masks,
labels=labels)
loss = outputs.loss
logits = outputs.logits
running_loss += loss.item()
preds = torch.argmax(logits, dim=1)
running_labels.extend( list(labels.detach().cpu().numpy()) )
running_preds.extend( list(preds.detach().cpu().numpy()) )
running_loss += loss.item()
matches += torch.sum(preds == labels).item()
samples += labels.shape[0]
torch.cuda.empty_cache()
# break
# print(running_labels)
if subtask == 'subtask_c':
average = 'macro'
elif subtask in ['subtask_a', 'subtask_b']:
average = 'binary'
else:
raise Exception()
# import pdb; pdb.set_trace()
precision = precision_score(running_labels, running_preds, average=average)
recall = recall_score(running_labels, running_preds, average=average)
f1 = f1_score(running_labels, running_preds, average=average)
running_loss /= len(dataloader)
running_acc = matches / samples
return precision, recall, f1, running_loss, running_acc
def train_model(dataloader):
model.train()
running_loss = 0.
matches = 0.
samples = 0.
for idx, item in enumerate(dataloader):
inputs = item['input']
labels = item['label']
input_ids = tokenizer(inputs, padding=True, truncation=True, max_length=64, return_tensors='pt')
model_input = input_ids['input_ids'].to(device)
attention_masks = input_ids['attention_mask'].to(device)
labels = labels.to(device)
model.zero_grad()
outputs = model(model_input,
token_type_ids=None,
attention_mask=attention_masks,
labels=labels)
loss = outputs.loss
loss.backward()
optimizer.step()
logits = outputs.logits
running_loss += loss.item()
preds = torch.argmax(logits, dim=1)
matches += torch.sum(preds == labels).item()
samples += preds.shape[0]
torch.cuda.empty_cache()
# break
running_loss /= len(dataloader)
running_acc = matches / samples
return running_loss, running_acc
best_val_f1 = 0.
for epoch_i in range(epochs):
logprint("\n")
logprint('======== Epoch {:} / {:} ========\n'.format(epoch_i + 1, epochs))
logprint('Training...\n')
if epoch_i < lr_warmup_epochs:
lr = warmp_up(learning_rate, epoch_i, lr_warmup_epochs)
else:
lr = step_down(learning_rate, epoch_i - lr_warmup_epochs, epochs - lr_warmup_epochs)
for g in optimizer.param_groups:
g['lr'] = lr
train_loss, train_acc = train_model(trainDataloader)
logprint(f"Total loss: {train_loss} Train Acc: {train_acc}\n")
precision, recall, f1, val_loss, val_acc = validate_model(testDataloader, subtask=args.subtask)
logprint(f"Validation Loss: {val_loss}: Acc: {val_acc}\n")
logprint(f'Validation Precision:{precision} Recall: {recall} F1: {f1}\n')
if f1 > best_val_f1:
best_val_f1 = f1
logprint(f'best epoch: {epoch_i+1} best f1: {f1}\n')
logprint(f'{precision}\t{recall}\t{f1}\t{val_acc}')
save_dict = {
'best_model_wts' : model.state_dict(),
'best_f1' : best_val_f1,
'precision': precision,
'recall': recall
}
save_model_path = os.path.join(args.logs_dir, f'bert_{args.language}.pth')
torch.save(save_dict, save_model_path)