-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1319 lines (1022 loc) · 55.3 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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import random
import math
import sys
import time
import numpy as np
import pandas as pd
import preprocessor as p
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as Data
from pytorch_transformers import *
from torch.autograd import Variable
from torch.utils.data import Dataset
import torch.backends.cudnn as cudnn
from copy import deepcopy
from utils.memorybank import *
from utils.aum import *
from utils.aug import aug
from utils.ema import EMA
def oneRun(log_dir_path_multiRun=None, **params):
import torch
##### Default Setting
## Set path
# add sys path
root = './'
sys.path.append(root)
# output directory: used to store saved model and aum.json
output_dir_path = './experiment/hurricane'
# set Data Path
base_path = './data/'
target_list = ['hurricane']
print('\nData base directory: ', base_path)
print('Target list: ', target_list)
# set log_dir_path: used to store log, plots, saved model for current training
import time
cur_time = time.strftime("%Y%m%d-%H%M%S")
if log_dir_path_multiRun is None:
log_root = os.getcwd() + '/log/'
log_dir_path = log_root + cur_time + '/'
else:
log_root = log_dir_path_multiRun
log_dir_path = log_dir_path_multiRun + cur_time + '/'
if not os.path.exists(log_root):
os.makedirs(log_root)
if not os.path.exists(log_dir_path):
os.makedirs(log_dir_path)
## Set default hyperparameters
n_labeled_per_class = 10 if 'n_labeled_per_class' not in params else params['n_labeled_per_class']
n_unlabeled_per_class = None if 'n_unlabeled_per_class' not in params else params['n_unlabeled_per_class']
seed = 42 if 'seed' not in params else params['seed']
device_idx = 1 if 'device_idx' not in params else params['device_idx']
val_interval = 25 if 'val_interval' not in params else params['val_interval'] # 20, 25
steps = 5000 if 'steps' not in params else params['steps']
early_stop_tolerance = 10 if 'early_stop_tolerance' not in params else params['early_stop_tolerance'] # 5, 6, 10
lr = 2e-5 if 'lr' not in params else params['lr']
bs = 32 if 'bs' not in params else params['bs'] # original: 32
bs_u = 32 if 'bs_u' not in params else params['bs_u'] # original: 32
max_length = 64
# - data augmentation
aug_mode = True if 'aug_mode' not in params else params['aug_mode']
num_aug = 2 if 'num_aug' not in params else params['num_aug']
alpha_sr = 0.1 if 'alpha_sr' not in params else params['alpha_sr']
alpha_rs = 0.1 if 'alpha_rs' not in params else params['alpha_rs']
aug_dict = {'num_aug': num_aug, 'alpha_sr': alpha_sr, 'alpha_rs': alpha_rs}
strong_aug_mode = False if 'strong_aug_mode' not in params else params['strong_aug_mode']
# - mixup
mixup_mode = False if 'mixup_mode' not in params else params['mixup_mode']
mixup_method = 3 if 'mixup_method' not in params else params['mixup_method'] #[1,2,3]
mixup_alpha = 0.75 if 'mixup_alpha' not in params else params['mixup_alpha'] # alpha: 2, 0.75
mixup_layers = [i for i in range(13)] if 'mixup_layers' not in params else params['mixup_layers'] # mixup_layers: [[-1], [i for i in range(13)], [0], [12], [7,9,12], [6,8,11]]
# - semi-supervised
weight_u_loss = 10 if 'weight_u_loss' not in params else params['weight_u_loss']
u_loss_rampup_length = 1000 if 'u_loss_rampup_length' not in params else params['u_loss_rampup_length']
threshold_mode = 'hard_t' if 'threshold_mode' not in params else params['threshold_mode'] # hard_t, sat, sat_global
labeling_mode = 'hard' if 'labeling_mode' not in params else params['labeling_mode'] # hard, soft, sharpening
u_loss = 'L2' if 'u_loss' not in params else params['u_loss'] # cross_entropy, L2
# - consistency regularization
consis_reg = 'avg_pred' if 'avg_pred' not in params else params['avg_pred'] # avg_pred, weak_sup_strong
# - pseudo-labeling
psl_mode = False if 'psl_mode' not in params else params['psl_mode']
psl_threshold_h = 0.75 if 'psl_threshold_h' not in params else params['psl_threshold_h']
threshold_rampup_length = 100 if 'threshold_rampup_length' not in params else params['threshold_rampup_length']
sharpening_T = 0.5 if 'sharpening_T' not in params else params['sharpening_T']
# - ema
ema_mode = False if 'ema_mode' not in params else params['ema_mode']
ema_momentum = 0.999 if 'ema_momentum' not in params else params['ema_momentum']
# - dibias
# threshold_mode = 'debias'
marginal_loss = False if 'marginal_loss' not in params else params['marginal_loss']
tau = 0.4 if 'tau' not in params else params['tau']
qhat_momentum = 0.99 if 'qhat_momentum' not in params else params['qhat_momentum'] # 0.99, 0.999
# - memorybank
use_memorybank = False if 'use_memorybank' not in params else params['use_memorybank'] # original: 200
memory_per_class = 200 if 'memory_per_class' not in params else params['memory_per_class'] # original: 5
samples_per_class = 5 if 'samples_per_class' not in params else params['samples_per_class']
sampling_strategy = None if 'sampling_strategy' not in params else params['sampling_strategy'] # 'avg_probs', None
sampling_temperature = 1 if 'sampling_temperature' not in params else params['sampling_temperature'] # Range:[0~2]
selection_strategy = None if 'selection_strategy' not in params else params['selection_strategy'] # selection_strategy = 'top', 'proportional', 'top_proportional'
selection_metrics = None if 'selection_metrics' not in params else params['selection_metrics'] # selection_metrics = 'margin', 'aum'
selection_top_threshold = 0.8 if 'selection_top_threshold' not in params else params['selection_top_threshold']
selection_sharpening_T = 0.5 if 'selection_sharpening_T' not in params else params['selection_sharpening_T']
# - investigate
investigate = None if 'investigate' not in params else params['investigate'] # ['psl_acc', 'psl_num', 'psl_acc_num']
if investigate == 'psl_num' or investigate == 'psl_acc_num':
# (b) use same # psl per classs for training per iteration, here psl are not guaranteed to be correct (invest. num) -> (memory bank)
use_memorybank = True
# Set random seed
random.seed(seed)
torch.manual_seed(seed)
np.random.seed(seed)
cudnn.deterministic = True
# Check & set device
if torch.cuda.is_available():
device = torch.device("cuda", device_idx)
print('\nThere are %d GPU(s) available.' % torch.cuda.device_count())
print('We will use the GPU-', device_idx, torch.cuda.get_device_name(device_idx))
else:
print('\nNo GPU available, using the CPU instead.')
device = torch.device("cpu")
##### Data Processing
# read data
def read_data_split(event_list, split):
total_df = pd.DataFrame()
for event in event_list:
df = pd.read_csv(base_path+'/%s/%s_%s.tsv' % (event, event, split), sep='\t')
total_df = pd.concat([total_df, df])
return total_df
target_train_df = read_data_split(target_list, 'train')
target_dev_df = read_data_split(target_list, 'dev')
target_test_df = read_data_split(target_list, 'test')
# labels mapping
labels_set = sorted(set(target_train_df['class_label'].to_list())) # use sorted() here because set() output order is unstable
n_labels = len(labels_set)
labels_mapping = {label: idx for idx, label in enumerate(labels_set)}
print('\nAvailable labels and mapping: \n', labels_mapping)
print('n_labels: ', n_labels)
print('')
data = [target_train_df, target_dev_df, target_test_df]
data_name = ['target_train_df', 'target_dev_df', 'target_test_df']
for idx, df in enumerate(data):
df['class_label'] = df['class_label'].map(labels_mapping)
print('Original %s samples: %d' % (data_name[idx], df.shape[0]))
## Data Preprocessing
import preprocessor as p
def clean_tweet(df):
"""
Clean Tweet:
1.remove URL, Mention/Username, Hashtag sign, Emoji, Smiley, Number
2.lowercasing, remove punctuation, remove retweet 'RT'
"""
# remove URL, Mention/Username, Emoji, Smiley, Number
p.set_options(p.OPT.URL, p.OPT.MENTION, p.OPT.EMOJI, p.OPT.SMILEY) # p.OPT.NUMBER
df['cleaned_tweet'] = df['tweet_text'].apply(lambda x: p.clean(x))
# lowercasing, remove punctuation, Hashtag sign, 'RT'
df['cleaned_tweet'] = df['cleaned_tweet'].str.replace('RT ','')
df['cleaned_tweet'] = df['cleaned_tweet'].str.lower()
df['cleaned_tweet'] = df['cleaned_tweet'].str.replace('[^\w\s]',' ')
return df
for idx, df in enumerate(data):
df = clean_tweet(df)
print('Cleaned %s samples: %d' % (data_name[idx], df.shape[0]))
## Data Spliting
def train_split(labels, n_labeled_per_class, unlabeled_per_class=None):
"""Split the original training set into labeled training set, unlabeled training set, development set
Arguments:
labels {list} -- List of labeles for original training set
n_labeled_per_class {int} -- Number of labeled data per class
Keyword Arguments:
unlabeled_per_class {int or None} -- Number of unlabeled data per class (default: {None})
Returns:
[list] -- idx for labeled training set, unlabeled training set, development set
"""
labels = np.array(labels)
n_labels = len(set(labels))
train_labeled_idxs = []
train_unlabeled_idxs = []
for i in range(n_labels):
idxs = np.where(labels == i)[0]
np.random.shuffle(idxs)
train_labeled_idxs.extend(idxs[:n_labeled_per_class])
if unlabeled_per_class:
train_unlabeled_idxs.extend(idxs[n_labeled_per_class:n_labeled_per_class+unlabeled_per_class])
else:
train_unlabeled_idxs.extend(idxs[n_labeled_per_class:])
np.random.shuffle(train_labeled_idxs)
np.random.shuffle(train_unlabeled_idxs)
return train_labeled_idxs, train_unlabeled_idxs
n_labeled_per_class = n_labeled_per_class
n_unlabeled_per_class = n_unlabeled_per_class
labels = list(target_train_df["class_label"])
train_labeled_idxs, train_unlabeled_idxs = train_split(labels, n_labeled_per_class, unlabeled_per_class=n_unlabeled_per_class)
target_train_labeled_df = target_train_df.iloc[train_labeled_idxs]
target_train_unlabeled_df = target_train_df.iloc[train_unlabeled_idxs]
print('\nn_labeled_per_class: ', n_labeled_per_class) # labeled target samples per class
print('n_unlabeled_per_class: ', n_unlabeled_per_class) # labeled target samples per class
print('target_train_labeled_df samples: %d' % (target_train_labeled_df.shape[0]))
print('target_train_unlabeled_df samples: %d' % (target_train_unlabeled_df.shape[0]))
# check n_smaples_per_class
print('Check n_smaples_per_class in the original training set: ', target_train_df['class_label'].value_counts().to_dict())
print('Check n_smaples_per_class in the clearned labeled training set: ', target_train_labeled_df['class_label'].value_counts().to_dict())
print('Check n_smaples_per_class in the clearned unlabeled training set: ', target_train_unlabeled_df['class_label'].value_counts().to_dict())
# return 1
# sys.exit()
##### Create Torch Dataset and DataLoader
from transformers import BertTokenizer
## Load the BERT tokenizer.
print('\nLoading BERT tokenizer...')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)
## Data Augmentation
from utils.aug import aug
## Create Torch Dataset and DataLoader
from torch.utils.data import DataLoader
class TextDataset(torch.utils.data.Dataset):
def __init__(self, df, test=False, return_list=False):
self.sentences = list(df['cleaned_tweet'])
self.labels = list(df['class_label'])
self.sample_ids = list(df['tweet_id'])
self.test = test
self.return_list = return_list
def __len__(self):
return len(self.sentences)
def __getitem__(self, idx):
sentence, label = self.sentences[idx], self.labels[idx]
encodings = tokenizer.encode_plus(sentence, max_length=max_length, truncation=True, padding='max_length', return_attention_mask=True)
item = {key: torch.tensor(val) for key, val in encodings.items()}
if not self.test:
item["labels"] = torch.tensor(label)
sample_id = self.sample_ids[idx]
item['sample_ids'] = sample_id
if self.return_list:
return [item]
else:
return item
class AugDataset_L(torch.utils.data.Dataset):
def __init__(self, df, test=False, aug_dict=None):
self.sentences = list(df['cleaned_tweet'])
self.labels = list(df['class_label'])
self.sample_ids = list(df['tweet_id'])
self.test = test
self.aug_dict = aug_dict
def __len__(self):
return len(self.sentences)
def __getitem__(self, idx):
sentence, label = self.sentences[idx], self.labels[idx]
# select only one aug sentence for labeled data
num_aug, alpha_sr, alpha_rs = aug_dict["num_aug"], aug_dict["alpha_sr"], aug_dict["alpha_rs"]
aug_sentence = aug(sentence, num_aug=num_aug, alpha_sr=alpha_sr, alpha_rs=alpha_rs, return_ori=False)[0]
encodings = tokenizer.encode_plus(aug_sentence, max_length=max_length, truncation=True, padding='max_length', return_attention_mask=True)
item = {key: torch.tensor(val) for key, val in encodings.items()}
if not self.test:
item["labels"] = torch.tensor(label)
sample_id = self.sample_ids[idx]
item['sample_ids'] = sample_id
return item
class AugDataset_U(torch.utils.data.Dataset):
def __init__(self, df, test=False, aug_dict=None):
self.sentences = list(df['cleaned_tweet'])
self.labels = list(df['class_label'])
self.sample_ids = list(df['tweet_id'])
self.test = test
self.aug_dict = aug_dict
def __len__(self):
return len(self.sentences)
def __getitem__(self, idx):
sentence, label = self.sentences[idx], self.labels[idx]
num_aug, alpha_sr, alpha_rs = aug_dict["num_aug"], aug_dict["alpha_sr"], aug_dict["alpha_rs"]
aug_sentences = aug(sentence, num_aug=num_aug, alpha_sr=alpha_sr, alpha_rs=alpha_rs, return_ori=True)
items = []
for s in aug_sentences:
encodings = tokenizer.encode_plus(s, max_length=max_length, truncation=True, padding='max_length', return_attention_mask=True)
item = {key: torch.tensor(val) for key, val in encodings.items()}
if not self.test:
item["labels"] = torch.tensor(label)
sample_id = self.sample_ids[idx]
item['sample_ids'] = sample_id
items.append(item)
return items
class AugDataset_U_ws(torch.utils.data.Dataset):
def __init__(self, df, test=False, aug_dict=None, strong_aug=None, train_unlabeled_idxs=None):
self.sentences = list(df['cleaned_tweet'])
self.labels = list(df['class_label'])
self.sample_ids = list(df['tweet_id'])
self.test = test
self.aug_dict = aug_dict
self.strong_aug = strong_aug
self.train_unlabeled_idxs = train_unlabeled_idxs
def __len__(self):
return len(self.sentences)
def __getitem__(self, idx):
sentence, label = self.sentences[idx], self.labels[idx]
# - weak aug
num_aug, alpha_sr, alpha_rs = aug_dict["num_aug"], aug_dict["alpha_sr"], aug_dict["alpha_rs"]
w_aug = aug(sentence, num_aug=num_aug, alpha_sr=alpha_sr, alpha_rs=alpha_rs, return_ori=True)[0]
# - strong aug
s_aug = self.strong_aug.iloc[self.train_unlabeled_idxs[idx]][random.randint(0,1)]
# - strong_weak aug: avoid overfit on limited strong aug sentences
# s_aug = aug(s_aug, num_aug=1, alpha_sr=alpha_sr, alpha_rs=alpha_rs, return_ori=True)
# print('Tree s_aug: ', s_aug)
# print('Tree ori: ', sentence)
aug_sentences = [w_aug, s_aug]
items = []
for s in aug_sentences:
encodings = tokenizer.encode_plus(s, max_length=max_length, truncation=True, padding='max_length', return_attention_mask=True)
item = {key: torch.tensor(val) for key, val in encodings.items()}
if not self.test:
item["labels"] = torch.tensor(label)
sample_id = self.sample_ids[idx]
item['sample_ids'] = sample_id
items.append(item)
return items
# - Create Datasets
target_dev_dataset, target_test_dataset = TextDataset(target_dev_df), TextDataset(target_test_df)
if aug_mode:
if strong_aug_mode:
bt_data_path = 'data/hurricane/bt_data.csv' # back-translated data path
df_bt_dta = pd.read_csv(bt_data_path)
target_train_labeled_dataset, target_train_unlabeled_dataset = AugDataset_L(target_train_labeled_df, aug_dict=aug_dict), AugDataset_U_ws(target_train_unlabeled_df, aug_dict=aug_dict, strong_aug=df_bt_dta, train_unlabeled_idxs=train_unlabeled_idxs)
else:
target_train_labeled_dataset, target_train_unlabeled_dataset = AugDataset_L(target_train_labeled_df, aug_dict=aug_dict), AugDataset_U(target_train_unlabeled_df, aug_dict=aug_dict)
elif aug_mode==False and psl_mode==True:
target_train_labeled_dataset, target_train_unlabeled_dataset = TextDataset(target_train_labeled_df), TextDataset(target_train_unlabeled_df, return_list=True)
else:
target_train_labeled_dataset, target_train_unlabeled_dataset = TextDataset(target_train_labeled_df), TextDataset(target_train_unlabeled_df)
# - Create DataLoaders
# We'll take training samples in random order.
target_train_labeled_loader= DataLoader(target_train_labeled_dataset, batch_size=bs, shuffle=True)
target_train_unlabeled_loader= DataLoader(target_train_unlabeled_dataset, batch_size=bs_u, shuffle=True)
target_dev_loader = DataLoader(target_dev_dataset, batch_size=bs, shuffle=True)
target_test_loader = DataLoader(target_test_dataset, batch_size=bs, shuffle=True)
##### Model & Optimizer & Learning Rate Scheduler
## Model
from models.Bert4Mixup import Bert4MixupForSequenceClassification
net = Bert4MixupForSequenceClassification.from_pretrained(
"bert-base-uncased", # Use the 12-layer BERT model, with an uncased vocab.
num_labels = n_labels, # 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.
)
# Tell pytorch to run this model on the GPU.
net.to(device)
## Optimizer & Learning Rate Scheduler
# Note: AdamW is a class from the huggingface library (as opposed to pytorch), 'W' stands for 'Weight Decay fix"
optimizer_net = AdamW(net.parameters(),
lr = lr, # args.learning_rate - default is 5e-5, our notebook had 2e-5
eps = 1e-8 # args.adam_epsilon - default is 1e-8.
)
## EMA Initialization
net.train()
if ema_mode:
ema = EMA(net, ema_momentum)
ema.register()
##### Training Loop
## Helper function for training
import time
import datetime
def format_time(elapsed):
'''
Takes a time in seconds and returns a string hh:mm:ss
'''
# Round to the nearest second.
elapsed_rounded = int(round((elapsed)))
# Format as hh:mm:ss
return str(datetime.timedelta(seconds=elapsed_rounded))
def linear_rampup(current, rampup_length):
if rampup_length == 0:
return 1.0
else:
current = np.clip(current / rampup_length, 0.0, 1.0)
return float(current)
def save_model(save_name, save_path):
save_filename = os.path.join(save_path, save_name)
# copy EMA parameters to ema_model for saving with model as temp
net.eval()
# use ema model for evaluation
if ema_mode:
ema.apply_shadow()
ema_model = net.state_dict()
# restore training mode
if ema_mode:
ema.restore()
net.train()
torch.save({'model': net.state_dict(),
'optimizer': optimizer_net.state_dict(),
# 'scheduler': scheduler.state_dict(),
# 'it': it,
'ema_model': ema_model},
save_filename)
print(f"model saved: {save_filename}")
## Evaluation
# define evaluation metrics
import torch
from torchmetrics import F1Score
from torchmetrics import Accuracy
from torchmetrics.classification import MulticlassConfusionMatrix
f1 = F1Score(num_classes=n_labels, average='macro')
accuracy = Accuracy(num_classes=n_labels, average='weighted')
accuracy_classwise = Accuracy(num_classes=n_labels, average='none')
confusion_matrix = MulticlassConfusionMatrix(num_classes=n_labels)
@torch.no_grad()
def evaluation(loader, final_eval=False):
"""Evaluation"""
# print("\nRunning Evaluation...")
t1 = time.time()
# Put the model in evaluation mode--the dropout layers behave differently
# during evaluation.
net.eval()
if ema_mode:
# use ema model for evaluation
ema.apply_shadow() # does ema model need .eval() and .train()?
# Tracking variables
total_eval_accuracy = 0
total_eval_loss = 0
total_eval_f1 = 0
# - For calculating classwise accuracy, note: need to avoid nan value when there is a class that does not have any data
preds_all = []
target_all = []
# Evaluate data for one epoch
for batch in loader:
b_input_ids = batch['input_ids'].to(device)
b_input_mask = batch['attention_mask'].to(device)
b_labels = batch['labels'].to(device)
# Tell pytorch not to bother with constructing the compute graph during
# the forward pass, since this is only needed for backprop (training).
with torch.no_grad():
result = net(b_input_ids, token_type_ids=None,attention_mask=b_input_mask,
labels=b_labels,return_dict=True)
loss = result.loss
logits = result.logits
# Move logits and labels to CPU
probs = torch.softmax(logits, dim=1)
preds = torch.argmax(probs, dim=1).cpu()
target = b_labels.cpu()
# Calculate the accuracy for this batch of test sentences, and
# accumulate over all batches.
total_eval_accuracy += accuracy(preds, target).item()
total_eval_f1 += f1(preds, target).item()
total_eval_loss += loss.item()
# For calculating classwise acc
preds_all.append(preds)
target_all.append(target)
# Report the final accuracy, f1, loss for this validation run.
avg_val_accuracy = total_eval_accuracy / len(loader)
avg_val_f1 = total_eval_f1 / len(loader)
avg_val_loss = total_eval_loss / len(loader)
# Calculate classwise acc
accuracy_classwise_ = accuracy_classwise(torch.cat(preds_all), torch.cat(target_all)).numpy().round(3)
# print('===accuracy_classwise_', accuracy_classwise_)
if final_eval:
confmat_result = confusion_matrix(torch.cat(preds_all), torch.cat(target_all))
return avg_val_accuracy, avg_val_f1, avg_val_loss, list(accuracy_classwise_), confmat_result
else:
return avg_val_accuracy, avg_val_f1, avg_val_loss, list(accuracy_classwise_)
## Kick off the training!
import torch.nn.functional as F
t0 = time.time() # Measure how long the training epoch takes.
start = 0
best_acc = 0
best_model_step = 0
pslt_global, pslt_confidence = 0, 0
cw_u_avg_prob, cw_u_avg_conf = torch.zeros(n_labels), torch.zeros(n_labels)
psl_total_eval = 0
psl_correct_eval = 0
cw_psl_total, cw_psl_correct = torch.zeros(n_labels, dtype=int), torch.zeros(n_labels, dtype=int)
cw_psl_total_eval, cw_psl_correct_eval = torch.zeros(n_labels, dtype=int), torch.zeros(n_labels, dtype=int)
cw_psl_total_accum, cw_psl_correct_accum = torch.zeros(n_labels, dtype=int), torch.zeros(n_labels, dtype=int)
training_stats = []
print('\n\nn_labeled_per_class: ', n_labeled_per_class) # labeled target samples per class
print("Aug_mode: ", aug_mode)
print("Aug_dict: ", aug_dict)
print('Total steps: ', steps)
criterion = nn.CrossEntropyLoss()
data_iter_t = iter(target_train_labeled_loader)
data_iter_t_unl = iter(target_train_unlabeled_loader)
len_train_target = len(target_train_labeled_loader)
len_train_target_unl = len(target_train_unlabeled_loader)
# self-adaptive thresholding setting
p_model = (torch.ones(n_labels) / n_labels).to(device)
time_p = p_model.mean()
# initial qhat, ema_mean_prob, ema_mean_conf
qhat = (torch.ones([1, n_labels], dtype=torch.float)/n_labels).to(device)
ema_mean_prob = (torch.ones([1, n_labels], dtype=torch.float)/n_labels).to(device)
ema_mean_conf = (torch.ones([1, n_labels], dtype=torch.float)/n_labels).to(device)
# initialize memorybank
if use_memorybank:
memorybank = MemoryBank(n_classes=n_labels, memory_per_class = memory_per_class,
selection_strategy=selection_strategy, selection_top_threshold=selection_top_threshold, selection_sharpening_T=selection_sharpening_T)
# initiliaze AUMRecorder
all_sample_ids = target_train_df['tweet_id']
labels = target_train_df["class_label"]
AUMRecords = AUMRecorder(all_sample_ids, labels)
net.train()
for step in range(start, steps+1):
# --- Check Performance on Validation set every val_interval batches. ---#
# if step % val_interval == 0 and not step == 0:
if step % val_interval == 0:
acc_test, f1_test, loss_test, acc_test_cw = evaluation(target_test_loader)
acc_val, f1_val, loss_val, acc_val_cw = evaluation(target_dev_loader)
acc_train, f1_train, loss_train, acc_train_cw = evaluation(target_train_labeled_loader)
# check accuracy of pseudo-labels
# avg_psl_acc_eval = total_psl_acc_eval/val_interval
# total_psl_acc_eval = 0
# restore training mode
if ema_mode:
ema.restore()
net.train()
print('Step %d acc %f f1 %f loss %f acc_train %f f1_train %f loss_train %f acc_val %f f1_val %f loss_val %f psl_cor %d psl_totl %d pslt_global %f pslt_confidence %f' %
(step, acc_test, f1_test, loss_test, acc_train, f1_train, loss_train, acc_val, f1_val, loss_val, psl_correct_eval, psl_total_eval, pslt_global, pslt_confidence),
'Tim {:}'.format(format_time(time.time() - t0)))
# Record all statistics from this evaluation.
training_stats.append(
{ 'step': step,
'acc_test': acc_test,
'f1_test': f1_test,
'loss_test': loss_test,
'acc_train': acc_train,
'f1_train': f1_train,
'loss_train': loss_train,
'acc_val': acc_val,
'f1_val': f1_val,
'loss_val': loss_val,
'psl_correct': psl_correct_eval,
'psl_total': psl_total_eval,
'pslt_global': pslt_global,
'pslt_confidence': pslt_confidence,
'cw_acc_train': acc_train_cw,
'cw_acc_val': acc_val_cw,
'cw_acc_test': acc_test_cw,
'cw_u_avg_prob': cw_u_avg_prob.tolist(),
'cw_u_avg_conf': cw_u_avg_conf.tolist(),
# 'cw_psl_total': cw_psl_total.tolist(),
# 'cw_psl_correct': cw_psl_correct.tolist(),
'cw_psl_total_eval': cw_psl_total_eval.tolist(),
'cw_psl_correct_eval': cw_psl_correct_eval.tolist(),
'cw_psl_acc_eval': (cw_psl_correct_eval/cw_psl_total_eval).tolist(),
'cw_psl_total_accum': cw_psl_total_accum.tolist(),
'cw_psl_correct_accum': cw_psl_correct_accum.tolist(),
'cw_psl_acc_accum': (cw_psl_correct_accum/cw_psl_total_accum).tolist(),
})
if psl_mode:
# check classwise psl accuracy for the current eval
print('Tree test: ', cw_psl_total_eval.tolist(), cw_psl_correct_eval.tolist())
print('Tree test: ', (cw_psl_correct_eval/cw_psl_total_eval).tolist())
# Early stopping & Save best model
# - best criterion: acc_val (TODO: consider changes to avg of acc and F1 ?)
if acc_val >= best_acc:
best_acc = acc_val
best_model_step = step
early_stop_count = 0
save_model('model_best.pth', output_dir_path)
else:
early_stop_count+=1
if early_stop_count >= early_stop_tolerance:
print('Early stopping trigger at step: ', step)
break
# initialize pseudo labels evaluation
psl_total_eval, psl_correct_eval = 0, 0
cw_psl_total_eval, cw_psl_correct_eval = torch.zeros(n_labels, dtype=int), torch.zeros(n_labels, dtype=int)
# --- Done ---#
if step % len_train_target == 0:
data_iter_t = iter(target_train_labeled_loader)
if step % len_train_target_unl == 0:
data_iter_t_unl = iter(target_train_unlabeled_loader)
data_t = next(data_iter_t)
data_t_unl = next(data_iter_t_unl)
bs_l_actual = data_t['input_ids'].size()[0]
# prepare model input data and target: labeled and unlabeled
if psl_mode==True:
# Telling the model not to compute or store gradients, saving memory and speeding up prediction
with torch.no_grad():
if consis_reg == 'weak_sup_strong':
# if strong_aug_mode:
# use w_aug to generate psl for supervising s_aug -> consistency regularizaion
w_aug_data = data_t_unl[0]
out = net(w_aug_data['input_ids'].to(device),
attention_mask=w_aug_data['attention_mask'].to(device),
token_type_ids=None,
labels=None,
return_dict=True
)
logits = out.logits
p_avg = torch.softmax(logits, dim=1)
l_avg = logits
else:
# average predictions -> consistency regularization
p_augs = []
l_augs = []
for aug_data in data_t_unl:
# aug_data['input_ids'], aug_data['attention_mask'], aug_data['labels']
out = net(aug_data['input_ids'].to(device),
attention_mask=aug_data['attention_mask'].to(device),
token_type_ids=None,
labels=None,
return_dict=True
)
logits = out.logits
p = torch.softmax(logits, dim=1)
p_augs.append(p)
l_augs.append(logits)
probs_u = torch.cat(p_augs)
p_avg = torch.mean(torch.stack(p_augs), dim=0)
l_avg = torch.mean(torch.stack(l_augs), dim=0)
## Label guessing: select high confident predictions as pseudo-labels: None, hard_t, sat
max_probs, max_idx = torch.max(p_avg, dim=-1)
pslt_confidence = max_probs.mean().item()
# print('===max_probs: ', max_probs)
# print('===p_avg: ', p_avg)
## -Info
# Count cw_u_avg_prob(mean_prob), cw_u_avg_conf(mean_conf) for all unlabeled data
current_logit = l_avg
mean_prob = torch.softmax(current_logit, dim=-1).mean(dim=0)
mean_conf = (torch.zeros(n_labels) / n_labels).to(device) # repeat
for i in range(n_labels):
# if there exists samples in class i, update mean confidence
if sum(max_idx==i) > 0:
mean_conf[i] = max_probs[max_idx==i].mean()
# if there does not exist samples in class i, mean confidence stays the same
else:
mean_conf[i] = ema_mean_conf[0, i]
ema_mean_prob = ema_momentum * ema_mean_prob + (1 - ema_momentum) * mean_prob
ema_mean_conf = ema_momentum * ema_mean_conf + (1 - ema_momentum) * mean_conf
cw_u_avg_prob, cw_u_avg_conf = ema_mean_prob[0], ema_mean_conf[0]
# print('Tree testt: ', ema_mean_prob[0])
# Update/Compute AUM related info to AUMRecords
for aug_data in data_t_unl:
sample_ids = aug_data['sample_ids'].tolist()
logits = l_avg
AUMRecords.update(logits, sample_ids)
# Retrieve AUM-related selection information
if selection_strategy is not None:
if selection_metrics == 'margin':
margin_metrics = l_avg # margin between the largest two logits/probs
top2 = torch.topk(margin_metrics,2).values
selection_scores = top2[:,0] - top2[:,1]
elif selection_metrics == 'aum':
# Retrieve aum from AUMRecords
aug_data = data_t_unl[0] # aum info same for all weak augmentations of the sample data
sample_ids = aug_data['sample_ids'].tolist()
selection_scores = torch.tensor([AUMRecords.records[sample_id]['aum'][-1] for sample_id in sample_ids])
if threshold_mode=='hard_t':
# hard threshold
pslt_global = psl_threshold_h
u_psl_mask = max_probs >= pslt_global
elif threshold_mode == 'debias':
# debias by adjusting logits (or probabilities) via mean_probs
current_logit = l_avg
debiased_prob = F.softmax(current_logit - tau*torch.log(qhat), dim=1)
# - update qhat
mean_prob = torch.softmax(current_logit, dim=-1).mean(dim=0) #repeat
qhat = ema_momentum * qhat + (1 - ema_momentum) * mean_prob
# - get psl_mask
debiased_max_probs, debiased_max_idx = torch.max(debiased_prob, dim=-1)
pslt_global = psl_threshold_h
u_psl_mask = debiased_max_probs.ge(pslt_global)
elif threshold_mode == 'debias2':
# debias by adjusting logits via mean_conf
current_logit = l_avg
debiased_prob = F.softmax(current_logit - tau*torch.log(qhat), dim=1)
# - update qhat by confidence of each class
mean_conf = (torch.zeros(n_labels) / n_labels).to(device) # repeat
for i in range(n_labels):
# if there exists samples in class i, update mean confidence
if sum(max_idx==i) > 0:
mean_conf[i] = max_probs[max_idx==i].mean()
# if there does not exist samples in class i, mean confidence stays the same
else:
mean_conf[i] = qhat[0, i]
qhat = ema_momentum * qhat + (1 - ema_momentum) * mean_conf
# - get psl_mask
debiased_max_probs, debiased_max_idx = torch.max(debiased_prob, dim=-1)
pslt_global = psl_threshold_h
u_psl_mask = debiased_max_probs.ge(pslt_global)
# TODO: record class-wise mean confidence
print(qhat)
elif threshold_mode=='sat':
# self-adaptive threshold
time_p = time_p * ema_momentum + max_probs.mean() * (1-ema_momentum)
# time_p = max_probs.mean()
pslt_global = time_p
p_model = p_model * ema_momentum + torch.mean(probs_u, dim=0) * (1-ema_momentum)
p_model_cutoff = p_model / torch.max(p_model,dim=-1)[0]
u_psl_mask = max_probs.ge(pslt_global * p_model_cutoff[max_idx])
elif threshold_mode=='sat_global':
# self-adaptive threshold
time_p = time_p * ema_momentum + max_probs.mean() * (1-ema_momentum)
# time_p = max_probs.mean()
pslt_global = time_p
u_psl_mask = max_probs.ge(pslt_global)
elif threshold_mode=='linear_rampup':
# hard threshold
pslt_global = psl_threshold_h * linear_rampup(step, threshold_rampup_length)
u_psl_mask = max_probs >= pslt_global
## pseudo-labeling mode
if labeling_mode=='soft':
# soft labels
u_label_psl = p_avg[u_psl_mask]
u_label_psl = u_label_psl.detach()
elif labeling_mode=='sharpening':
# soft labels + sharpening
p_avg = p_avg[u_psl_mask]
pt = p_avg**(1/sharpening_T)
u_label_psl = pt / pt.sum(dim=1, keepdim=True)
u_label_psl = u_label_psl.detach()
else:
# hard labels
u_label_psl = max_idx[u_psl_mask]
u_label_psl = F.one_hot(u_label_psl, num_classes=n_labels).to(device)
if consis_reg == 'weak_sup_strong':
s_aug_data = data_t_unl[1]
u_labels_psl = u_label_psl
u_inputs_psl = s_aug_data['input_ids'][u_psl_mask]
u_masks_psl = s_aug_data['attention_mask'][u_psl_mask]
else:
u_labels_psl = torch.cat([u_label_psl for i in range(len(data_t_unl))])
u_inputs_psl = torch.cat([_['input_ids'][u_psl_mask] for _ in data_t_unl])
u_masks_psl = torch.cat([_['attention_mask'][u_psl_mask] for _ in data_t_unl])
if selection_strategy is not None:
u_selection_scores_psl = torch.cat([selection_scores[u_psl_mask] for i in range(len(data_t_unl))])
## investigate influence of incorrect psl, # psl, both
if investigate == 'psl_acc':
# (a) delete incorrect psl (invest. acc)
gt_labels_u = data_t_unl[0]['labels'][u_psl_mask].to(device)
_, u_label_psl_hard = torch.max(u_label_psl, dim=-1)
mask_cor_psl = u_label_psl_hard == gt_labels_u
u_labels_psl = torch.cat([u_label_psl[mask_cor_psl] for i in range(len(data_t_unl))])
u_inputs_psl = torch.cat([_['input_ids'][u_psl_mask][mask_cor_psl] for _ in data_t_unl])
u_masks_psl = torch.cat([_['attention_mask'][u_psl_mask][mask_cor_psl] for _ in data_t_unl])
elif investigate == 'psl_num':
# (b) use same # psl per classs for training per iteration, here psl are not guaranteed to be correct (invest. num) -> (memory bank)
use_memorybank = True
elif investigate == 'psl_acc_num':
# (c) replace incorrect psl with correct psl (invest. both) -> results is quite interesting and counter-intuitive, need further pondering!!!
u_labels_psl = torch.cat([F.one_hot(_['labels'], num_classes=n_labels)[u_psl_mask] for _ in data_t_unl]).to(device)
# (d) use same # psl per classs for training per iteration, replace incorrect psl with correct psl (invest. both)
use_memorybank = True
if not use_memorybank:
labels_l_onehot = F.one_hot(data_t['labels'], num_classes=n_labels).to(device)
all_labels = torch.cat([labels_l_onehot, u_labels_psl])
all_inputs = torch.cat([data_t['input_ids'], u_inputs_psl])
all_masks = torch.cat([data_t['attention_mask'], u_masks_psl])
else:
# update memorybank
if selection_strategy is not None:
memorybank.update_memorybank(u_inputs_psl, u_masks_psl, u_labels_psl, selection_score=u_selection_scores_psl)
else:
memorybank.update_memorybank(u_inputs_psl, u_masks_psl, u_labels_psl)
# sample memorybank
if sampling_strategy == 'avg_probs':
sampled_input, sampled_mask, sampled_label = memorybank.sample_memorybank(samples_per_class, cw_mean_prob=cw_u_avg_prob, t=sampling_temperature)
else:
sampled_input, sampled_mask, sampled_label = memorybank.sample_memorybank(samples_per_class)
labels_l_onehot = F.one_hot(data_t['labels'], num_classes=n_labels)
all_labels = torch.cat([labels_l_onehot, sampled_label]).to(device)
all_inputs = torch.cat([data_t['input_ids'], sampled_input])
all_masks = torch.cat([data_t['attention_mask'], sampled_mask])
data = all_inputs
mask = all_masks
target = all_labels
## check the total and correct number of pseudo-labels
# psl_total = u_labels_psl.shape[0]
# _, u_label_psl_hard = torch.max(u_labels_psl, dim=-1)
# gt_labels_u = torch.cat([_['labels'][u_psl_mask] for _ in data_t_unl])
# psl_correct = torch.sum(u_label_psl_hard == gt_labels_u).item()
gt_labels_u = data_t_unl[0]['labels'][u_psl_mask].to(device)
psl_total = torch.sum(u_psl_mask).item()
_, u_label_psl_hard = torch.max(u_label_psl, dim=-1)
psl_correct = torch.sum(u_label_psl_hard == gt_labels_u).item()
psl_total_eval += psl_total
psl_correct_eval += psl_correct
# check class-wise total and correct number of pseudo-labels
cw_psl_total = torch.bincount(u_label_psl_hard, minlength=n_labels).to('cpu')
cw_psl_correct = torch.bincount(u_label_psl_hard[u_label_psl_hard == gt_labels_u], minlength=n_labels).to('cpu')
cw_psl_total_eval += cw_psl_total
cw_psl_correct_eval += cw_psl_correct
cw_psl_total_accum += cw_psl_total
cw_psl_correct_accum += cw_psl_correct
else:
data = data_t['input_ids']
mask = data_t['attention_mask']
target = data_t['labels']
mixup_dict = None
if mixup_mode:
# mixup preparation
mixup_dict = {}
mixup_layers = mixup_layers
mixup_dict["layer_num"] = np.random.choice(mixup_layers)
lam = np.random.beta(mixup_alpha, mixup_alpha)
mixup_dict["lam"] = max(lam, 1-lam)