-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
763 lines (662 loc) · 33.6 KB
/
main.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
"""
Description: Run text classification on the given dataset with the given model.
"""
import json
import logging
import os
import numpy as np
import torch
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from torch.utils.data import DataLoader
from tqdm import tqdm, trange
from transformers import get_linear_schedule_with_warmup, AdamW, AutoModelForSequenceClassification, Trainer, \
TrainingArguments, AutoTokenizer
from ensemble_models import SimplifiedStacking, SimplifiedWeightedBoost, WeightedEnsemble
from data import Dataset, SimpleDataset, load_data, load_pretrained_embeddings, build_tokenizer_for_word_embeddings, \
prepare_data_custom_tokenizer, prepare_data
from models import MLP, collate_for_mlp, LSTM, collate_for_lstm
try:
import wandb
WANDB = True
except ImportError:
logging.info("Wandb not installed. Skipping tracking.")
WANDB = False
MODELS = {
"BERT": "bert-base-uncased",
"ROBERTA": "roberta-base",
"DEBERTA": "microsoft/deberta-base",
"MLP": "bert-base-uncased",
"ERNIE": "nghuyong/ernie-2.0-base-en",
"DISTILBERT": "distilbert-base-uncased",
"ALBERT": "albert-base-v2",
"LSTM": "bert-base-uncased",
}
VALID_DATASETS = ['MR', 'R8', 'SearchSnippets', 'Twitter', 'TREC', 'SST2', 'NICE', 'NICE2', 'STOPS', 'STOPS2']
VALID_MODELS = list(MODELS.keys()) + ["STACKING", "WEIGHTED_BOOST", "WEIGHTED"]
def compute_metrics(pred):
"""
Compute the metrics for the given predictions.
:param pred: the predictions
:return: accuracy
"""
labels = pred.label_ids
preds = pred.predictions.argmax(-1)
# f1 = f1_score(labels, preds, average="weighted")
acc = accuracy_score(labels, preds)
return {"accuracy": acc}
def train_transformer(model, dataset, output_dir, training_batch_size, eval_batch_size, learning_rate,
num_train_epochs, weight_decay,
disable_tqdm=False):
"""
Train and fine-tune the model using HuggingFace's PyTorch implementation and the Trainer API.
"""
# training params
model_ckpt = MODELS[model]
print(model_ckpt)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
output = f"{output_dir}/{model_ckpt}-finetuned-{dataset['name']}"
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
# max length of 512 for ERNIE as it is not predefined in the model
if model == "ERNIE":
test_data, train_data, label_dict = prepare_data(dataset, tokenizer, Dataset, max_length=512)
else:
test_data, train_data, label_dict = prepare_data(dataset, tokenizer, Dataset)
model = AutoModelForSequenceClassification.from_pretrained(model_ckpt, num_labels=len(label_dict)).to(device)
logging_steps = len(train_data) // training_batch_size
# train
if WANDB:
wandb.watch(model)
training_args = TrainingArguments(output_dir=output,
num_train_epochs=num_train_epochs,
learning_rate=learning_rate,
per_device_train_batch_size=training_batch_size,
per_device_eval_batch_size=eval_batch_size,
weight_decay=weight_decay,
evaluation_strategy="epoch",
disable_tqdm=disable_tqdm,
logging_steps=logging_steps,
log_level="error",
logging_dir="./logs",
report_to="wandb")
else:
training_args = TrainingArguments(output_dir=output,
num_train_epochs=num_train_epochs,
learning_rate=learning_rate,
per_device_train_batch_size=training_batch_size,
per_device_eval_batch_size=eval_batch_size,
weight_decay=weight_decay,
evaluation_strategy="epoch",
disable_tqdm=disable_tqdm,
logging_steps=logging_steps,
log_level="error",
logging_dir="./logs")
trainer = Trainer(model=model,
args=training_args,
train_dataset=train_data,
eval_dataset=test_data,
compute_metrics=compute_metrics,
tokenizer=tokenizer)
trainer.train()
evaluate_trainer(trainer, test_data, output_dir)
# save model
model.save_pretrained(f"{output}/model")
def evaluate_trainer(trainer, test_data, output_dir):
"""
Evaluate the fine-tuned trainer on the test set.
Therefore, the accuracy is computed and a confusion matrix is generated.
"""
# accuracy
prediction_output = trainer.predict(test_data)
logging.info(f"Prediction metrics: {prediction_output.metrics}")
# confusion matrix
y_preds = np.argmax(prediction_output.predictions, axis=1)
y_true = prediction_output.label_ids
cm = confusion_matrix(y_true, y_preds)
logging.info(f"Confusion matrix:\n{cm}")
# create file if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# save results to file
with open(f"{output_dir}/eval_results.json", "a") as f:
f.write("\n")
json.dump(prediction_output.metrics, f)
if WANDB:
wandb.log(prediction_output.metrics)
def train_mlp(dataset, output_dir, epochs, warmup_steps, learning_rate, weight_decay, gradient_accumulation_steps,
training_batch_size, eval_batch_size, dropout, hidden_size, num_hidden_layers, pretrained_embedding_path,
freeze_embedding):
"""
Train a simple MLP model.
"""
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
output = f"{output_dir}/mlp-finetuned-{dataset['name']}"
if (pretrained_embedding_path is not None) and (pretrained_embedding_path != ""):
logging.info("Loading pretrained embedding")
vocab, embedding = load_pretrained_embeddings(pretrained_embedding_path, unk_token="[UNK]")
logging.debug(f"Vocab size: {len(vocab)}")
logging.debug(f"Embedding size: {embedding.shape}")
tokenizer = build_tokenizer_for_word_embeddings(vocab, "[UNK]")
test_data, train_data, label_dict = prepare_data_custom_tokenizer(dataset, tokenizer, SimpleDataset)
vocab_size = len(vocab)
else:
logging.info("Loading tokenizer")
tokenizer = AutoTokenizer.from_pretrained(MODELS["MLP"])
test_data, train_data, label_dict = prepare_data(dataset, tokenizer, SimpleDataset)
vocab_size = tokenizer.vocab_size
embedding = None
train_loader = DataLoader(train_data,
collate_fn=collate_for_mlp,
shuffle=True,
batch_size=training_batch_size)
model = MLP(vocab_size,
len(label_dict),
dropout=dropout,
hidden_size=hidden_size,
num_hidden_layers=num_hidden_layers,
pretrained_embedding=embedding,
freeze=freeze_embedding
).to(device)
train_model(device, epochs, eval_batch_size, gradient_accumulation_steps, learning_rate, model, test_data,
train_data, train_loader, warmup_steps, weight_decay, output)
def train_lstm(dataset, output_dir, epochs, warmup_steps, learning_rate, weight_decay, gradient_accumulation_steps,
training_batch_size, eval_batch_size, bidirectional, dropout, num_layers, hidden_size,
pretrained_embedding_path, freeze_embedding):
"""
Train a simple LSTM model.
"""
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
output = f"{output_dir}/lstm-finetuned-{dataset['name']}"
if (pretrained_embedding_path is not None) and (pretrained_embedding_path != ""):
logging.info("Loading pretrained embedding")
vocab, embedding = load_pretrained_embeddings(pretrained_embedding_path, unk_token="[UNK]")
logging.debug(f"Vocab size: {len(vocab)}")
logging.debug(f"Embedding size: {embedding.shape}")
tokenizer = build_tokenizer_for_word_embeddings(vocab, "[UNK]")
test_data, train_data, label_dict = prepare_data_custom_tokenizer(dataset, tokenizer, SimpleDataset)
vocab_size = len(vocab)
else:
logging.info("Loading tokenizer")
tokenizer = AutoTokenizer.from_pretrained(MODELS["LSTM"])
test_data, train_data, label_dict = prepare_data(dataset, tokenizer, SimpleDataset)
vocab_size = tokenizer.vocab_size
embedding = None
# change collate?
train_loader = DataLoader(train_data,
collate_fn=collate_for_lstm,
shuffle=True,
batch_size=training_batch_size)
model = LSTM(vocab_size,
len(label_dict),
bidirectional=bidirectional,
dropout=dropout,
num_layers=num_layers,
hidden_size=hidden_size,
pretrained_embedding=embedding,
freeze=freeze_embedding
).to(device)
train_model(device, epochs, eval_batch_size, gradient_accumulation_steps, learning_rate, model, test_data,
train_data, train_loader, warmup_steps, weight_decay, output)
def train_model(device, epochs, eval_batch_size, gradient_accumulation_steps, learning_rate, model, test_data,
train_data, train_loader, warmup_steps, weight_decay, output_dir):
"""
Train a PyTorch model with the specified parameters.
Used by both the MLP and LSTM model.
"""
if WANDB:
wandb.watch(model)
optimizer = AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)
t_total = len(train_loader) // gradient_accumulation_steps * epochs
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=warmup_steps,
num_training_steps=t_total)
logging.info(f"Training model on {device}")
global_step = 0
training_loss, logging_loss = 0.0, 0.0
model.zero_grad()
train_iterator = trange(epochs, desc="Epoch")
for epoch in train_iterator:
epoch_iterator = tqdm(train_loader, desc="Iteration")
for step, batch in enumerate(epoch_iterator):
model.train()
batch = tuple(t.to(device) for t in batch)
flat_docs, offsets, labels = batch
outputs = model(flat_docs, offsets, labels)
loss = outputs[0]
if gradient_accumulation_steps > 1:
loss = loss / gradient_accumulation_steps
loss.backward()
training_loss += loss.item()
if (step + 1) % gradient_accumulation_steps == 0:
optimizer.step()
scheduler.step()
model.zero_grad()
global_step += 1
if WANDB:
wandb.log({
"train/epoch": epoch,
"train/train_loss": loss,
"train/learning_rate": scheduler.get_lr()[0]
})
logging_steps = len(train_data) // 16
if global_step % logging_steps == 0:
acc, eval_loss = evaluate_model(model, test_data, eval_batch_size, device, output_dir)
logging.info(f"Epoch {epoch} Step {step} Loss {loss.item()} Eval loss {eval_loss} Acc {acc}")
# eval
acc, eval_loss = evaluate_model(model, test_data, eval_batch_size, device, output_dir)
logging.info(f"Evaluation loss: {eval_loss}")
logging.info(f"Evaluation accuracy: {acc}")
def evaluate_model(model, test_data, eval_batch_size, device, output_dir):
"""
Evaluate a trained PyTorch model.
Therefore, the accuracy and loss are calculated.
"""
data_loader = DataLoader(test_data,
collate_fn=collate_for_mlp,
shuffle=False,
batch_size=eval_batch_size)
all_logits = []
all_targets = []
eval_steps, eval_loss = 0, 0.0
for batch in tqdm(data_loader, desc="Evaluating"):
model.eval()
batch = tuple(t.to(device) for t in batch)
with torch.no_grad():
flat_inputs, lengths, labels = batch
outputs = model(flat_inputs, lengths, labels)
all_targets.append(labels.detach().cpu())
eval_steps += 1
loss, logits = outputs[:2]
eval_loss += loss.mean().item()
all_logits.append(logits.detach().cpu())
logits = torch.cat(all_logits).numpy()
targets = torch.cat(all_targets).numpy()
eval_loss /= eval_steps
preds = np.argmax(logits, axis=1)
acc = (preds == targets).sum() / targets.size
# create file if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# append result to file
with open(f"{output_dir}/eval_results.json", "a") as f:
f.write("\n")
json.dump({"acc": acc, "model parameter": str(model)}, f)
if WANDB:
wandb.log({
"eval/loss": eval_loss,
"eval/accuracy": acc
})
return acc, eval_loss
def train_stacking(dataset, model1_name, model2_name, meta_model_name, m1_dropout, m2_dropout, m3_dropout,
m1_hidden_size, m2_hidden_size, m3_hidden_size, m1_num_layers, m2_num_layers, m3_num_layers,
batch_size, m1_lr, m2_lr, m3_lr, m1_weight_decay, m2_weight_decay, m3_weight_decay, epochs):
"""
Train a stacking model.
:param dataset: dataset
:param model1_name: name of the first model
:param model2_name: name of the second model
:param meta_model_name: name of the meta model
:param m1_dropout: dropout of the first model
:param m2_dropout: dropout of the second model
:param m3_dropout: dropout of the meta model
:param m1_hidden_size: hidden size of the first model
:param m2_hidden_size: hidden size of the second model
:param m3_hidden_size: hidden size of the third model
:param m1_num_layers: number of layers of the first model
:param m2_num_layers: number of layers of the second model
:param m3_num_layers: number of layers of the third model
:param batch_size: batch size for all models
:param m1_lr: learning rate of the first model
:param m2_lr: learning rate of the second model
:param m3_lr: learning rate of the meta model
:param m1_weight_decay: weight decay of the first model
:param m2_weight_decay: weight decay of the second model
:param m3_weight_decay: weight decay of the meta model
:param epochs: number of epochs for all models
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
label_dict = dataset['label_dict']
# setup models
is_m1_transformer, m1, tokenizer1 = setup_model(device, label_dict, m1_dropout, m1_hidden_size, m1_num_layers,
model1_name)
is_m2_transformer, m2, tokenizer2 = setup_model(device, label_dict, m2_dropout, m2_hidden_size, m2_num_layers,
model2_name)
is_m3_transformer, m3, tokenizer3 = setup_model(device, label_dict, m3_dropout, m3_hidden_size, m3_num_layers,
meta_model_name)
model = SimplifiedStacking(m1, m2, m3, is_m1_transformer, is_m2_transformer, is_m3_transformer, tokenizer1,
tokenizer2, tokenizer3)
model.fit(dataset, batch_size, m1_lr, m2_lr, m3_lr, m1_weight_decay, m2_weight_decay, m3_weight_decay, epochs,
device, )
acc = model.evaluate(dataset, batch_size, device)
if WANDB:
wandb.log({
"eval/accuracy": acc
})
def train_weighted_boost(dataset, model1_name, model2_name, m1_dropout, m2_dropout, m1_hidden_size, m2_hidden_size,
m1_num_layers, m2_num_layers, batch_size, m1_lr, m2_lr, m1_weight_decay, m2_weight_decay,
epochs, alpha):
"""
Train a weighted boosting model.
:param dataset: dataset
:param model1_name: name of the first model
:param model2_name: name of the second model
:param m1_dropout: dropout of the first model
:param m2_dropout: dropout of the second model
:param m1_hidden_size: hidden size of the first model
:param m2_hidden_size: hidden size of the second model
:param m1_num_layers: number of layers of the first model
:param m2_num_layers: number of layers of the second model
:param batch_size: batch size for all models
:param m1_lr: learning rate of the first model
:param m2_lr: learning rate of the second model
:param m1_weight_decay: weight decay of the first model
:param m2_weight_decay: weight decay of the second model
:param epochs: number of epochs
:param alpha: weight of the first model
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
label_dict = dataset['label_dict']
# setup models
is_m1_transformer, m1, tokenizer1 = setup_model(device, label_dict, m1_dropout, m1_hidden_size, m1_num_layers,
model1_name)
is_m2_transformer, m2, tokenizer2 = setup_model(device, label_dict, m2_dropout, m2_hidden_size, m2_num_layers,
model2_name)
model = SimplifiedWeightedBoost(m1, m2, is_m1_transformer, is_m2_transformer, tokenizer1, tokenizer2)
model.fit(dataset, batch_size, m1_lr, m2_lr, m1_weight_decay, m2_weight_decay, epochs, device)
acc = model.evaluate(dataset, batch_size, device, alpha)
logging.info(f"alpha: {alpha}, acc: {acc}")
eval_for_different_alpha(acc, batch_size, dataset, device, model, alpha)
def eval_for_different_alpha(init_acc, batch_size, dataset, device, model, alpha):
"""
Evaluate the model for different alpha values.
The values are chosen in a range from 0.1 to 0.9 with a step size of 0.1.
:param init_acc: the initial accuracy
:param batch_size: the batch size
:param dataset: the dataset
:param device: the device
:param model: the model
:param alpha: the initial alpha value
"""
best_acc = init_acc
# test different alpha values
for a in [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]:
acc = model.evaluate(dataset, batch_size, device, a)
logging.info(f"alpha: {a}, acc: {acc}")
if acc > best_acc:
best_acc = acc
alpha = a
if WANDB:
wandb.log({
"eval/accuracy": acc,
"best alpha": alpha,
"eval/accuracy with fix alpha": init_acc
})
def train_weighted(dataset, model1_name, model2_name, m1_dropout, m2_dropout, m1_hidden_size, m2_hidden_size,
m1_num_layers, m2_num_layers, batch_size, m1_lr, m2_lr, m1_weight_decay, m2_weight_decay,
m1_epochs, m2_epochs, alpha):
"""
Train a weighted ensemble model
:param dataset: dataset
:param model1_name: name of the first model
:param model2_name: name of the second model
:param m1_dropout: dropout of the first model
:param m2_dropout: dropout of the second model
:param m1_hidden_size: hidden size of the first model
:param m2_hidden_size: hidden size of the second model
:param m1_num_layers: number of layers of the first model
:param m2_num_layers: number of layers of the second model
:param batch_size: batch size for all models
:param m1_lr: learning rate of the first model
:param m2_lr: learning rate of the second model
:param m1_weight_decay: weight decay of the first model
:param m2_weight_decay: weight decay of the second model
:param m1_epochs: number of epochs of the first model
:param m2_epochs: number of epochs of the second model
:param alpha: weight of the first model
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
label_dict = dataset['label_dict']
# setup models
is_m1_transformer, m1, tokenizer1 = setup_model(device, label_dict, m1_dropout, m1_hidden_size, m1_num_layers,
model1_name)
is_m2_transformer, m2, tokenizer2 = setup_model(device, label_dict, m2_dropout, m2_hidden_size, m2_num_layers,
model2_name)
model = WeightedEnsemble(m1, m2, is_m1_transformer, is_m2_transformer, tokenizer1, tokenizer2)
model.fit(dataset, batch_size, m1_lr, m2_lr, m1_weight_decay, m2_weight_decay, m1_epochs, m2_epochs, device)
acc = model.evaluate(dataset, batch_size, device, alpha)
logging.info(f"alpha: {alpha}, acc: {acc}")
eval_for_different_alpha(acc, batch_size, dataset, device, model, alpha)
def setup_model(device, label_dict, dropout, hidden_size, num_layers, model_name):
"""
Set up a model for training based on the given model name.
Used for the ensemble models.
:param device: device to use for training
:param label_dict: label dictionary
:param dropout: dropout rate
:param hidden_size: hidden size
:param num_layers: number of layers
:param model_name: name of the model
:return: is_transformer, model, tokenizer
"""
if model_name == "MLP":
is_m1_transformer = False
tokenizer1 = AutoTokenizer.from_pretrained(MODELS["MLP"])
vocab_size = tokenizer1.vocab_size
m1 = MLP(vocab_size,
len(label_dict),
dropout=dropout,
hidden_size=hidden_size,
num_hidden_layers=num_layers,
pretrained_embedding=None,
freeze=False
).to(device)
elif model_name == "LSTM":
is_m1_transformer = False
tokenizer1 = AutoTokenizer.from_pretrained(MODELS["LSTM"])
vocab_size = tokenizer1.vocab_size
m1 = LSTM(vocab_size,
len(label_dict),
bidirectional=False,
dropout=dropout,
num_layers=num_layers,
hidden_size=hidden_size,
pretrained_embedding=None,
freeze=False
).to(device)
# Transformer:
else:
is_m1_transformer = True
tokenizer1 = AutoTokenizer.from_pretrained(MODELS[model_name])
m1 = AutoModelForSequenceClassification.from_pretrained(MODELS[model_name], num_labels=len(label_dict)).to(
device)
return is_m1_transformer, m1, tokenizer1
def main():
"""
The main entry point of the script.
Parses the arguments and starts the training.
"""
# Parse arguments
import argparse
parser = argparse.ArgumentParser(description='Run text classification on the given dataset with the given model.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# general arguments
parser.add_argument('dataset', type=str, choices=VALID_DATASETS, help='Dataset to use.')
parser.add_argument('model', type=str, choices=VALID_MODELS, help='Model to use.')
parser.add_argument('--output_dir', type=str, default='output', help='Output directory.')
parser.add_argument('--log_level', type=str, default='info', help='Log level.')
parser.add_argument('--log_to_file', action='store_true', help='Log to file.')
parser.add_argument('--log_file', type=str, default='log.txt', help='Log file.')
# training arguments
parser.add_argument('--batch_size', type=int, default=128, help='The batch size.')
parser.add_argument('--learning_rate', type=float, default=5e-5, help='Learning rate.')
parser.add_argument('--num_train_epochs', type=int, default=10, help='Number of training epochs.')
parser.add_argument('--weight_decay', type=float, default=0.00, help='Weight decay.')
parser.add_argument('--warmup_steps', type=int, default=0, help='Number of warmup steps.')
parser.add_argument('--gradient_accumulation_steps', type=int, default=1,
help='Number of gradient accumulation steps.')
parser.add_argument('--dropout', type=float, default=0.5, help='Dropout.')
# lstm arguments
parser.add_argument('--bidirectional', action='store_true', help='Use bidirectional LSTM.')
# lstm / mlp arguments
parser.add_argument('--num_layers', type=int, default=1, help='Number of layers.')
parser.add_argument('--hidden_size', type=int, default=1024, help='Hidden size.')
parser.add_argument('--pretrained_embedding_path', type=str, default=None, help='Path to pretrained embeddings.')
parser.add_argument('--unfreeze_embedding', action='store_true', help='Unfreeze embedding.')
# ensemble arguments
parser.add_argument('--m1', type=str, choices=VALID_MODELS, help='Model 1 if ensemble is used.')
parser.add_argument('--m2', type=str, choices=VALID_MODELS, help='Model 2 if ensemble is used.')
parser.add_argument('--mm', type=str, choices=VALID_MODELS, help='Meta model if stacking ensemble is used.')
parser.add_argument('--m1_learning_rate', type=float, default=5e-5, help='Learning rate.')
parser.add_argument('--m1_weight_decay', type=float, default=0.00, help='Weight decay.')
parser.add_argument('--m1_warmup_steps', type=int, default=0, help='Number of warmup steps.')
parser.add_argument('--m1_dropout', type=float, default=0.5, help='Dropout.')
parser.add_argument('--m1_num_layers', type=int, default=1, help='Number of layers.')
parser.add_argument('--m1_hidden_size', type=int, default=1024, help='Hidden size.')
parser.add_argument('--m2_learning_rate', type=float, default=5e-5, help='Learning rate.')
parser.add_argument('--m2_weight_decay', type=float, default=0.00, help='Weight decay.')
parser.add_argument('--m2_warmup_steps', type=int, default=0, help='Number of warmup steps.')
parser.add_argument('--m2_dropout', type=float, default=0.5, help='Dropout.')
parser.add_argument('--m2_num_layers', type=int, default=1, help='Number of layers.')
parser.add_argument('--m2_hidden_size', type=int, default=1024, help='Hidden size.')
parser.add_argument('--m2_num_train_epochs', type=int, default=10,
help='Number of training epochs. Only used with WEIGHTED')
parser.add_argument('--mm_learning_rate', type=float, default=5e-5, help='Learning rate.')
parser.add_argument('--mm_weight_decay', type=float, default=0.00, help='Weight decay.')
parser.add_argument('--mm_warmup_steps', type=int, default=0, help='Number of warmup steps.')
parser.add_argument('--mm_dropout', type=float, default=0.5, help='Dropout.')
parser.add_argument('--mm_num_layers', type=int, default=1, help='Number of layers.')
parser.add_argument('--mm_hidden_size', type=int, default=1024, help='Hidden size.')
parser.add_argument('--alpha', type=float, default=0.5, help='Weight of model 1 in weighted ensemble.')
args = parser.parse_args()
# Set up logging
log_level = getattr(logging, args.log_level.upper())
if args.log_to_file:
logging.basicConfig(filename=f'{args.output_dir}/{args.log_file}', level=log_level)
else:
logging.basicConfig(level=log_level)
# init wandb
if WANDB:
# init wandb name
model = args.model
if model == 'LSTM' and args.bidirectional:
name = 'Bi-LSTM'
elif model == 'WEIGHTED_BOOST' or model == 'WEIGHTED':
name = f'{model} ({args.m1} + {args.m2})'
elif model == 'STACKING':
name = f'{model} ({args.m1} + {args.m2} -> {args.mm})'
else:
name = model
wandb.init(project='Bachelor-Thesis',
name=name,
config=vars(args))
config = wandb.config
else:
config = vars(args)
logging.info("Starting...")
logging.debug("Arguments: %s", args)
# Start training
logging.info(f"Loading {args.dataset} data...")
dataset = load_data(args.dataset)
if args.model == "MLP":
train_mlp(dataset,
args.output_dir,
eval_batch_size=config["batch_size"],
training_batch_size=config["batch_size"],
learning_rate=config["learning_rate"],
epochs=config["num_train_epochs"],
weight_decay=config["weight_decay"],
warmup_steps=config["warmup_steps"],
gradient_accumulation_steps=config["gradient_accumulation_steps"],
dropout=config["dropout"],
hidden_size=config["hidden_size"],
num_hidden_layers=config["num_layers"],
pretrained_embedding_path=config["pretrained_embedding_path"],
freeze_embedding=not config["unfreeze_embedding"],
)
elif args.model == "LSTM":
train_lstm(dataset,
config["output_dir"],
epochs=config["num_train_epochs"],
warmup_steps=config["warmup_steps"],
learning_rate=config["learning_rate"],
weight_decay=config["weight_decay"],
training_batch_size=config["batch_size"],
eval_batch_size=config["batch_size"],
bidirectional=config["bidirectional"],
num_layers=config["num_layers"],
hidden_size=config["hidden_size"],
dropout=config["dropout"],
gradient_accumulation_steps=config["gradient_accumulation_steps"],
pretrained_embedding_path=config["pretrained_embedding_path"],
freeze_embedding=not config["unfreeze_embedding"]
)
elif args.model == "STACKING":
train_stacking(dataset,
model1_name=config["m1"],
model2_name=config["m2"],
meta_model_name=config["mm"],
m1_dropout=config["m1_dropout"],
m2_dropout=config["m2_dropout"],
m3_dropout=config["mm_dropout"],
m1_hidden_size=config["m1_hidden_size"],
m2_hidden_size=config["m2_hidden_size"],
m3_hidden_size=config["mm_hidden_size"],
m1_num_layers=config["m1_num_layers"],
m2_num_layers=config["m2_num_layers"],
m3_num_layers=config["mm_num_layers"],
batch_size=config["batch_size"],
m1_lr=config["m1_learning_rate"],
m2_lr=config["m2_learning_rate"],
m3_lr=config["mm_learning_rate"],
m1_weight_decay=config["m1_weight_decay"],
m2_weight_decay=config["m2_weight_decay"],
m3_weight_decay=config["mm_weight_decay"],
epochs=config["num_train_epochs"]
)
elif args.model == "WEIGHTED_BOOST":
train_weighted_boost(dataset,
model1_name=config["m1"],
model2_name=config["m2"],
m1_dropout=config["m1_dropout"],
m2_dropout=config["m2_dropout"],
m1_hidden_size=config["m1_hidden_size"],
m2_hidden_size=config["m2_hidden_size"],
m1_num_layers=config["m1_num_layers"],
m2_num_layers=config["m2_num_layers"],
batch_size=config["batch_size"],
m1_lr=config["m1_learning_rate"],
m2_lr=config["m2_learning_rate"],
m1_weight_decay=config["m1_weight_decay"],
m2_weight_decay=config["m2_weight_decay"],
epochs=config["num_train_epochs"],
alpha=config["alpha"]
)
elif args.model == "WEIGHTED":
train_weighted(dataset,
model1_name=config["m1"],
model2_name=config["m2"],
m1_dropout=config["m1_dropout"],
m2_dropout=config["m2_dropout"],
m1_hidden_size=config["m1_hidden_size"],
m2_hidden_size=config["m2_hidden_size"],
m1_num_layers=config["m1_num_layers"],
m2_num_layers=config["m2_num_layers"],
batch_size=config["batch_size"],
m1_lr=config["m1_learning_rate"],
m2_lr=config["m2_learning_rate"],
m1_weight_decay=config["m1_weight_decay"],
m2_weight_decay=config["m2_weight_decay"],
m1_epochs=config["num_train_epochs"],
m2_epochs=config["m2_num_train_epochs"],
alpha=config["alpha"]
)
else:
train_transformer(config["model"],
dataset,
config["output_dir"],
training_batch_size=config["batch_size"],
eval_batch_size=config["batch_size"],
learning_rate=config["learning_rate"],
num_train_epochs=config["num_train_epochs"],
weight_decay=config["weight_decay"])
if __name__ == '__main__':
main()