-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
753 lines (569 loc) · 24.4 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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import argparse
import csv
import logging
import os
import random
import pickle
import sys
from global_config import *
import numpy as np
import wandb
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import accuracy_score, f1_score
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset
from torch.utils.data.distributed import DistributedSampler
from tqdm import tqdm, trange
from torch.nn import CrossEntropyLoss, L1Loss, BCEWithLogitsLoss
from scipy.stats import pearsonr, spearmanr
from sklearn.metrics import matthews_corrcoef
from transformers import (
AlbertConfig,
AlbertTokenizer,
AlbertForSequenceClassification,
BertForNextSentencePrediction,
BertTokenizer,
get_linear_schedule_with_warmup,
)
from models import *
from transformers.optimization import AdamW
def return_unk():
return 0
parser = argparse.ArgumentParser()
parser.add_argument(
"--model", type=str, choices=["HKT","language_only", "acoustic_only", "visual_only","hcf_only"], default="HKT",
)
parser.add_argument("--dataset", type=str, choices=["humor", "sarcasm"], default="sarcasm")
parser.add_argument("--batch_size", type=int, default=16)
parser.add_argument("--max_seq_length", type=int, default=85)
parser.add_argument("--n_layers", type=int, default=1)
parser.add_argument("--n_heads", type=int, default=1)
parser.add_argument("--cross_n_layers", type=int, default=1)
parser.add_argument("--cross_n_heads", type=int, default=4)
parser.add_argument("--fusion_dim", type=int, default=172)
parser.add_argument("--dropout", type=float, default=0.2366)
parser.add_argument("--epochs", type=int, default=20)
parser.add_argument("--seed", type=int, default=100)
parser.add_argument("--learning_rate", type=float, default=0.000005)
parser.add_argument("--learning_rate_a", type=float, default=0.003)
parser.add_argument("--learning_rate_h", type=float, default=0.0003)
parser.add_argument("--learning_rate_v", type=float, default=0.003)
parser.add_argument("--warmup_ratio", type=float, default=0.07178)
parser.add_argument("--save_weight", type=str, choices=["True","False"], default="False")
args = parser.parse_args()
class InputFeatures(object):
"""A single set of features of data."""
def __init__(self, input_ids, input_mask, segment_ids, visual, acoustic,hcf,label_id):
self.input_ids = input_ids
self.input_mask = input_mask
self.segment_ids = segment_ids
self.visual = visual
self.acoustic = acoustic
self.hcf = hcf
self.label_id = label_id
def _truncate_seq_pair(tokens_a, tokens_b, max_length):
"""Truncates a sequence pair in place to the maximum length."""
pop_count = 0
while True:
total_length = len(tokens_a) + len(tokens_b)
if total_length <= max_length:
break
if len(tokens_a) == 0:
tokens_b.pop()
else:
pop_count += 1
tokens_a.pop(0)
return pop_count
#albert tokenizer split words in to subwords. "_" marker helps to find thos sub words
#our acoustic and visual features are aligned on word level. So we just create copy the same
#visual/acoustic vectors that belong to same word.
def get_inversion(tokens, SPIECE_MARKER="▁"):
inversion_index = -1
inversions = []
for token in tokens:
if SPIECE_MARKER in token:
inversion_index += 1
inversions.append(inversion_index)
return inversions
def convert_humor_to_features(examples, tokenizer, punchline_only=False):
features = []
for (ex_index, example) in enumerate(examples):
#p denotes punchline, c deontes context
#hid is the utterance unique id. these id's are provided by the authors of urfunny and mustard
#label is either 1/0 . 1=humor, 0=not humor
(
(p_words, p_visual, p_acoustic, p_hcf),
(c_words, c_visual, c_acoustic, c_hcf),
hid,
label
) = example
text_a = ". ".join(c_words)
text_b = p_words + "."
tokens_a = tokenizer.tokenize(text_a)
tokens_b = tokenizer.tokenize(text_b)
inversions_a = get_inversion(tokens_a)
inversions_b = get_inversion(tokens_b)
pop_count = _truncate_seq_pair(tokens_a, tokens_b, args.max_seq_length - 3)
inversions_a = inversions_a[pop_count:]
inversions_b = inversions_b[: len(tokens_b)]
visual_a = []
acoustic_a = []
hcf_a=[]
#our acoustic and visual features are aligned on word level. So we just
#create copy of the same visual/acoustic vectors that belong to same word.
#because ber tokenizer split word into subwords
for inv_id in inversions_a:
visual_a.append(c_visual[inv_id, :])
acoustic_a.append(c_acoustic[inv_id, :])
hcf_a.append(c_hcf[inv_id, :])
visual_a = np.array(visual_a)
acoustic_a = np.array(acoustic_a)
hcf_a = np.array(hcf_a)
visual_b = []
acoustic_b = []
hcf_b = []
for inv_id in inversions_b:
visual_b.append(p_visual[inv_id, :])
acoustic_b.append(p_acoustic[inv_id, :])
hcf_b.append(p_hcf[inv_id, :])
visual_b = np.array(visual_b)
acoustic_b = np.array(acoustic_b)
hcf_b = np.array(hcf_b)
tokens = ["[CLS]"] + tokens_a + ["[SEP]"] + tokens_b + ["[SEP]"]
acoustic_zero = np.zeros((1, ACOUSTIC_DIM_ALL))
if len(tokens_a) == 0:
acoustic = np.concatenate(
(acoustic_zero, acoustic_zero, acoustic_b, acoustic_zero)
)
else:
acoustic = np.concatenate(
(acoustic_zero, acoustic_a, acoustic_zero, acoustic_b, acoustic_zero)
)
visual_zero = np.zeros((1, VISUAL_DIM_ALL))
if len(tokens_a) == 0:
visual = np.concatenate((visual_zero, visual_zero, visual_b, visual_zero))
else:
visual = np.concatenate(
(visual_zero, visual_a, visual_zero, visual_b, visual_zero)
)
hcf_zero = np.zeros((1,4))
if len(tokens_a) == 0:
hcf = np.concatenate((hcf_zero, hcf_zero, hcf_b, hcf_zero))
else:
hcf = np.concatenate(
(hcf_zero, hcf_a, hcf_zero, hcf_b, hcf_zero)
)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
segment_ids = [0] * (len(tokens_a) + 2) + [1] * (len(tokens_b) + 1)
input_mask = [1] * len(input_ids)
acoustic_padding = np.zeros(
(args.max_seq_length - len(input_ids), acoustic.shape[1])
)
acoustic = np.concatenate((acoustic, acoustic_padding))
#original urfunny acoustic feature dimension is 81.
#we found many features are highly correllated. so we removed
#highly correlated feature to reduce dimension
acoustic=np.take(acoustic, acoustic_features_list,axis=1)
visual_padding = np.zeros(
(args.max_seq_length - len(input_ids), visual.shape[1])
)
visual = np.concatenate((visual, visual_padding))
#original urfunny visual feature dimension is more than 300.
#we only considred the action unit and face shape parameter features
visual = np.take(visual, visual_features_list,axis=1)
hcf_padding= np.zeros(
(args.max_seq_length - len(input_ids), hcf.shape[1])
)
hcf = np.concatenate((hcf, hcf_padding))
padding = [0] * (args.max_seq_length - len(input_ids))
input_ids += padding
input_mask += padding
segment_ids += padding
assert len(input_ids) == args.max_seq_length
assert len(input_mask) == args.max_seq_length
assert len(segment_ids) == args.max_seq_length
assert acoustic.shape[0] == args.max_seq_length
assert visual.shape[0] == args.max_seq_length
assert hcf.shape[0] == args.max_seq_length
label_id = float(label)
features.append(
InputFeatures(
input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids,
visual=visual,
acoustic=acoustic,
hcf=hcf,
label_id=label_id,
)
)
return features
def get_appropriate_dataset(data, tokenizer, parition):
features = convert_humor_to_features(data, tokenizer)
all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in features], dtype=torch.long)
all_segment_ids = torch.tensor([f.segment_ids for f in features], dtype=torch.long)
all_visual = torch.tensor([f.visual for f in features], dtype=torch.float)
all_acoustic = torch.tensor([f.acoustic for f in features], dtype=torch.float)
hcf = torch.tensor([f.hcf for f in features], dtype=torch.float)
all_label_ids = torch.tensor([f.label_id for f in features], dtype=torch.float)
dataset = TensorDataset(
all_input_ids,
all_visual,
all_acoustic,
all_input_mask,
all_segment_ids,
hcf,
all_label_ids,
)
return dataset
def set_up_data_loader():
if args.dataset=="humor":
data_file = "ur_funny.pkl"
elif args.dataset=="sarcasm":
data_file = "mustard.pkl"
with open(
os.path.join(DATASET_LOCATION, data_file),
"rb",
) as handle:
all_data = pickle.load(handle)
train_data = all_data["train"]
dev_data = all_data["dev"]
test_data = all_data["test"]
tokenizer = AlbertTokenizer.from_pretrained("albert-base-v2")
train_dataset = get_appropriate_dataset(train_data, tokenizer, "train")
dev_dataset = get_appropriate_dataset(dev_data, tokenizer, "dev")
test_dataset = get_appropriate_dataset(test_data, tokenizer, "test")
train_dataloader = DataLoader(
train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=1
)
dev_dataloader = DataLoader(
dev_dataset, batch_size=args.batch_size, shuffle=True, num_workers=1
)
test_dataloader = DataLoader(
test_dataset, batch_size=args.batch_size, shuffle=True, num_workers=1
)
return train_dataloader, dev_dataloader, test_dataloader
def train_epoch(model, train_dataloader, optimizer, scheduler, loss_fct):
model.train()
tr_loss = 0
nb_tr_examples, nb_tr_steps = 0, 0
for step, batch in enumerate(tqdm(train_dataloader, desc="Iteration")):
batch = tuple(t.to(DEVICE) for t in batch)
(
input_ids,
visual,
acoustic,
input_mask,
segment_ids,
hcf,
label_ids
) = batch
visual = torch.squeeze(visual, 1)
acoustic = torch.squeeze(acoustic, 1)
if args.model == "language_only":
outputs = model(
input_ids,
token_type_ids=segment_ids,
attention_mask=input_mask,
labels=None,
)
elif args.model == "acoustic_only":
outputs = model(
acoustic
)
elif args.model == "visual_only":
outputs = model(
visual
)
elif args.model=="hcf_only":
outputs=model(hcf)
elif args.model=="HKT":
outputs = model(input_ids, visual, acoustic,hcf, token_type_ids=segment_ids, attention_mask=input_mask,)
logits = outputs[0]
loss = loss_fct(logits.view(-1), label_ids.view(-1))
tr_loss += loss.item()
nb_tr_examples += input_ids.size(0)
nb_tr_steps += 1
loss.backward()
for o_i in range(len(optimizer)):
optimizer[o_i].step()
scheduler[o_i].step()
model.zero_grad()
return tr_loss/nb_tr_steps
def eval_epoch(model, dev_dataloader, loss_fct):
model.eval()
dev_loss = 0
nb_dev_examples, nb_dev_steps = 0, 0
with torch.no_grad():
for step, batch in enumerate(tqdm(dev_dataloader, desc="Iteration")):
batch = tuple(t.to(DEVICE) for t in batch)
(
input_ids,
visual,
acoustic,
input_mask,
segment_ids,
hcf,
label_ids
) = batch
visual = torch.squeeze(visual, 1)
acoustic = torch.squeeze(acoustic, 1)
if args.model == "language_only":
outputs = model(
input_ids,
token_type_ids=segment_ids,
attention_mask=input_mask,
labels=None,
)
elif args.model == "acoustic_only":
outputs = model(
acoustic
)
elif args.model == "visual_only":
outputs = model(
visual
)
elif args.model=="hcf_only":
outputs=model(hcf)
elif args.model=="HKT":
outputs = model(input_ids, visual, acoustic,hcf, token_type_ids=segment_ids, attention_mask=input_mask,)
logits = outputs[0]
loss = loss_fct(logits.view(-1), label_ids.view(-1))
dev_loss += loss.item()
nb_dev_examples += input_ids.size(0)
nb_dev_steps += 1
return dev_loss/nb_dev_steps
def test_epoch(model, test_data_loader, loss_fct):
""" Epoch operation in evaluation phase """
model.eval()
eval_loss = 0.0
nb_eval_steps = 0
preds = []
all_labels = []
with torch.no_grad():
for step, batch in enumerate(tqdm(test_data_loader, desc="Iteration")):
batch = tuple(t.to(DEVICE) for t in batch)
(
input_ids,
visual,
acoustic,
input_mask,
segment_ids,
hcf,
label_ids
) = batch
visual = torch.squeeze(visual, 1)
acoustic = torch.squeeze(acoustic, 1)
if args.model == "language_only":
outputs = model(
input_ids,
token_type_ids=segment_ids,
attention_mask=input_mask,
labels=None,
)
elif args.model == "acoustic_only":
outputs = model(
acoustic
)
elif args.model == "visual_only":
outputs = model(
visual
)
elif args.model=="hcf_only":
outputs=model(hcf)
elif args.model=="HKT":
outputs = model(input_ids, visual, acoustic,hcf, token_type_ids=segment_ids, attention_mask=input_mask,)
logits = outputs[0]
tmp_eval_loss = loss_fct(logits.view(-1), label_ids.view(-1))
eval_loss += tmp_eval_loss.mean().item()
nb_eval_steps += 1
logits = torch.sigmoid(logits)
if len(preds) == 0:
preds=logits.detach().cpu().numpy()
all_labels=label_ids.detach().cpu().numpy()
else:
preds= np.append(preds, logits.detach().cpu().numpy(), axis=0)
all_labels = np.append(
all_labels, label_ids.detach().cpu().numpy(), axis=0
)
eval_loss = eval_loss / nb_eval_steps
preds = np.squeeze(preds)
all_labels = np.squeeze(all_labels)
return preds, all_labels, eval_loss
def test_score_model(model, test_data_loader, loss_fct, exclude_zero=False):
predictions, y_test, test_loss = test_epoch(model, test_data_loader, loss_fct)
predictions = predictions.round()
f_score = f1_score(y_test, predictions, average="weighted")
accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", accuracy,"F score:", f_score)
return accuracy, f_score, test_loss
def train(
model,
train_dataloader,
dev_dataloader,
test_dataloader,
optimizer,
scheduler,
loss_fct,
):
best_valid_loss = 9e+9
run_name = str(wandb.run.id)
valid_losses = []
n_epochs=args.epochs
for epoch_i in range(n_epochs):
train_loss = train_epoch(
model, train_dataloader, optimizer, scheduler, loss_fct
)
valid_loss = eval_epoch(model, dev_dataloader, loss_fct)
valid_losses.append(valid_loss)
print(
"\nepoch:{},train_loss:{}, valid_loss:{}".format(
epoch_i, train_loss, valid_loss
)
)
test_accuracy, test_f_score, test_loss = test_score_model(
model, test_dataloader, loss_fct
)
if(valid_loss <= best_valid_loss):
best_valid_loss = valid_loss
best_valid_test_accuracy = test_accuracy
best_valid_test_fscore= test_f_score
if(args.save_weight == "True"):
torch.save(model.state_dict(),'./best_weights/'+run_name+'.pt')
#we report test_accuracy of the best valid loss (best_valid_test_accuracy)
wandb.log(
{
"train_loss": train_loss,
"valid_loss": valid_loss,
"test_loss": test_loss,
"best_valid_loss": best_valid_loss,
"best_valid_test_accuracy": best_valid_test_accuracy,
"best_valid_test_fscore":best_valid_test_fscore
}
)
def get_optimizer_scheduler(params,num_training_steps,learning_rate=1e-5):
no_decay = ["bias", "LayerNorm.bias", "LayerNorm.weight"]
optimizer_grouped_parameters = [
{
"params": [
p for n, p in params if not any(nd in n for nd in no_decay)
],
"weight_decay": 0.01,
},
{
"params": [
p for n, p in params if any(nd in n for nd in no_decay)
],
"weight_decay": 0.0,
},
]
optimizer = AdamW(optimizer_grouped_parameters, lr=learning_rate)
scheduler = get_linear_schedule_with_warmup(
optimizer,
num_warmup_steps=int(num_training_steps * args.warmup_ratio),
num_training_steps=num_training_steps,
)
return optimizer,scheduler
def prep_for_training(num_training_steps):
if args.model == "language_only":
model = AlbertForSequenceClassification.from_pretrained(
"albert-base-v2", num_labels=1
)
elif args.model == "acoustic_only":
model = Transformer(ACOUSTIC_DIM, num_layers=args.n_layers, nhead=args.n_heads, dim_feedforward=args.fc_dim)
elif args.model == "visual_only":
model = Transformer(VISUAL_DIM, num_layers=args.n_layers, nhead=args.n_heads, dim_feedforward=args.fc_dim)
elif args.model=="hcf_only":
model=Transformer(HCF_DIM, num_layers=args.n_layers, nhead=args.n_heads, dim_feedforward=args.fc_dim)
elif args.model == "HKT" :
#HKT model has 4 unimodal encoders. But the language one is ALBERT pretrained model. But other enocders are
#trained from scratch with low level features. We have found that many times most of the the gardients flows to albert encoders only as it
#already has rich contextual representation. So in the beginning the gradient flows ignores other encoders which are trained from low level features.
# We found that if we intitalize the weights of the acoustic, visual and hcf encoders of HKT model from the best unimodal models that we already ran for ablation study then
#the model converege faster. Other wise it takes very long time to converge.
if args.dataset=="humor":
visual_model = Transformer(VISUAL_DIM, num_layers=7, nhead=3, dim_feedforward= 128)
visual_model.load_state_dict(torch.load("./model_weights/init/humor/humorVisualTransformer.pt"))
acoustic_model = Transformer(ACOUSTIC_DIM, num_layers=8, nhead=3, dim_feedforward = 256)
acoustic_model.load_state_dict(torch.load("./model_weights/init/humor/humorAcousticTransformer.pt"))
hcf_model = Transformer(HCF_DIM, num_layers=3, nhead=2, dim_feedforward = 128)
hcf_model.load_state_dict(torch.load("./model_weights/init/humor/humorHCFTransformer.pt"))
elif args.dataset=="sarcasm":
visual_model = Transformer(VISUAL_DIM, num_layers=8, nhead=4, dim_feedforward=1024)
visual_model.load_state_dict(torch.load("./model_weights/init/sarcasm/sarcasmVisualTransformer.pt"))
acoustic_model = Transformer(ACOUSTIC_DIM, num_layers=1, nhead=3, dim_feedforward=512)
acoustic_model.load_state_dict(torch.load("./model_weights/init/sarcasm/sarcasmAcousticTransformer.pt"))
hcf_model = Transformer(HCF_DIM, num_layers=8, nhead=4, dim_feedforward=128)
hcf_model.load_state_dict(torch.load("./model_weights/init/sarcasm/sarcasmHCFTransformer.pt"))
text_model = AlbertModel.from_pretrained('albert-base-v2')
model = HKT(text_model, visual_model, acoustic_model,hcf_model, args)
else:
raise ValueError("Requested model is not available")
model.to(DEVICE)
loss_fct = BCEWithLogitsLoss()
# Prepare optimizer
# used different learning rates for different componenets.
if args.model == "HKT" :
acoustic_params,visual_params,hcf_params,other_params = model.get_params()
optimizer_o,scheduler_o=get_optimizer_scheduler(other_params,num_training_steps,learning_rate=args.learning_rate)
optimizer_h,scheduler_h=get_optimizer_scheduler(hcf_params,num_training_steps,learning_rate=args.learning_rate_h)
optimizer_v,scheduler_v=get_optimizer_scheduler(visual_params,num_training_steps,learning_rate=args.learning_rate_v)
optimizer_a,scheduler_a=get_optimizer_scheduler(acoustic_params,num_training_steps,learning_rate=args.learning_rate_a)
optimizers=[optimizer_o,optimizer_h,optimizer_v,optimizer_a]
schedulers=[scheduler_o,scheduler_h,scheduler_v,scheduler_a]
else:
params = list(model.named_parameters())
optimizer_l, scheduler_l = get_optimizer_scheduler(
params, num_training_steps, learning_rate=args.learning_rate
)
optimizers=[optimizer_l]
schedulers=[scheduler_l]
return model, optimizers, schedulers,loss_fct
def set_random_seed(seed):
"""
This function controls the randomness by setting seed in all the libraries we will use.
"""
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.enabled = False
torch.backends.cudnn.deterministic = True
def main():
wandb.init(project="HKT")
wandb.config.update(args)
if(args.seed == -1):
seed = random.randint(0, 9999)
print("seed",seed)
else:
seed = args.seed
wandb.config.update({"seed": seed}, allow_val_change=True)
set_random_seed(seed)
train_dataloader,dev_dataloader,test_dataloader=set_up_data_loader()
print("Dataset Loaded: ",args.dataset)
num_training_steps = len(train_dataloader) * args.epochs
model, optimizers, schedulers, loss_fct = prep_for_training(
num_training_steps
)
print("Model Loaded: ",args.model)
train(
model,
train_dataloader,
dev_dataloader,
test_dataloader,
optimizers,
schedulers,
loss_fct,
)
if __name__ == "__main__":
main()