-
Notifications
You must be signed in to change notification settings - Fork 7
/
Ensemble_models.py
714 lines (624 loc) · 32.7 KB
/
Ensemble_models.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import logging
import numpy as np
import torch
from sklearn.metrics import accuracy_score
from torch.utils.data import DataLoader
from tqdm import tqdm, trange
from transformers import get_linear_schedule_with_warmup, AdamW
from data import Dataset, SimpleDataset, prepare_data
from models import collate_for_mlp, collate_for_transformer
class SimplifiedStacking:
"""
A simple stacking model that uses two models and a meta model to predict the labels.
"""
def __init__(self, model1, model2, meta_model, is_m1_transformer, is_m2_transformer, is_mm_transformer, tokenizer1,
tokenizer2, tokenizer3):
self.model1 = model1
self.model2 = model2
self.meta_model = meta_model
self.is_m1_transformer = is_m1_transformer
self.is_m2_transformer = is_m2_transformer
self.is_mm_transformer = is_mm_transformer
self.tokenizer1 = tokenizer1
self.tokenizer2 = tokenizer2
self.tokenizer3 = tokenizer3
self.trained = False
def fit(self, dataset, batch_size, m1_lr, m2_lr, mm_lr, m1_weight_decay,
m2_weight_decay, mm_weight_decay, epochs, device, m1_num_warmup_steps=0, m2_num_warmup_steps=0,
mm_num_warmup_steps=0):
"""
Fit the models to the dataset, by training model1 normal, model2 on the misclassified examples of model1,
and the meta model decide which model to use.
:param dataset: Dataset object
:param batch_size: batch size
:param m1_lr: learning rate for model 1
:param m2_lr: learning rate for model 2
:param mm_lr: learning rate for meta model
:param m1_weight_decay: weight decay for model 1
:param m2_weight_decay: weight decay for model 2
:param mm_weight_decay: weight decay for meta model
:param epochs: number of epochs
:param device: device to use
:param m1_num_warmup_steps: number of warmup steps for model 1
:param m2_num_warmup_steps: number of warmup steps for model 2
:param mm_num_warmup_steps: number of warmup steps for meta model
:return: None
"""
logging.debug("Starting to fit")
# prerequisites
_, train_data, label_dict = prepare_data(dataset, self.tokenizer1,
Dataset if self.is_m1_transformer else SimpleDataset, shuffle=True)
train_loader_m1 = DataLoader(train_data,
collate_fn=collate_for_transformer if self.is_m1_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=False) # this has to be False, so the indexing is correct
optimiser_m1 = AdamW(self.model1.parameters(), lr=m1_lr, weight_decay=m1_weight_decay)
scheduler_m1 = get_linear_schedule_with_warmup(optimiser_m1, num_warmup_steps=m1_num_warmup_steps,
num_training_steps=len(train_loader_m1) * epochs)
optimiser_m2 = AdamW(self.model2.parameters(), lr=m2_lr, weight_decay=m2_weight_decay)
optimiser_mm = AdamW(self.meta_model.parameters(), lr=mm_lr, weight_decay=mm_weight_decay)
logging.debug("Starting to train")
# train
self.model1.train()
train_iterator = trange(epochs, desc="Epoch")
for epoch in train_iterator:
train_iterator.set_description(f"Epoch {epoch}")
correct_classified_inputs = []
misclassified_inputs = []
data_counter = 0
# region train model 1
epoch_iterator = tqdm(train_loader_m1, desc="Model 1 Iteration")
for batch in epoch_iterator:
batch = tuple(t.to(device) for t in batch)
if self.is_m1_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
outputs = self.model1(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.model1(flat_docs, offsets, labels)
inputs = {'input_ids': flat_docs,
'labels': labels}
loss = outputs[0]
logits = outputs[1]
# collect misclassified inputs and labels
for i in range(len(logits)):
if not torch.equal(inputs['labels'][i], torch.argmax(logits[i])):
misclassified_inputs.append(data_counter)
else:
correct_classified_inputs.append(data_counter)
data_counter += 1
loss.backward()
optimiser_m1.step()
scheduler_m1.step()
optimiser_m1.zero_grad()
# endregion
# region train model 2
logging.debug(f"Misclassified {len(misclassified_inputs)} inputs")
logging.debug(f"Correctly classified {len(correct_classified_inputs)} inputs")
# train model 2 on the missclassified inputs
if len(misclassified_inputs) > 0:
# select only the misclassified inputs
train_text, train_labels = dataset['train']
train_text = [train_text[i] for i in misclassified_inputs]
train_labels = [train_labels[i] for i in misclassified_inputs]
train_encodings = self.tokenizer2(train_text, truncation=True, padding=True)
train_labels_encoded = [label_dict[label] for label in train_labels]
m2_dataset = Dataset(train_encodings,
train_labels_encoded) if self.is_m2_transformer else SimpleDataset(
train_encodings,
train_labels_encoded)
train_loader_m2 = DataLoader(m2_dataset,
collate_fn=collate_for_transformer if self.is_m2_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=True)
scheduler_m2 = get_linear_schedule_with_warmup(optimiser_m2, num_warmup_steps=m2_num_warmup_steps,
num_training_steps=len(train_loader_m2) * epochs)
epoch_iterator2 = tqdm(train_loader_m2, desc="Model 2 Iteration")
for batch2 in epoch_iterator2:
batch2 = tuple(t.to(device) for t in batch2)
if self.is_m2_transformer:
inputs = {'input_ids': batch2[0],
'attention_mask': batch2[1],
'labels': batch2[2]}
outputs = self.model2(**inputs)
else:
flat_docs, offsets, labels = batch2
outputs = self.model1(flat_docs, offsets, labels)
loss = outputs[0]
loss.backward()
optimiser_m2.step()
optimiser_m2.step()
scheduler_m2.step()
optimiser_m2.zero_grad()
# endregion
# region train meta model to distinguish between correct and misclassified inputs
train_text, _ = dataset['train']
correct = [train_text[i] for i in correct_classified_inputs]
misclassified = [train_text[i] for i in misclassified_inputs]
X = correct + misclassified
X = self.tokenizer3(X, truncation=True, padding=True)
y = [0] * len(correct) + [1] * len(misclassified)
meta_dataset = Dataset(X, y) if self.is_mm_transformer else SimpleDataset(X, y)
train_loader_meta = DataLoader(meta_dataset,
collate_fn=collate_for_transformer if self.is_mm_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=True)
scheduler_mm = get_linear_schedule_with_warmup(optimiser_mm, num_warmup_steps=mm_num_warmup_steps,
num_training_steps=len(train_loader_meta) * epochs)
epoch_iterator3 = tqdm(train_loader_meta, desc="Meta Iteration")
for batch3 in epoch_iterator3:
batch3 = tuple(t.to(device) for t in batch3)
if self.is_mm_transformer:
inputs = {'input_ids': batch3[0],
'attention_mask': batch3[1],
'labels': batch3[2]}
outputs = self.meta_model(**inputs)
else:
flat_docs, offsets, labels = batch3
outputs = self.meta_model(flat_docs, offsets, labels)
loss = outputs[0]
loss.backward()
optimiser_mm.step()
scheduler_mm.step()
optimiser_mm.zero_grad()
# endregion
self.trained = True
def evaluate(self, dataset, batch_size, device):
assert self.trained, "Model not trained yet"
m1_data = []
m2_data = []
# region meta model decides which model to use
test_data, _, label_dict = prepare_data(dataset, self.tokenizer3,
Dataset if self.is_mm_transformer else SimpleDataset, shuffle=True)
self.meta_model.to(device)
data_counter = 0
data_loader_mm = DataLoader(test_data,
collate_fn=collate_for_transformer if self.is_mm_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=False) # this has to be False, so the indexing is correct
for batch in tqdm(data_loader_mm, desc="Meta Model"):
self.meta_model.eval()
batch = tuple(t.to(device) for t in batch)
with torch.no_grad():
if self.is_mm_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
outputs = self.meta_model(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.meta_model(flat_docs, offsets, labels)
loss, logits = outputs[:2]
for logit in logits:
if logit[0] > logit[1]:
m1_data.append(data_counter)
else:
m2_data.append(data_counter)
data_counter += 1
# endregion
logging.info(
f"Meta model decided to use model 1 for {len(m1_data)} inputs and model 2 for {len(m2_data)} inputs")
# region evaluate model 1
preds_m1 = []
labels_m1 = []
if len(m1_data) > 0:
test_data, test_labels = dataset['test']
label_dict = dataset['label_dict']
selected_test_data = [test_data[i] for i in m1_data]
selected_test_labels = [label_dict[test_labels[i]] for i in m1_data]
test_encodings = self.tokenizer1(selected_test_data, truncation=True, padding=True)
test_dataset = Dataset(test_encodings, selected_test_labels) if self.is_m1_transformer else SimpleDataset(
test_encodings,
selected_test_labels)
test_loader = DataLoader(test_dataset,
collate_fn=collate_for_transformer if self.is_m1_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=True)
self.model1.to(device)
self.model1.eval()
for batch in tqdm(test_loader, desc="Evaluate Model 1"):
batch = tuple(t.to(device) for t in batch)
with torch.no_grad():
if self.is_m1_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
labels = inputs['labels']
outputs = self.model1(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.model1(flat_docs, offsets, labels)
loss, logits = outputs[:2]
preds_m1.extend(logits.detach().cpu().numpy())
labels_m1.extend(labels.detach().cpu().numpy())
preds_m1 = np.argmax(preds_m1, axis=1)
# endregion
# region evaluate model 2
preds_m2 = []
labels_m2 = []
if len(m2_data) > 0:
test_data, test_labels = dataset['test']
label_dict = dataset['label_dict']
selected_test_data = [test_data[i] for i in m2_data]
selected_test_labels = [label_dict[test_labels[i]] for i in m2_data]
test_encodings = self.tokenizer2(selected_test_data, truncation=True, padding=True)
test_dataset = Dataset(test_encodings, selected_test_labels) if self.is_m2_transformer else SimpleDataset(
test_encodings,
selected_test_labels)
test_loader = DataLoader(test_dataset,
collate_fn=collate_for_transformer if self.is_m2_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=True)
self.model2.to(device)
self.model2.eval()
for batch in tqdm(test_loader, desc="Evaluate Model 2"):
batch = tuple(t.to(device) for t in batch)
with torch.no_grad():
if self.is_m2_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
labels = inputs['labels']
outputs = self.model2(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.model2(flat_docs, offsets, labels)
loss, logits = outputs[:2]
preds_m2.extend(logits.detach().cpu().numpy())
labels_m2.extend(labels.detach().cpu().numpy())
preds_m2 = np.argmax(preds_m2, axis=1)
# endregion
# calculate accuracy
preds = np.concatenate((preds_m1, preds_m2))
labels = np.concatenate((labels_m1, labels_m2))
acc = accuracy_score(labels, preds)
logging.info(f"Accuracy: {acc}")
return acc
class SimplifiedWeightedBoost:
"""
A simple ensemble model that trains the second model on the misclassified examples of the first model.
"""
def __init__(self, model1, model2, is_m1_transformer, is_m2_transformer, tokenizer1,
tokenizer2):
self.model1 = model1
self.model2 = model2
self.is_m1_transformer = is_m1_transformer
self.is_m2_transformer = is_m2_transformer
self.tokenizer1 = tokenizer1
self.tokenizer2 = tokenizer2
self.trained = False
def fit(self, dataset, batch_size, m1_lr, m2_lr, m1_weight_decay,
m2_weight_decay, epochs, device, m1_num_warmup_steps=0, m2_num_warmup_steps=0):
"""
Fit the models to the dataset, by training model1 normal and model2 on the misclassified examples of model1.
:param dataset: Dataset object
:param batch_size: batch size
:param m1_lr: learning rate for model 1
:param m2_lr: learning rate for model 2
:param m1_weight_decay: weight decay for model 1
:param m2_weight_decay: weight decay for model 2
:param epochs: number of epochs
:param device: device to use
:param m1_num_warmup_steps: number of warmup steps for model 1
:param m2_num_warmup_steps: number of warmup steps for model 2
:return: None
"""
logging.debug("Starting to fit")
# prerequisites
_, train_data, label_dict = prepare_data(dataset, self.tokenizer1,
Dataset if self.is_m1_transformer else SimpleDataset, shuffle=True)
train_loader_m1 = DataLoader(train_data,
collate_fn=collate_for_transformer if self.is_m1_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=False) # this has to be False, so the indexing is correct
optimiser_m1 = AdamW(self.model1.parameters(), lr=m1_lr, weight_decay=m1_weight_decay)
scheduler_m1 = get_linear_schedule_with_warmup(optimiser_m1, num_warmup_steps=m1_num_warmup_steps,
num_training_steps=len(train_loader_m1) * epochs)
optimiser_m2 = AdamW(self.model2.parameters(), lr=m2_lr, weight_decay=m2_weight_decay)
logging.debug("Starting to train")
# train
self.model1.train()
train_iterator = trange(epochs, desc="Epoch")
for epoch in train_iterator:
train_iterator.set_description(f"Epoch {epoch}")
correct_classified_inputs = []
misclassified_inputs = []
data_counter = 0
# region train model 1
epoch_iterator = tqdm(train_loader_m1, desc="Model 1 Iteration")
for batch in epoch_iterator:
batch = tuple(t.to(device) for t in batch)
if self.is_m1_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
outputs = self.model1(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.model1(flat_docs, offsets, labels)
inputs = {'input_ids': flat_docs,
'labels': labels}
loss = outputs[0]
logits = outputs[1]
# collect misclassified inputs and labels
for i in range(len(logits)):
if not torch.equal(inputs['labels'][i], torch.argmax(logits[i])):
misclassified_inputs.append(data_counter)
else:
correct_classified_inputs.append(data_counter)
data_counter += 1
loss.backward()
optimiser_m1.step()
scheduler_m1.step()
optimiser_m1.zero_grad()
# endregion
# region train model 2
logging.debug(f"Misclassified {len(misclassified_inputs)} inputs")
logging.debug(f"Correctly classified {len(correct_classified_inputs)} inputs")
# train model 2 on the missclassified inputs
if len(misclassified_inputs) > 0:
# select only the misclassified inputs
train_text, train_labels = dataset['train']
train_text = [train_text[i] for i in misclassified_inputs]
train_labels = [train_labels[i] for i in misclassified_inputs]
train_encodings = self.tokenizer2(train_text, truncation=True, padding=True)
train_labels_encoded = [label_dict[label] for label in train_labels]
m2_dataset = Dataset(train_encodings,
train_labels_encoded) if self.is_m2_transformer else SimpleDataset(
train_encodings,
train_labels_encoded)
train_loader_m2 = DataLoader(m2_dataset,
collate_fn=collate_for_transformer if self.is_m2_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=True)
scheduler_m2 = get_linear_schedule_with_warmup(optimiser_m2, num_warmup_steps=m2_num_warmup_steps,
num_training_steps=len(train_loader_m2) * epochs)
epoch_iterator2 = tqdm(train_loader_m2, desc="Model 2 Iteration")
for batch2 in epoch_iterator2:
batch2 = tuple(t.to(device) for t in batch2)
if self.is_m2_transformer:
inputs = {'input_ids': batch2[0],
'attention_mask': batch2[1],
'labels': batch2[2]}
outputs = self.model2(**inputs)
else:
flat_docs, offsets, labels = batch2
outputs = self.model1(flat_docs, offsets, labels)
loss = outputs[0]
loss.backward()
optimiser_m2.step()
optimiser_m2.step()
scheduler_m2.step()
optimiser_m2.zero_grad()
# endregion
self.trained = True
def evaluate(self, dataset, batch_size, device, alpha=0.5):
assert self.trained, "Model not trained yet"
# region evaluate model 1
test_data, _, label_dict = prepare_data(dataset, self.tokenizer1,
Dataset if self.is_m1_transformer else SimpleDataset,
shuffle=False)
self.model1.eval()
self.model1.to(device)
test_loader = DataLoader(test_data,
collate_fn=collate_for_transformer if self.is_m1_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=False) # this has to be False, so the indexing is correct
preds_m1 = []
self.model1.to(device)
self.model1.eval()
for batch in tqdm(test_loader, desc="Evaluate Model 1"):
batch = tuple(t.to(device) for t in batch)
with torch.no_grad():
if self.is_m1_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
outputs = self.model1(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.model1(flat_docs, offsets, labels)
loss, logits = outputs[:2]
preds_m1.extend(logits.detach().cpu().numpy())
# endregion
# region evaluate model 2
test_data, _, label_dict = prepare_data(dataset, self.tokenizer2,
Dataset if self.is_m2_transformer else SimpleDataset, shuffle=False)
self.model2.eval()
self.model2.to(device)
test_loader = DataLoader(test_data,
collate_fn=collate_for_transformer if self.is_m2_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=False) # this has to be False, so the indexing is correct
preds_m2 = []
for batch in tqdm(test_loader, desc="Evaluate Model 2"):
batch = tuple(t.to(device) for t in batch)
with torch.no_grad():
if self.is_m2_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
outputs = self.model2(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.model2(flat_docs, offsets, labels)
loss, logits = outputs[:2]
preds_m2.extend(logits.detach().cpu().numpy())
# endregion
# region combine predictions with alpha
_, test_labels = dataset['test']
test_labels_encoded = [label_dict[label] for label in test_labels]
preds_m1 = np.array(preds_m1)
preds_m2 = np.array(preds_m2)
preds = alpha * preds_m1 + (1 - alpha) * preds_m2
preds = np.argmax(preds, axis=1)
# endregion
# calculate accuracy
acc = accuracy_score(test_labels_encoded, preds)
logging.info(f"Accuracy: {acc}")
return acc
class WeightedEnsemble:
"""
A simple ensemble model that combines the predictions of two models with a given weight.
"""
def __init__(self, model1, model2, is_m1_transformer, is_m2_transformer, tokenizer1,
tokenizer2):
self.model1 = model1
self.model2 = model2
self.is_m1_transformer = is_m1_transformer
self.is_m2_transformer = is_m2_transformer
self.tokenizer1 = tokenizer1
self.tokenizer2 = tokenizer2
self.trained = False
def fit(self, dataset, batch_size, m1_lr, m2_lr, m1_weight_decay,
m2_weight_decay, m1_epochs, m2_epochs, device, m1_num_warmup_steps=0, m2_num_warmup_steps=0):
"""
Fit the models to the dataset, by training each model separately and then combining the predictions
:param dataset: Dataset object
:param batch_size: batch size
:param m1_lr: learning rate for model 1
:param m2_lr: learning rate for model 2
:param m1_weight_decay: weight decay for model 1
:param m2_weight_decay: weight decay for model 2
:param m1_epochs: number of epochs for model 1
:param m2_epochs: number of epochs for model 2
:param device: device to use
:param m1_num_warmup_steps: number of warmup steps for model 1
:param m2_num_warmup_steps: number of warmup steps for model 2
:return: None
"""
logging.debug("Starting to fit")
# prerequisites
_, train_data, label_dict = prepare_data(dataset, self.tokenizer1,
Dataset if self.is_m1_transformer else SimpleDataset)
train_loader_m1 = DataLoader(train_data,
collate_fn=collate_for_transformer if self.is_m1_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=True)
optimiser_m1 = AdamW(self.model1.parameters(), lr=m1_lr, weight_decay=m1_weight_decay)
scheduler_m1 = get_linear_schedule_with_warmup(optimiser_m1, num_warmup_steps=m1_num_warmup_steps,
num_training_steps=len(train_loader_m1) * m1_epochs)
_, train_data, label_dict = prepare_data(dataset, self.tokenizer2,
Dataset if self.is_m2_transformer else SimpleDataset)
train_loader_m2 = DataLoader(train_data,
collate_fn=collate_for_transformer if self.is_m2_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=True)
optimiser_m2 = AdamW(self.model2.parameters(), lr=m2_lr, weight_decay=m2_weight_decay)
scheduler_m2 = get_linear_schedule_with_warmup(optimiser_m2, num_warmup_steps=m2_num_warmup_steps,
num_training_steps=len(train_loader_m1) * m2_epochs)
logging.debug("Starting to train")
# region train model 1
self.model1.train()
train_iterator = trange(m1_epochs, desc="Model 1 Epoch")
for epoch in train_iterator:
train_iterator.set_description(f"Model 1 Epoch {epoch}")
epoch_iterator = tqdm(train_loader_m1, desc="Model 1 Iteration")
for batch in epoch_iterator:
batch = tuple(t.to(device) for t in batch)
if self.is_m1_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
outputs = self.model1(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.model1(flat_docs, offsets, labels)
loss = outputs[0]
loss.backward()
optimiser_m1.step()
scheduler_m1.step()
optimiser_m1.zero_grad()
# endregion
logging.info("Finished training model 1")
# region train model 2
self.model2.train()
train_iterator = trange(m2_epochs, desc="Model 2 Epoch")
for epoch in train_iterator:
train_iterator.set_description(f"Model 2 Epoch {epoch}")
epoch_iterator = tqdm(train_loader_m2, desc="Model 2 Iteration")
for batch in epoch_iterator:
batch = tuple(t.to(device) for t in batch)
if self.is_m2_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
outputs = self.model2(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.model2(flat_docs, offsets, labels)
loss = outputs[0]
loss.backward()
optimiser_m2.step()
scheduler_m2.step()
optimiser_m2.zero_grad()
# endregion
logging.info("Finished training model 2")
self.trained = True
def evaluate(self, dataset, batch_size, device, alpha=0.5):
assert self.trained, "Model not trained yet"
# region evaluate model 1
test_data, _, label_dict = prepare_data(dataset, self.tokenizer1,
Dataset if self.is_m1_transformer else SimpleDataset,
shuffle=False)
self.model1.eval()
self.model1.to(device)
test_loader = DataLoader(test_data,
collate_fn=collate_for_transformer if self.is_m1_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=False) # this has to be False, so the indexing is correct
preds_m1 = []
self.model1.to(device)
self.model1.eval()
for batch in tqdm(test_loader, desc="Evaluate Model 1"):
batch = tuple(t.to(device) for t in batch)
with torch.no_grad():
if self.is_m1_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
outputs = self.model1(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.model1(flat_docs, offsets, labels)
loss, logits = outputs[:2]
preds_m1.extend(logits.detach().cpu().numpy())
# endregion
# region evaluate model 2
test_data, _, label_dict = prepare_data(dataset, self.tokenizer2,
Dataset if self.is_m2_transformer else SimpleDataset, shuffle=False)
self.model2.eval()
self.model2.to(device)
test_loader = DataLoader(test_data,
collate_fn=collate_for_transformer if self.is_m2_transformer else collate_for_mlp,
batch_size=batch_size,
shuffle=False) # this has to be False, so the indexing is correct
preds_m2 = []
for batch in tqdm(test_loader, desc="Evaluate Model 2"):
batch = tuple(t.to(device) for t in batch)
with torch.no_grad():
if self.is_m2_transformer:
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2]}
outputs = self.model2(**inputs)
else:
flat_docs, offsets, labels = batch
outputs = self.model2(flat_docs, offsets, labels)
loss, logits = outputs[:2]
preds_m2.extend(logits.detach().cpu().numpy())
# endregion
# region combine predictions with alpha
_, test_labels = dataset['test']
test_labels_encoded = [label_dict[label] for label in test_labels]
preds_m1 = np.array(preds_m1)
preds_m2 = np.array(preds_m2)
preds = alpha * preds_m1 + (1 - alpha) * preds_m2
preds = np.argmax(preds, axis=1)
# endregion
# calculate accuracy
acc = accuracy_score(test_labels_encoded, preds)
logging.info(f"Accuracy: {acc}")
return acc