-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetune_classify.py
377 lines (310 loc) · 13.7 KB
/
finetune_classify.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import torch
import os
from transformers import BertForSequenceClassification, RobertaForSequenceClassification, \
AlbertForSequenceClassification, XLNetForSequenceClassification, CamembertForSequenceClassification, \
FlaubertForSequenceClassification, AdamW, get_linear_schedule_with_warmup
from sklearn.metrics import classification_report
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
import torch.nn.functional as F
from torch.autograd import Variable
import random
from utils import read_sents, tokenize_and_pad, format_time, flat_accuracy, decode_result
from configure import parse_args
import numpy as np
import time
if not os.path.exists('logs'):
os.makedirs('logs')
if not os.path.exists('checkpoints'):
os.makedirs('checkpoints')
args = parse_args()
# argparse doesn't deal in absolutes, so we pass a str flag & convert
if args.verb_segment_ids == 'yes':
use_segment_ids = True
else:
use_segment_ids = False
print('Uses verb segment ids: ' + args.verb_segment_ids)
print('Model: ' + args.transformer_model)
if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
transformer_model = args.transformer_model
epochs = args.num_epochs
# read sentences, choose labels of telicity/duration
train_sentences, train_labels, val_sentences, val_labels, \
test_sentences, test_labels = read_sents(args.data_path, args.label_marker)
# make input ids, attention masks, segment ids
train_inputs, train_masks, train_segments = tokenize_and_pad(train_sentences)
val_inputs, val_masks, val_segments = tokenize_and_pad(val_sentences)
test_inputs, test_masks, test_segments = tokenize_and_pad(test_sentences)
print('Train set: ' + str(len(train_inputs)))
print('Validation set: ' + str(len(val_inputs)))
print('Test set: ' + str(len(test_inputs)))
train_inputs = torch.tensor(train_inputs)
val_inputs = torch.tensor(val_inputs)
test_inputs = torch.tensor(test_inputs)
train_labels = torch.tensor(train_labels)
val_labels = torch.tensor(val_labels)
test_labels = torch.tensor(test_labels)
if use_segment_ids:
train_segments = torch.tensor(train_segments)
val_segments = torch.tensor(val_segments)
test_segments = torch.tensor(test_segments)
train_masks = torch.tensor(train_masks)
val_masks = torch.tensor(val_masks)
test_masks = torch.tensor(test_masks)
# Create the DataLoader for our training set.
batch_size = args.batch_size
if use_segment_ids:
train_data = TensorDataset(train_inputs, train_masks, train_labels, train_segments)
else:
train_data = TensorDataset(train_inputs, train_masks, train_labels)
train_sampler = RandomSampler(train_data)
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size)
# Create the DataLoader for our validation set.
if use_segment_ids:
val_data = TensorDataset(val_inputs, val_masks, val_labels, val_segments)
else:
val_data = TensorDataset(val_inputs, val_masks, val_labels)
val_sampler = SequentialSampler(val_data)
val_dataloader = DataLoader(val_data, sampler=val_sampler, batch_size=batch_size)
# Create the DataLoader for our test set.
if use_segment_ids:
test_data = TensorDataset(test_inputs, test_masks, test_labels, test_segments)
else:
test_data = TensorDataset(test_inputs, test_masks, test_labels)
test_sampler = SequentialSampler(test_data)
test_dataloader = DataLoader(test_data, sampler=test_sampler, batch_size=batch_size)
if args.training == 'yes':
if (args.transformer_model).split("-")[0] == 'bert':
model = BertForSequenceClassification.from_pretrained(
transformer_model,
num_labels = 2,
output_attentions = True,
output_hidden_states = False,
)
elif (args.transformer_model).split("-")[0] == 'roberta':
model = RobertaForSequenceClassification.from_pretrained(
transformer_model,
num_labels = 2,
output_attentions = True,
output_hidden_states = False,
)
elif (args.transformer_model).split("-")[0] == 'albert':
model = AlbertForSequenceClassification.from_pretrained(
transformer_model,
num_labels = 2,
output_attentions = True,
output_hidden_states = False,
)
elif (args.transformer_model).split("-")[0] == 'xlnet':
model = XLNetForSequenceClassification.from_pretrained(
transformer_model,
num_labels = 2,
output_attentions = True,
output_hidden_states = False,
)
elif 'flaubert' in args.transformer_model:
model = FlaubertForSequenceClassification.from_pretrained(
transformer_model,
num_labels = 2,
output_attentions = True,
output_hidden_states = False,
)
elif 'camembert' in args.transformer_model:
model = CamembertForSequenceClassification.from_pretrained(
transformer_model,
num_labels = 2,
output_attentions = True,
output_hidden_states = False,
)
if torch.cuda.is_available():
model.cuda()
optimizer = AdamW(model.parameters(),
lr = 2e-5,
eps = 1e-8
)
total_steps = len(train_dataloader) * epochs
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps = 0,
num_training_steps = total_steps)
seed_val = 42
random.seed(seed_val)
np.random.seed(seed_val)
torch.manual_seed(seed_val)
torch.cuda.manual_seed_all(seed_val)
loss_values = []
for epoch_i in range(0, epochs):
print('\t======== Epoch {:} / {:} ========'.format(epoch_i + 1, epochs))
t0 = time.time()
total_loss = 0
model.train()
for step, batch in enumerate(train_dataloader):
if step % 40 == 0 and not step == 0:
# Calculate elapsed time in minutes.
elapsed = format_time(time.time() - t0)
print('\tBatch {:>5,}\tof\t{:>5,}.\t\tElapsed: {:}.'.format(step, len(train_dataloader), elapsed))
b_input_ids = batch[0].to(device)
b_input_mask = batch[1].to(device)
b_labels = batch[2].to(device)
if use_segment_ids:
b_segments = batch[3].to(device)
model.zero_grad()
if use_segment_ids:
outputs = model(b_input_ids,
token_type_ids=b_segments,
attention_mask=b_input_mask,
labels=b_labels,
output_hidden_states=True)
else:
outputs = model(b_input_ids,
token_type_ids=None,
attention_mask=b_input_mask,
labels=b_labels,
output_hidden_states=True)
loss = outputs[0]
total_loss += loss.item()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
scheduler.step()
avg_train_loss = total_loss / len(train_dataloader)
loss_values.append(avg_train_loss)
print('\tAverage training loss: {0:.2f}'.format(avg_train_loss))
print('\tTraining epoch took: {:}'.format(format_time(time.time() - t0)))
# ========================================
# Validation
# ========================================
print('\tRunning Validation...')
t0 = time.time()
model.eval()
eval_loss, eval_accuracy = 0, 0
nb_eval_steps, nb_eval_examples = 0, 0
all_labels = []
all_preds = []
for batch in val_dataloader:
batch = tuple(t.to(device) for t in batch)
if use_segment_ids:
b_input_ids, b_input_mask, b_labels, b_segments = batch
else:
b_input_ids, b_input_mask, b_labels = batch
with torch.no_grad():
if use_segment_ids:
outputs = model(b_input_ids,
token_type_ids=b_segments,
attention_mask=b_input_mask)
else:
outputs = model(b_input_ids,
token_type_ids=None,
attention_mask=b_input_mask)
logits = outputs[0]
logits = logits.detach().cpu().numpy()
label_ids = b_labels.to('cpu').numpy()
tmp_eval_accuracy = flat_accuracy(label_ids, logits)
eval_accuracy += tmp_eval_accuracy
nb_eval_steps += 1
all_labels += label_ids.tolist()
all_preds += np.argmax(logits, axis=1).flatten().tolist()
assert len(all_labels) == len(all_preds)
print('\tAccuracy: {0:.2f}'.format(eval_accuracy/nb_eval_steps))
print('\tValidation took: {:}'.format(format_time(time.time() - t0)))
print('\tConfusion matrix:\n')
print(classification_report(all_labels, all_preds))
# Save the model on epoch 4
output_model_dir = 'checkpoints/' + args.data_path[5:] + '/' + args.label_marker + '/' + \
args.transformer_model.split('/')[-1] + '_' + args.verb_segment_ids
model.save_pretrained(output_model_dir)
# ========================================
# TESTING
# ========================================
# Load the model of the last epoch
if (args.transformer_model).split("-")[0] == 'bert':
model = BertForSequenceClassification.from_pretrained(
'checkpoints/' + args.data_path[5:] + '/' + \
args.label_marker + '/' + args.transformer_model.split('/')[-1] + \
'_' + args.verb_segment_ids)
elif (args.transformer_model).split("-")[0] == 'roberta':
model = RobertaForSequenceClassification.from_pretrained(
'checkpoints/' + args.data_path[5:] + '/' + \
args.label_marker + '/' + args.transformer_model.split('/')[-1] + \
'_' + args.verb_segment_ids)
elif (args.transformer_model).split("-")[0] == 'albert':
model = AlbertForSequenceClassification.from_pretrained(
'checkpoints/' + args.data_path[5:] + '/' + \
args.label_marker + '/' + args.transformer_model.split('/')[-1] + \
'_' + args.verb_segment_ids)
elif (args.transformer_model).split("-")[0] == 'xlnet':
model = XLNetForSequenceClassification.from_pretrained(
'checkpoints/' + args.data_path[5:] + '/' + \
args.label_marker + '/' + args.transformer_model.split('/')[-1] + \
'_' + args.verb_segment_ids)
elif 'flaubert' in args.transformer_model:
model = FlaubertForSequenceClassification.from_pretrained(
'checkpoints/' + args.data_path[5:] + '/' + \
args.label_marker + '/' + args.transformer_model.split('/')[-1] + \
'_' + args.verb_segment_ids)
elif 'camembert' in args.transformer_model:
model = CamembertForSequenceClassification.from_pretrained(
'checkpoints/' + args.data_path[5:] + '/' + \
args.label_marker + '/' + args.transformer_model.split('/')[-1] + \
'_' + args.verb_segment_ids)
if torch.cuda.is_available():
model.cuda()
print('Loaded model succesful. Running testing...')
t0 = time.time()
model.eval()
test_loss, test_accuracy = 0, 0
nb_test_steps, nb_test_examples = 0, 0
all_inputs = []
all_labels = []
all_preds = []
all_probs = []
for batch in test_dataloader:
batch = tuple(t.to(device) for t in batch)
if use_segment_ids:
b_input_ids, b_input_mask, b_labels, b_segments = batch
else:
b_input_ids, b_input_mask, b_labels = batch
with torch.no_grad():
if use_segment_ids:
outputs = model(b_input_ids,
token_type_ids=b_segments,
attention_mask=b_input_mask)
else:
outputs = model(b_input_ids,
token_type_ids=None,
attention_mask=b_input_mask)
logits = outputs[0]
logits = logits.detach().cpu().numpy()
label_ids = b_labels.to('cpu').numpy()
log_probs = F.softmax(Variable(torch.from_numpy(logits)), dim=-1)
tmp_test_accuracy = flat_accuracy(label_ids, logits)
test_accuracy += tmp_test_accuracy
nb_test_steps += 1
all_inputs += b_input_ids.to('cpu').numpy().tolist()
all_labels += label_ids.tolist()
all_preds += np.argmax(logits, axis=1).flatten().tolist()
all_probs += log_probs.tolist()
#assert len(all_inputs) == len(all_labels) == len(all_preds)
# Report the accuracy, the sentences
print('Accuracy: {0:.2f}'.format(test_accuracy/nb_test_steps))
print('Confusion matrix:\n')
print(classification_report(all_labels, all_preds))
# Uncomment the following to see the decoded sentences
print('\nWrong predictions:')
counter = 0
for n, sent in enumerate(all_inputs):
if all_labels[n] != all_preds[n]:
counter += 1
#sentence = decode_result(sent)
#probs = all_probs[n]
#print('Predicted: ', all_preds[n], '\tProbs: ', str(probs), '\tSent: ', sentence)
print(str(counter) + ' out of ' + str(len(all_inputs)))
# Uncomment to see the right predictions
print('\nRight predictions:')
#for n, sent in enumerate(all_inputs):
# if all_labels[n] == all_preds[n]:
# counter += 1
# sentence = decode_result(sent)
# print('Predicted: ', '\tProbs: ', '\tSent: ', sentence)
print(str(len(all_inputs) - counter) + ' out of ' + str(len(all_inputs)))