-
Notifications
You must be signed in to change notification settings - Fork 5
/
experiments.py
1453 lines (1216 loc) · 58.8 KB
/
experiments.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
from dataload import DPRewriteDataset
from models.downstream_models.bert_downstream import BertDownstream
from utils import decode_rewritten, EarlyStopping
from models.autoencoders.adept import ADePT, ADePTModelConfig
from models.autoencoders.dp_bart import DPBart, DPBartModelConfig
from models.autoencoders.custom import CustomModel_RNN, CustomModel_Transformer, CustomModelConfig
import matplotlib.pyplot as plt
import pandas as pd
import time
import json
from tqdm import tqdm
from copy import deepcopy
import os
import tempfile
from abc import ABC, abstractmethod
from settings import Settings
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from sklearn.metrics import precision_recall_fscore_support
from sacrebleu.metrics import BLEU
from bert_score import BERTScorer
os.environ['MPLCONFIGDIR'] = os.getcwd() + '/configs/'
import pdb
from utils import load_neurons_for_pruning, determine_neurons_to_prune, add_neurons_to_prune
import warnings
warnings.filterwarnings('ignore')
class Experiment(ABC):
def __init__(self, ss:Settings):
# General vars and directories
self.seed = ss.args.seed
self.local = ss.args.local
self.local_iter_size = ss.args.local_iter_size
self.mode = ss.args.mode
self.exp_output_dir = ss.exp_output_dir
self.exp_dump_dir = ss.exp_dump_dir
self.checkpoint_dir = ss.checkpoint_dir
self.asset_dir = ss.args.asset_dir
self.embed_dir_unprocessed = ss.args.embed_dir_unprocessed
self.embed_dir_processed = ss.embed_dir_processed
self.dataset_name = ss.args.dataset
self.custom_train_path = ss.args.custom_train_path
self.custom_valid_path = ss.args.custom_valid_path
self.custom_test_path = ss.args.custom_test_path
self.last_checkpoint_path = ss.args.last_checkpoint_path
self.save_unique_checkpoint_every_epoch =\
ss.args.save_unique_checkpoint_every_epoch
self.length_threshold = ss.args.length_threshold
self.custom_preprocessor = ss.args.custom_preprocessor
self.data_split_cutoff = ss.args.data_split_cutoff
self.iteration_cutoff = ss.args.iteration_cutoff
if self.local:
self.device = torch.device('cpu')
else:
self.device = torch.device(
'cuda' if torch.cuda.is_available() else 'cpu')
# Hyperparameters (general)
self.model = ss.args.model.lower()
self.model_type = ss.args.model_type
self.max_seq_len = ss.args.max_seq_len
self.optim_type = ss.args.optim_type
self.epochs = ss.args.epochs
self.batch_size = ss.args.batch_size
self.learning_rate = ss.args.learning_rate
self.weight_decay = ss.args.weight_decay
self.early_stop = ss.args.early_stopping
self.patience = ss.args.patience
self.pruning = ss.args.pruning
self.prune_finetune_k = ss.args.prune_finetune_k
self.pruning_index_path = ss.args.pruning_index_path
self.gradually_increase_pruning = ss.args.gradually_increase_pruning
self.early_stopping = EarlyStopping(self.patience)
self.two_optimizers = ss.args.two_optimizers
self.optimizer = None
self.enc_optimizer = None
self.dec_optimizer = None
self.loss = None
# Hyperparameters (specific to models)
self.transformer_type = ss.args.transformer_type
self.train_teacher_forcing_ratio = ss.args.train_teacher_forcing_ratio
# only for transformer-based models
self.hidden_size = ss.args.hidden_size
self.enc_out_size = ss.args.enc_out_size
# general for experiments that add DP module after encoder outputs
self.embed_type = ss.args.embed_type
self.vocab_size = ss.args.vocab_size
# for experiments with non-HF-based tokenizers
self.embed_size = ss.args.embed_size
self.custom_model_arguments = ss.args.custom_model_arguments
# Private parameters (not all necessary, depending on DP module):
self.private = ss.args.private
self.epsilon = ss.args.epsilon
self.delta = ss.args.delta
self.clipping_constant = ss.args.clipping_constant
self.norm_ord = ss.args.l_norm
self.dp_module = ss.args.dp_module
self.dp_mechanism = ss.args.dp_mechanism
self.discretize = ss.args.discretize
if (self.dp_mechanism == 'gaussian' or self.dp_mechanism == 'analytic_gaussian') and self.norm_ord == 1:
print(f"\n+++ WARNING: Using {self.dp_mechanism} noise with norm order {self.norm_ord}. +++\n")
# Additional settings
self.no_clipping = ss.args.no_clipping
self.save_initial_model = ss.args.save_initial_model
self.prepend_labels = ss.args.prepend_labels
self.train_ratio = ss.args.train_ratio
self.downstream_test_data = ss.args.downstream_test_data
# General variables for experiments
self.trainable_params = 0
self.train_losses = []
self.valid_losses = []
# Variables for evaluation metrics
self.bleu = BLEU()
if self.device == torch.device('cuda'):
self.run_bert_score = True
self.bert_scorer = BERTScorer(
lang='en', rescale_with_baseline=True,
batch_size=4)
else:
self.run_bert_score = False
self.temp_train_file_original = None
self.temp_train_file_preds = None
self.temp_valid_file_original = None
self.temp_valid_file_preds = None
# Write configuration and various stats to json files for documentation
self.stats = {}
config = {key: value for key, value in ss.args.__dict__.items()
if not key.startswith('__') and not callable(key)}
with open(os.path.join(self.exp_output_dir, 'config.json'), 'w',
encoding='utf-8') as f:
json.dump(config, f, ensure_ascii=False, indent=4)
def epoch_time(self, start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
@abstractmethod
def _load_checkpoint(self):
pass
@abstractmethod
def train_iteration(self):
pass
@abstractmethod
def evaluate(self):
pass
@abstractmethod
def train(self):
pass
@abstractmethod
def plot_learning_curve(self):
pass
@abstractmethod
def run_experiment(self):
pass
class PretrainExperiment(Experiment):
def __init__(self, ss: Settings):
super().__init__(ss)
self.dataset = DPRewriteDataset(
self.dataset_name, self.asset_dir, self.checkpoint_dir,
self.max_seq_len, self.batch_size, mode=self.mode,
embed_type=self.embed_type, train_ratio=self.train_ratio,
embed_size=self.embed_size,
embed_dir_processed=self.embed_dir_processed,
embed_dir_unprocessed=self.embed_dir_unprocessed,
transformer_type=self.transformer_type,
vocab_size=self.vocab_size,
model_type=self.model_type, private=self.private,
prepend_labels=self.prepend_labels,
length_threshold=self.length_threshold,
data_split_cutoff=self.data_split_cutoff,
custom_preprocessor=self.custom_preprocessor,
local=self.local,
custom_train_path=self.custom_train_path,
custom_valid_path=self.custom_valid_path,
custom_test_path=self.custom_test_path,
downstream_test_data=self.downstream_test_data
)
self.dataset.load_and_process()
print('Initializing model...')
self._init_model()
def _init_model_config(self):
# Setting the padding index
if self.model_type == 'rnn':
pad_idx = self.dataset.preprocessor.word2idx[
self.dataset.preprocessor.PAD]
else: # 'transformer'
pad_idx = self.dataset.preprocessor.tokenizer.pad_token_id
self.pad_idx = pad_idx
# Preparing the general model configuration
general_config_dict = {
'max_seq_len': self.max_seq_len, 'batch_size': self.batch_size,
'mode': self.mode, 'local': self.local, 'device': self.device,
'hidden_size': self.hidden_size,
'enc_out_size': self.enc_out_size,
'embed_size': self.embed_size, 'pad_idx': pad_idx,
'transformer_type': self.transformer_type,
'private': self.private, 'epsilon': self.epsilon,
'delta': self.delta, 'norm_ord': self.norm_ord,
'clipping_constant': self.clipping_constant,
'dp_mechanism': self.dp_mechanism,
'pruning': self.pruning,
'pruning_index_path': self.pruning_index_path,
'experiment_output_dir': self.exp_output_dir}
# Preparing the specific model configuration class
model_config = self._get_specific_model_config(general_config_dict)
return model_config
def _get_specific_model_config(self, general_config_dict):
if self.model == 'adept':
specific_config_dict = {
'pretrained_embeddings': self.dataset.preprocessor.embeds,
'vocab_size': self.dataset.preprocessor.vocab_size,
'no_clipping': self.no_clipping
}
specific_config = ADePTModelConfig(
**general_config_dict, **specific_config_dict
)
elif self.model == 'dp_bart':
specific_config_dict = {
'dp_module': self.dp_module,
'no_clipping': self.no_clipping,
'discretize': self.discretize
}
specific_config = DPBartModelConfig(
**general_config_dict, **specific_config_dict
)
elif self.model in ['custom_rnn', 'custom_transformer']:
specific_config_dict = {
'custom_config_list': self.custom_model_arguments}
specific_config = CustomModelConfig(
**general_config_dict, **specific_config_dict
)
else:
raise NotImplementedError
return specific_config
def _get_model_type(self):
if self.model == 'adept':
model_type = ADePT
elif self.model == 'dp_bart':
model_type = DPBart
elif self.model == 'custom_rnn':
model_type = CustomModel_RNN
elif self.model == 'custom_transformer':
model_type = CustomModel_Transformer
else:
raise NotImplementedError
return model_type
def _init_model(self):
model_config = self._init_model_config()
model_type = self._get_model_type()
model = model_type(model_config)
self.model = model.to(self.device)
num_params = sum(p.numel()
for p in model.parameters() if p.requires_grad)
print(f"Num parameters in model: {num_params,}")
self.stats['num_params'] = num_params
if self.optim_type == 'adam':
optimizer = optim.Adam
elif self.optim_type == 'sgd':
optimizer = optim.SGD
else:
raise Exception('Incorrect optimizer type specified.')
if self.two_optimizers:
self.enc_optimizer = optimizer(
self.model.encoder.parameters(), lr=self.learning_rate,
weight_decay=self.weight_decay)
self.dec_optimizer = optim.Adam(
self.model.decoder.parameters(), lr=self.learning_rate,
weight_decay=self.weight_decay)
else:
self.optimizer = optimizer(self.model.parameters(),
lr=self.learning_rate,
weight_decay=self.weight_decay)
mem_params = sum([param.nelement()*param.element_size() for param in model.parameters()])
mem_bufs = sum([buf.nelement()*buf.element_size() for buf in model.buffers()])
mem = mem_params + mem_bufs # in bytes
print("Estimated non-peak memory usage of model (MBs):", mem / 1000000)
self.loss = nn.CrossEntropyLoss(ignore_index=self.pad_idx)
def _load_checkpoint(self):
'''
Load existing checkpoint of a model and stats dict, if available.
Stats dict only loaded if there is an existing checkpoint.
'''
try:
mod_name = os.path.join(self.checkpoint_dir, 'checkpoint.pt')
checkpoint = torch.load(mod_name, map_location=self.device)
self.model.load_state_dict(checkpoint['model_state_dict'])
if self.two_optimizers:
self.enc_optimizer.load_state_dict(
checkpoint['enc_optimizer_state_dict'])
self.dec_optimizer.load_state_dict(
checkpoint['dec_optimizer_state_dict'])
else:
self.optimizer.load_state_dict(
checkpoint['optimizer_state_dict'])
loaded_epoch = checkpoint['checkpoint_epoch'] + 1
# Restart training from the next epoch
early_stopping_counter = checkpoint['checkpoint_early_stopping']
print(f"Loaded model from epoch {loaded_epoch} with early stopping counter at {early_stopping_counter}.")
try:
stats_path = os.path.join(self.exp_output_dir, 'stats.json')
with open(stats_path, 'r', encoding='utf-8') as f:
self.stats = json.load(f)
except:
print("Could not load existing stats dictionary.")
except:
print("Could not load checkpointed model, starting from scratch...")
loaded_epoch = 0
early_stopping_counter = 0
return loaded_epoch, early_stopping_counter
def train_iteration(self, epoch):
epoch_loss = 0
if self.local:
iter_size = self.local_iter_size
else:
iter_size = len(self.dataset.train_iterator)
self.model.train()
for idx, batch in tqdm(enumerate(self.dataset.train_iterator)):
if self.local:
if idx == iter_size:
break
if self.iteration_cutoff is not None and idx == self.iteration_cutoff:
break
if self.model_type == 'rnn':
encoder_input_ids = batch[0]
lengths = batch[1]
encoder_input_ids = encoder_input_ids.to(self.device)
inputs = {'input_ids': encoder_input_ids, 'lengths': lengths,
'teacher_forcing_ratio': self.train_teacher_forcing_ratio}
tgt = deepcopy(encoder_input_ids)[:, 1:].reshape(-1)
else:
encoder_input_ids = batch['input_ids'].to(self.device)
attention_mask = batch['attention_mask'].to(self.device)
inputs = {'input_ids': encoder_input_ids,
'attention_mask': attention_mask}
tgt = deepcopy(encoder_input_ids)[:, 1:].reshape(-1)
if self.two_optimizers:
self.enc_optimizer.zero_grad()
self.dec_optimizer.zero_grad()
else:
self.optimizer.zero_grad()
loss = 0
outputs = self.model(**inputs)
loss = self.loss(outputs, tgt)
loss.backward()
if self.two_optimizers:
torch.nn.utils.clip_grad_norm_(self.model.encoder.parameters(),
max_norm=1)
torch.nn.utils.clip_grad_norm_(self.model.decoder.parameters(),
max_norm=1)
else:
torch.nn.utils.clip_grad_norm_(self.model.parameters(),
max_norm=1)
if self.two_optimizers:
self.enc_optimizer.step()
self.dec_optimizer.step()
else:
self.optimizer.step()
epoch_loss += loss.item()
if idx == 0:
preds = torch.max(outputs, dim=1).indices.view(
encoder_input_ids.shape[0],
encoder_input_ids.shape[1] - 1)
decoded_text = decode_rewritten(
preds[0].unsqueeze(0),
self.dataset.preprocessor,
remove_special_tokens=False,
model_type=self.model_type)[0]
original = decode_rewritten(
encoder_input_ids[0][1:].unsqueeze(0),
self.dataset.preprocessor,
remove_special_tokens=False,
model_type=self.model_type)[0]
print("TRAIN ORIGINAL: ", original)
print("TRAIN PRED: ", decoded_text)
self.stats[f'sample_original_ep{epoch}_train'] = original
self.stats[f'sample_pred_ep{epoch}_train'] = decoded_text
return epoch_loss / iter_size
def evaluate(self, epoch, final=False):
epoch_loss = 0
if self.local:
iter_size = 1
else:
iter_size = len(self.dataset.valid_iterator)
self.model.eval()
with torch.no_grad():
for idx, batch in tqdm(enumerate(self.dataset.valid_iterator)):
if self.local:
if idx == iter_size:
break
if self.iteration_cutoff is not None and idx == self.iteration_cutoff:
break
if self.model_type == 'rnn':
encoder_input_ids = batch[0]
lengths = batch[1]
encoder_input_ids = encoder_input_ids.to(self.device)
inputs = {'input_ids': encoder_input_ids, 'lengths': lengths,
'teacher_forcing_ratio': 0.0}
tgt = deepcopy(encoder_input_ids)[:, 1:].reshape(-1)
else:
encoder_input_ids = batch['input_ids'].to(self.device)
attention_mask = batch['attention_mask'].to(self.device)
inputs = {'input_ids': encoder_input_ids,
'attention_mask': attention_mask}
tgt = deepcopy(encoder_input_ids)[:, 1:].reshape(-1)
if self.two_optimizers:
self.enc_optimizer.zero_grad()
self.dec_optimizer.zero_grad()
else:
self.optimizer.zero_grad()
loss = 0
outputs = self.model(**inputs)
loss = self.loss(outputs, tgt)
loss = loss.item()
epoch_loss += loss
if idx == 0 and not final:
preds = torch.max(outputs, dim=1).indices.view(
encoder_input_ids.shape[0],
encoder_input_ids.shape[1] - 1)
decoded_text = decode_rewritten(
preds[0].unsqueeze(0),
self.dataset.preprocessor,
remove_special_tokens=False,
model_type=self.model_type)[0]
original = decode_rewritten(
encoder_input_ids[0][1:].unsqueeze(0),
self.dataset.preprocessor,
remove_special_tokens=False,
model_type=self.model_type)[0]
print("VALID ORIGINAL: ", original)
print("VALID PRED: ", decoded_text)
self.stats[f'sample_original_ep{epoch}_valid'] = original
self.stats[f'sample_pred_ep{epoch}_valid'] = decoded_text
if final:
preds = torch.max(outputs, dim=1).indices.view(
encoder_input_ids.shape[0],
encoder_input_ids.shape[1] - 1)
decoded_text = decode_rewritten(
preds, self.dataset.preprocessor,
remove_special_tokens=True,
model_type=self.model_type)
original = decode_rewritten(
encoder_input_ids, self.dataset.preprocessor,
remove_special_tokens=True,
model_type=self.model_type)
for batch_idx in range(len(decoded_text)):
with open(self.temp_valid_file_preds, 'a', encoding='utf-8') as f:
f.write(decoded_text[batch_idx])
f.write('\n')
with open(self.temp_valid_file_original, 'a', encoding='utf-8') as f:
f.write(original[batch_idx])
f.write('\n')
return epoch_loss / iter_size
def train(self, loaded_epoch=0, early_stopping_counter=0):
self.early_stopping.counter = early_stopping_counter
for epoch in range(loaded_epoch, self.epochs):
start_time = time.time()
train_loss = self.train_iteration(epoch)
valid_loss = self.evaluate(epoch, final=False)
end_time = time.time()
epoch_mins, epoch_secs = self.epoch_time(start_time, end_time)
self.train_losses.append(train_loss)
self.valid_losses.append(valid_loss)
self.plot_learning_curve()
print(f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s')
print(f'\tTrain Loss: {train_loss:.3f}')
print(f'\tVal. Loss: {valid_loss:.3f}')
# Saving checkpoint
early_stop = self._save_checkpoint_and_early_stopping(
epoch, valid_loss, early_stop=self.early_stop)
if early_stop:
break
if self.pruning and self.gradually_increase_pruning:
counter = epoch + 1
self.model.model.add_neurons_to_prune(counter)
# Updating stats dictionary
self.stats[f'pretrain_epoch_mins_{epoch}'] = epoch_mins
self.stats[f'pretrain_epoch_secs_{epoch}'] = epoch_secs
self.stats[f'pretrain_train_loss_{epoch}'] = train_loss
self.stats[f'pretrain_valid_loss_{epoch}'] = valid_loss
# Saving stats dictionary
self._save_stats_dict()
# Only calculating BERTScore once at the end, and if running on GPU,
# since it takes longer
self.calculate_evaluation_metrics(
epoch, bert_score=self.run_bert_score)
# Saving stats dictionary one last time
self._save_stats_dict()
def _save_stats_dict(self):
with open(os.path.join(self.exp_output_dir, 'stats.json'), 'w',
encoding='utf-8') as f:
json.dump(self.stats, f, ensure_ascii=False, indent=4)
def _save_checkpoint_and_early_stopping(self, epoch, valid_loss, early_stop=True):
if self.save_unique_checkpoint_every_epoch:
checkpoint_name = os.path.join(self.checkpoint_dir, f'checkpoint_{epoch}.pt')
else:
checkpoint_name = os.path.join(self.checkpoint_dir, f'checkpoint.pt')
if self.two_optimizers:
checkpoint_dict = {
'checkpoint_epoch': epoch,
'checkpoint_early_stopping': self.early_stopping.counter,
'model_state_dict': self.model.state_dict(),
'enc_optimizer_state_dict': self.enc_optimizer.state_dict(),
'dec_optimizer_state_dict': self.dec_optimizer.state_dict()
}
else:
checkpoint_dict = {
'checkpoint_epoch': epoch,
'checkpoint_early_stopping': self.early_stopping.counter,
'model_state_dict': self.model.state_dict(),
'optimizer_state_dict': self.optimizer.state_dict(),
}
# Early stopping
if early_stop:
self.early_stopping(valid_loss, checkpoint_dict, checkpoint_name)
return self.early_stopping.early_stop
else:
torch.save(checkpoint_dict, checkpoint_name)
return False
def _set_up_evaluation_files(self):
self.temp_valid_file_original = os.path.join(
self.exp_output_dir, 'temp_valid_original.txt')
self.temp_valid_file_preds = os.path.join(
self.exp_output_dir, 'temp_valid_preds.txt')
with open(self.temp_valid_file_original, 'w', encoding='utf-8') as f:
f.write('valid original\n')
with open(self.temp_valid_file_preds, 'w', encoding='utf-8') as f:
f.write('valid preds\n')
def calculate_evaluation_metrics(self, epoch, bert_score=False):
print("Calculating evaluation metrics...")
print("Performing final evaluation...")
valid_loss = self.evaluate(epoch, final=True)
valid_hyps, valid_refs = self._get_refs_and_hyps(
self.temp_valid_file_preds, self.temp_valid_file_original)
print("Calculating BLEU scores...")
valid_bleu = self._calculate_bleu_score(valid_hyps, valid_refs)
print("BLEU Valid:", valid_bleu)
self.stats[f'bleu_ep{epoch}_valid'] = valid_bleu.score
if bert_score:
print("\nCalculating BERTScore...")
valid_bert_res = self._calculate_bert_score(valid_hyps, valid_refs)
print(f"BERTScore Valid (P): {valid_bert_res[0]:.2f}")
print(f"BERTScore Valid (R): {valid_bert_res[1]:.2f}")
print(f"BERTScore Valid (F1): {valid_bert_res[2]:.2f}")
self.stats[f'bertscore_P_ep{epoch}_valid'] = valid_bert_res[0]
self.stats[f'bertscore_R_ep{epoch}_valid'] = valid_bert_res[1]
self.stats[f'bertscore_F1_ep{epoch}_valid'] = valid_bert_res[2]
def _get_refs_and_hyps(self, preds_file, original_file):
with open(preds_file, 'r', encoding='utf-8') as f:
hyps = [x.strip() for x in f]
hyps = hyps[1:]
with open(original_file, 'r', encoding='utf-8') as f:
refs = [x.strip() for x in f]
refs = [refs[1:]]
return hyps, refs
def _calculate_bleu_score(self, hyps, refs):
return self.bleu.corpus_score(hyps, refs)
def _calculate_bert_score(self, hyps, refs):
P, R, F1 = self.bert_scorer.score(hyps, refs)
P = P.mean().item()
R = R.mean().item()
F1 = F1.mean().item()
return (P, R, F1)
def plot_learning_curve(self):
'''
Result png figures are saved in the log directory.
'''
fig, ax = plt.subplots(num=1, clear=True)
fig.suptitle('Model Learning Curve')
epochs = list(range(len(self.train_losses)))
ax.plot(epochs, self.train_losses, 'o-', markersize=2, color='b',
label='Train')
ax.plot(epochs, self.valid_losses, 'o-', markersize=2, color='c',
label='Validation')
ax.set(xlabel='Epoch', ylabel='Pretrain Loss')
ax.legend()
plt.savefig(os.path.join(self.exp_output_dir, 'learning_curve.png'))
def run_experiment(self):
# Load an existing model checkpoint, if available
loaded_epoch, early_stopping_counter = self._load_checkpoint()
if self.save_initial_model and loaded_epoch == 0:
# For convenient comparison of non-pretrained models
print("Saving initial checkpoint of model...")
self._save_checkpoint_and_early_stopping(-1, np.inf,
early_stop=False)
# Setting up files for later evaluation of outputs
self._set_up_evaluation_files()
self.train(loaded_epoch=loaded_epoch,
early_stopping_counter=early_stopping_counter)
class RewriteExperiment(Experiment):
def __init__(self, ss: Settings):
super().__init__(ss)
self.rewritten_dataset_dir = None
# Whether to include original dataset in the rewritten dataframe
self.include_original = ss.args.include_original
self.dataset = DPRewriteDataset(
self.dataset_name, self.asset_dir, self.checkpoint_dir,
self.max_seq_len, self.batch_size, mode=self.mode,
embed_type=self.embed_type, embed_size=self.embed_size,
embed_dir_processed=self.embed_dir_processed,
embed_dir_unprocessed=self.embed_dir_unprocessed,
transformer_type=self.transformer_type,
vocab_size=self.vocab_size,
model_type=self.model_type, private=self.private,
prepend_labels=self.prepend_labels,
length_threshold=self.length_threshold,
data_split_cutoff=self.data_split_cutoff,
custom_preprocessor=self.custom_preprocessor, local=self.local,
last_checkpoint_path=self.last_checkpoint_path,
custom_train_path=self.custom_train_path,
custom_valid_path=self.custom_valid_path,
custom_test_path=self.custom_test_path,
downstream_test_data=self.downstream_test_data
)
self.dataset.load_and_process()
print('Initializing model...')
self._init_model()
def _init_model_config(self):
# Setting the padding index
if self.model_type == 'rnn':
pad_idx = self.dataset.preprocessor.word2idx[
self.dataset.preprocessor.PAD]
else: # 'transformer'
pad_idx = self.dataset.preprocessor.tokenizer.pad_token_id
self.pad_idx = pad_idx
# Preparing the general model configuration
general_config_dict = {
'max_seq_len': self.max_seq_len, 'batch_size': self.batch_size,
'mode': self.mode, 'local': self.local, 'device': self.device,
'hidden_size': self.hidden_size,
'enc_out_size': self.enc_out_size,
'embed_size': self.embed_size, 'pad_idx': pad_idx,
'transformer_type': self.transformer_type,
'private': self.private, 'epsilon': self.epsilon,
'delta': self.delta, 'norm_ord': self.norm_ord,
'clipping_constant': self.clipping_constant,
'dp_mechanism': self.dp_mechanism,
'pruning': self.pruning,
'pruning_index_path': self.pruning_index_path,
'experiment_output_dir': self.exp_output_dir}
# Preparing the specific model configuration class
model_config = self._get_specific_model_config(general_config_dict)
return model_config
def _get_specific_model_config(self, general_config_dict):
if self.model == 'adept':
specific_config_dict = {
'pretrained_embeddings': self.dataset.preprocessor.embeds,
'vocab_size': self.dataset.preprocessor.vocab_size,
'no_clipping': self.no_clipping
}
specific_config = ADePTModelConfig(
**general_config_dict, **specific_config_dict
)
elif self.model == 'dp_bart':
specific_config_dict = {
'dp_module': self.dp_module,
'no_clipping': self.no_clipping
}
specific_config = DPBartModelConfig(
**general_config_dict, **specific_config_dict
)
elif self.model in ['custom_rnn', 'custom_transformer']:
specific_config_dict = {
'custom_config_list': self.custom_model_arguments}
specific_config = CustomModelConfig(
**general_config_dict, **specific_config_dict
)
else:
raise NotImplementedError
return specific_config
def _get_model_type(self):
if self.model == 'adept':
model_type = ADePT
elif self.model == 'dp_bart':
model_type = DPBart
elif self.model == 'custom_rnn':
model_type = CustomModel_RNN
elif self.model == 'custom_transformer':
model_type = CustomModel_Transformer
else:
raise NotImplementedError
return model_type
def _init_model(self):
model_config = self._init_model_config()
model_type = self._get_model_type()
model = model_type(model_config)
self.model = model.to(self.device)
num_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"Num parameters in model: {num_params,}")
mem_params = sum([param.nelement()*param.element_size() for param in model.parameters()])
mem_bufs = sum([buf.nelement()*buf.element_size() for buf in model.buffers()])
mem = mem_params + mem_bufs # in bytes
print("Estimated non-peak memory usage of model (MBs):", mem / 1000000)
self.loss = nn.CrossEntropyLoss(ignore_index=self.pad_idx)
def _load_checkpoint(self):
checkpoint = torch.load(self.last_checkpoint_path,
map_location=self.device)
self.model.load_state_dict(checkpoint['model_state_dict'])
self.model.eval()
print(f"Loaded pretrained model from {self.last_checkpoint_path}.")
def evaluate(self, splits):
with torch.no_grad():
for split_name, iterator in splits.items():
rewritten_df = pd.DataFrame(
columns=["label", "text", "original_label", "original_text"],
index=range(len(iterator.dataset)))
epoch_loss = 0
for idx, batch in tqdm(enumerate(iterator)):
if self.model_type == 'rnn':
encoder_input_ids = batch[0]
lengths = batch[1]
true_labels = batch[2]
encoder_input_ids = encoder_input_ids.to(self.device)
inputs = {'input_ids': encoder_input_ids, 'lengths': lengths,
'teacher_forcing_ratio': 0.0}
tgt = deepcopy(encoder_input_ids)[:, 1:].reshape(-1)
else:
encoder_input_ids = batch['input_ids'].to(self.device)
attention_mask = batch['attention_mask'].to(self.device)
true_labels = batch['labels'].to(self.device)
inputs = {'input_ids': encoder_input_ids,
'attention_mask': attention_mask}
tgt = deepcopy(encoder_input_ids)[:, 1:].reshape(-1)
loss = 0
outputs = self.model(**inputs)
if self.model_type == 'rnn':
loss = self.loss(outputs, tgt)
loss = loss.item()
outputs_reshaped = outputs.view(
encoder_input_ids.shape[0],
encoder_input_ids.shape[1] - 1, outputs.shape[-1])
preds = torch.max(outputs_reshaped, dim=2).indices
else:
preds = outputs
if self.prepend_labels:
predicted_labels = preds[:, 0]
preds = preds[:, 1:]
encoder_input_ids = encoder_input_ids[:, 2:]
output_labels = decode_rewritten(
predicted_labels.unsqueeze(0),
self.dataset.preprocessor,
remove_special_tokens=False,
labels=True, model_type=self.model_type)[0]
true_labels = decode_rewritten(
true_labels.unsqueeze(0),
self.dataset.preprocessor,
remove_special_tokens=False,
labels=True, model_type=self.model_type)[0]
else:
if self.model_type == 'rnn':
output_labels = true_labels
else:
true_labels = [
self.dataset.preprocessor.lab_int2str[lab.item()]
for lab in true_labels
]
output_labels = true_labels
decoded_text = decode_rewritten(
preds, self.dataset.preprocessor,
remove_special_tokens=True,
model_type=self.model_type)
original = decode_rewritten(
encoder_input_ids, self.dataset.preprocessor,
remove_special_tokens=True,
model_type=self.model_type)
for batch_idx in range(len(decoded_text)):
current_data_idx = (idx * self.batch_size) + batch_idx
if decoded_text[batch_idx] == '':
decoded_text[batch_idx] = ' '
current_data_point = decoded_text[batch_idx]
current_data_original = original[batch_idx]
current_data_label = output_labels[batch_idx]
current_data_original_label = true_labels[batch_idx]
rewritten_df["text"].loc[current_data_idx] =\
current_data_point
rewritten_df["original_text"].loc[current_data_idx] =\
current_data_original
rewritten_df["label"].loc[current_data_idx] = \
current_data_label
rewritten_df["original_label"].loc[current_data_idx] = \
current_data_original_label
epoch_loss += loss
final_loss = epoch_loss / len(iterator)
print(f"{split_name} set: | Rewrite loss: {final_loss} |")
if not self.include_original:
rewritten_df_out = rewritten_df.drop('original_text', 1)
rewritten_df_out = rewritten_df_out.drop('original_label', 1)
else:
rewritten_df_out = rewritten_df
rewritten_df_out.to_csv(
os.path.join(
self.rewritten_dataset_dir,
f"rewritten_{split_name}.csv"),
header=False,
index=False)
self.calculate_evaluation_metrics(
rewritten_df, split_name, bert_score=self.run_bert_score)
def train(self):
pass
def train_iteration(self):
pass
def plot_learning_curve(self):
pass
def calculate_evaluation_metrics(self, rewritten_df, split_name,
bert_score=False):
print("Calculating evaluation metrics...")
hyps, refs = self._get_refs_and_hyps(rewritten_df)
print("Calculating BLEU scores...")
bleu_score = self._calculate_bleu_score(hyps, refs)
print(f"BLEU {split_name}:", bleu_score)
self.stats[f'bleu_{split_name}'] = bleu_score.score
if bert_score:
print("\nCalculating BERTScore...")
bert_res = self._calculate_bert_score(hyps, refs)
print(f"BERTScore {split_name} (P): {bert_res[0]:.2f}")
print(f"BERTScore {split_name} (R): {bert_res[1]:.2f}")
print(f"BERTScore {split_name} (F1): {bert_res[2]:.2f}")
self.stats[f'bertscore_P_{split_name}'] = bert_res[0]
self.stats[f'bertscore_R_{split_name}'] = bert_res[1]
self.stats[f'bertscore_F1_{split_name}'] = bert_res[2]
def _get_refs_and_hyps(self, rewritten_df):
hyps = rewritten_df['text'].tolist()
refs = [rewritten_df['original_text'].tolist()]
return hyps, refs
def _calculate_bleu_score(self, hyps, refs):
return self.bleu.corpus_score(hyps, refs)
def _calculate_bert_score(self, hyps, refs):
P, R, F1 = self.bert_scorer.score(hyps, refs)
P = P.mean().item()
R = R.mean().item()
F1 = F1.mean().item()
return (P, R, F1)
def run_experiment(self):
# Preparing dataset directory
self.rewritten_dataset_dir = os.path.join(
self.exp_dump_dir, self.dataset_name + "_rewritten")
if not os.path.exists(self.rewritten_dataset_dir):
os.makedirs(self.rewritten_dataset_dir)