-
Notifications
You must be signed in to change notification settings - Fork 2
/
email_supervised.py
1281 lines (1063 loc) · 62 KB
/
email_supervised.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
'''
MIT License
Copyright (c) 2021 Avemac Systems LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
########################################################################################
# This source contains model preparation, training, evaluation and inference routines
# to model supervised datasets for email classification tasks.
#
# -- This is the supporting code for my email insights and analysis blog series part 4
# available on my website at https://www.avemacconsulting.com. Part 4 of the series at
# https://www.avemacconsulting.com/2021/10/12/email-insights-from-data-science-part-4/
#
# The code has been tested for text tokenization to single label classification.
# Multi-label and multi-feature support is built-in, but specific models will need to be
# developed to take advantage of the framework.
#
# Currently implements classification using recurrent networks (LSTM/GRU/RNN),
# Transformer (encoder layer only), and prebuilt Roberta fine-tuning.
#
# K-Fold cross validation is implemented...wouldn't take much to abstract the routines
# to accept other CV methods. CV can be turned off for a "blended" overfitting techique.
#
# Ensemble evaluation logic has been implemented to support each model trained...plus
# a final inference prediction output from an aggregated ensemble of all models.
#
# -- The dataset used for this exercise was specifically created from the raw Enron
# email repository located at https://www.cs.cmu.edu/~enron/ with 3 labels generated
# for sentiment (positive/negative/neutral/unknown) and alignment(business/personal).
#
# The code for formatting the raw email content, performing basic analysis and creating
# the supervised dataset can be found in this Github repo with details referenced on my website.
#
# Part 1. https://www.avemacconsulting.com/2021/08/24/email-insights-from-data-science-techniques-part-1/
# Part 2. https://www.avemacconsulting.com/2021/08/27/email-insights-from-data-science-part-2/
# Part 3. https://www.avemacconsulting.com/2021/09/23/email-insights-from-data-science-part-3/
#
# ---- Classes ----
# class ContentDataset(Dataset) - Custom Pytorch "Dataset" implementation for tokenized email content.
# class Vocabulary() - Class for saving / retrieving / generating custom vocabulary from email content.
# class RawDataLoader() - Class methods for retrieving and formatting raw dataset into Pandas dataframe.
# class ModelSupport() - Weight/Bias initialization and graphing routines.
# class SupervisedRNN(nn.Module) - Pytorch recurrent model implementation (LSTM/GRU/RNN)
# class SupervisedTransformer(nn.Module) - Pytorch TransformerEncoder model.
# class PositionalEncoding(nn.Module) - SupervisedTransformer supporting function for positional embeddings.
# class SupervisedPrebuilt(nn.Module) - HuggingFace Robert-Base prebuilt transformer model implementation.
# class ModelManagement() - Common training/eval and state management routines for model creation.
# class PipelineConfig() - Common configuration class for Training and Inference pipeline logic.
# class TrainingPipeline() - Training/Eval pipeline logic.
# class InferencePipeline() - Inference pipeline logic.
#
# ---- Main ----
# Train/Eval Processing - Label and model selection point and main train/eval processing loop.
# Inference Testing - Label and model selection point and main inference processing loop.
#
########################################################################################
#!/usr/bin/python3 -W ignore::DeprecationWarning
import os
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
import re
import math
import glob
import pickle
import gc
from time import time
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, SubsetRandomSampler, Dataset
from sklearn.model_selection import KFold
from transformers import RobertaConfig, RobertaTokenizerFast, RobertaModel
from nltk.corpus import stopwords
pd.set_option('display.max_rows', 100)
pd.set_option('display.min_rows', 20)
pd.set_option('display.max_colwidth', 100)
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
PAD_KEY = '<pad>'
NBR_KEY = '<nbr>'
KAGGLE_BASEDIR = '/kaggle'
LOCAL_BASEDIR = '/proto/learning/avemac/email_analysis_blog'
LOCAL_PRETRAIN_BASEDIR = '/proto/models/hf'
IS_LOCAL = not os.path.exists(KAGGLE_BASEDIR) # running in Kaggle Environment or not
##############################################################################################################################
# Custom Content Dataset
##############################################################################################################################
class ContentDataset(Dataset):
'''
Custom Pytorch "Dataset" implementation for tokenized email content.
Implement the PyTorch dataset functions - example at https://pytorch.org/docs/stable/data.html#data-loading-order-and-sampler
- Requires label column be renamed to "label" if not already.
- Requires text column be renamed to "content" if not already.
'''
def __init__(self, df:pd.DataFrame, config:dict, vocab:dict) -> None:
super(ContentDataset).__init__()
self.config = config
print(f'\n--- Building ContentDataset for embedding_type "{config["embedding_type"]}"')
# tokenization is different if using trained versus pretrained models
if config['embedding_type'] == 'train':
df['text'] = self._custom_text_encoder(df['content'].values, vocab, max_tokens=config['max_tokens'])
columns = list(df.columns); columns.remove('content')
elif config['embedding_type'] == 'hgf_pretrained':
tokenizer = RobertaTokenizerFast.from_pretrained(f'{config["pretrained_dir"]}{config["pretrained_model"]}')
t = tokenizer(df['content'].to_list(), add_special_tokens=True, return_attention_mask=True, padding='max_length', truncation=True, max_length=config['max_tokens'], return_tensors='np')
df['input_ids'] = t['input_ids'].tolist()
df['attention_mask'] = t['attention_mask'].tolist()
columns = ['input_ids','attention_mask']
if 'label' in df.columns: columns.append('label')
else:
raise AssertionError('Invalid tokenization mode')
self.tds = {x : torch.tensor(df[x].to_list()).view(len(df),-1) for x in columns}
self.length = len(df)
return
def __iter__(self) -> iter:
self.pos = 0
return self
def __next__(self) -> dict:
if self.pos < self.__len__():
slice = {key:tensor[self.pos] for key, tensor in self.tds.items()}
self.pos += 1
return slice
else:
raise StopIteration
def __len__(self) -> int:
return self.length
def __getitem__(self, index) -> tuple():
return {key:tensor[index] for key, tensor in self.tds.items()}
def _custom_text_encoder(self, corpus, vocab, max_tokens=500):
''' Encode raw text content into dense token vectors for future embedding layer input. '''
encoded = []
for text in tqdm(corpus):
encode = []
tokens = re.findall(r"[a-zA-Z][a-z'][a-z']+", text)
for t in tokens:
t = t.lower()
if t in vocab: # only work with words in vocab
encode.append(vocab[t]) # encode token
if len(encode) >= max_tokens: break # stop if beyond max tokens
if len(encode) < max_tokens: # pad manually instead of using nn.utils.rnn.pad_packed_sequence in model
encode.extend([vocab[PAD_KEY] for _ in range(len(encode), max_tokens)])
encoded.append(encode) # add sample row
return encoded
##############################################################################################################################
# Vocabulary Tokenizer
##############################################################################################################################
class Vocabulary():
''' Class for saving / retrieving / generating custom vocabulary from email content. '''
def __init__(self, config) -> None:
self.config = config
self.vocab = None
return
def get_vocabulary(self, corpus=None, force_build=False) -> dict:
''' Retrieve the vocabulary if exists or generate new if forced or not found. '''
if self.vocab == None:
vocab_fn = f'{self.config["checkpoint_dir"]}{self.config["vocabulary_fn"]}'
if os.path.exists(vocab_fn) and not force_build:
with open(vocab_fn, 'rb') as f:
self.vocab = pickle.load(f)
elif corpus is not None:
self.vocab = self._create_vocabulary(corpus)
with open(vocab_fn, 'wb') as f:
pickle.dump(self.vocab, f)
return self.vocab
def _create_vocabulary(self, corpus) -> dict:
''' Iterate through the data samples and create the token vocabulary '''
stop_words = [w.lower() for w in stopwords.words()]
vocab = {PAD_KEY:0, NBR_KEY:1}; vocab_idx = 2
for text in tqdm(corpus):
tokens = re.findall(r"[a-zA-Z][a-z'][a-z']+", text)
for t in tokens:
t = t.lower()
if len(t) > 20: continue # skip long "words", most likely not a real word
if t in stop_words: continue # skip stopwords
if t not in vocab: # update vocab if token missing
vocab[t] = vocab_idx
vocab_idx += 1
return vocab
##############################################################################################################################
# Training and Eval Dataset Loader
##############################################################################################################################
class RawDataLoader():
''' Class methods for retrieving and formatting raw dataset into Pandas dataframe. '''
def __init__(self, config) -> None:
self.data_dir = config['data_dir']
self.columns = config['input_columns'] # dictionary {<actual>:<renamed>}
self.config = config
self.class_encoders = {
# can also use sklearn.preprocessing.LabelEncoder or PyTorch LabelEncoder or others...
# regardless of method, for non-labels the same encoding scheme must be used during model inference
'Outside_Hours':lambda x: 0 if x is False else 1,
'Forwarded':lambda x: 0 if x is False else 1,
'Source':lambda x: 0 if x == 'deleted' else 1 if x == 'responded' else 2 if x == 'sent' else 3,
'Class_Alignment_1':lambda x: 0 if x == 'work' else 1 if x == 'fun' else 2,
'Class_Alignment_2':lambda x: 0 if x == 'work' else 1 if x == 'fun' else 2,
'Class_Alignment_3':lambda x: 0 if x == 'work' else 1 if x == 'fun' else 2,
'Class_Sentiment_1':lambda x: 0 if x == 'neg' else 1 if x == 'pos' else 2,
'Class_Sentiment_2':lambda x: 0 if x == 'neg' else 1 if x == 'pos' else 2,
'Class_Sentiment_Vader':lambda x: 0 if x == 'neg' else 1 if x == 'pos' else 2,
}
self.df = self._fetch_raw_data(self.data_dir + config['supervised_dataset_fn'], self.columns)
self.vocab = Vocabulary(self.config).get_vocabulary(corpus=self.df['content'].values, force_build=True)
return
def _fetch_raw_data(self, fn:str, columns:dict) -> pd.DataFrame:
''' Routine to fetch the labeled training data, normalize some column names and encode classes'''
limit = self.config['limit']
keys = columns.keys()
# retrieve input dataframe from unsupervised pipeline process - note: sample will auto shuffle so set state for test consistency
raw_data = (pd.DataFrame)(pd.read_csv(fn)[keys]).sample(frac=limit, random_state=42)
# encode class if included in the columns list
for column in keys:
if column in self.class_encoders.keys():
raw_data[column] = raw_data[column].apply(self.class_encoders[column])
# rename raw feature names to standard names
raw_data.rename(columns=columns, inplace=True)
return raw_data
def split_dataset(self, df=None, split=0.1) -> tuple():
''' Break the dataset up based upon the split requested. '''
df = self.df if df is None else df
s2 = df.sample(frac=split, random_state=42)
s1 = df.drop(s2.index)
return (s1, s2) # return train/test
##############################################################################################################################
# Common Model Support Routines
##############################################################################################################################
class ModelSupport():
'''
Common model weight initialization and graphing support routines
Note: uses recursion.
Known issues: init_weights Will not properly process nested container modules of the same type (i.e. a ModuleList directly inside a ModuleList)
'''
def __init__(self, config):
self.config = config
return
def init_weights(self, module, mode='relu'):
''' Recurse through container modules and initialze weights/biases by module type. '''
if isinstance(module, (nn.ModuleList, nn.ModuleDict, nn.Sequential, nn.Transformer, nn.TransformerEncoder, nn.TransformerDecoder, nn.TransformerEncoderLayer, nn.TransformerDecoderLayer)):
for m in module.modules():
_ = self.init_weights(m) if m != module else None
elif isinstance(module, (nn.LayerNorm, nn.BatchNorm1d, nn.BatchNorm2d)):
nn.init.ones_(module.weight)
_ = nn.init.zeros_(module.bias) if module.bias is not None else None
elif isinstance(module, (nn.Linear, nn.Embedding, nn.LSTM, nn.GRU, nn.MultiheadAttention, nn.Conv1d, nn.Conv2d, nn.Conv3d)):
for name, param in module.named_parameters():
if 'bias' in name:
nn.init.zeros_(param)
elif 'weight' in name:
_ = nn.init.xavier_normal_(param) if mode=='tanh' else nn.init.kaiming_normal_(param)
return
def graph_outputs(self, fold, epoch, step, outputs:dict, mode=['hist'], layers=[]):
''' Debugging routine to visualze model layer outputs and detect anomalies. '''
layers = layers if len(layers) > 0 else [k for k in outputs.keys()]
subplot_rows = round(math.sqrt(len(layers)))
subplot_cols = math.ceil(math.sqrt(len(layers)))
# histogram
if 'hist' in mode:
idx = 0
fig, axes1 = plt.subplots(subplot_rows, subplot_cols, figsize=(subplot_cols*5,subplot_rows*2))
fig.suptitle('Output Tensor Distributions', y=0.99)
fig.supylabel('Frequency')
fig.subplots_adjust(top=0.90, bottom=0.1, wspace=0.35, hspace=0.80)
axes1 = axes1.flatten()
for name, output in outputs.items():
if name in layers:
d = output.detach().cpu().numpy().flatten()
ax = axes1[idx]
ax.set_title(f'{name} - {d.size}', {'fontsize':8})
ax.tick_params(axis='both', which='major')
ax.hist(d, bins=100)
idx += 1
fig.savefig(f'{self.config["graph_dir"]}Outputs_{self.config["model_id"]}_F{fold}E{epoch}S{step}.png', facecolor=fig.get_facecolor())
plt.close(fig)
return
def graph_parameters(self, model, fold, epoch, step, mode=['hist'], types=['weight','bias'], module_types=(), spot_check=True):
''' Routine to plot layer weights and verify acceptable neural processing.
Note -> using this routine for realtime analysis via debugger, could also save
checkpoints every iteration and feed into TensorBoard.
Should also create an abstract class for this at some point...
'''
if 'grad' in types and len(types) > 1:
raise AssertionError('Cannot mix gradient and weights/bias on same graph')
module_types = module_types if len(module_types) > 0 else (nn.Linear, nn.Embedding, nn.Conv1d, nn.Conv2d, nn.Conv3d, nn.LayerNorm, nn.BatchNorm1d, nn.BatchNorm2d, nn.LSTM, nn.GRU, nn.TransformerEncoder, nn.TransformerDecoder)
m_list = []
m_count = 0
# create list of modules to display, plus count the number of weight vectors for subplot matrix
for m in model.modules():
if isinstance(m, module_types):
m_list.append(m)
for name, param in m.named_parameters():
if 'weight' in name and 'weight' in types:
m_count += 4 if isinstance(m, nn.LSTM) else 3 if isinstance(m, nn.GRU) else 1
if 'bias' in name and 'bias' in types:
m_count += 1
if 'grad' in types and param.grad is not None:
m_count += 1
# nothing to graph
if m_count <= 0:
return
subplot_rows = round(math.sqrt(m_count))
subplot_cols = math.ceil(math.sqrt(m_count))
# histogram
if 'hist' in mode:
idx = 0
fig, axes1 = plt.subplots(subplot_rows, subplot_cols, figsize=(subplot_cols*5,subplot_rows*2))
fig.suptitle('Parameter Distributions' if 'grad' not in types else 'Gradient Distribution', y=0.99)
fig.supylabel('Frequency')
fig.subplots_adjust(top=0.90, bottom=0.1, wspace=0.35, hspace=0.80)
axes1 = axes1.flatten()
for m in m_list:
for name, param in m.named_parameters():
if 'weight' in name and 'weight' in types:
if isinstance(m, nn.LSTM):
w_i, w_f, w_c, w_o = param.chunk(4, 0)
d = {
'w_i':w_i.detach().cpu().numpy().flatten(),
'w_f':w_f.detach().cpu().numpy().flatten(),
'w_c':w_c.detach().cpu().numpy().flatten(),
'w_o':w_o.detach().cpu().numpy().flatten(),
}
elif isinstance(m, nn.GRU):
w_r, w_i, w_n = param.chunk(3, 0)
d = {
'w_i':w_i.detach().cpu().numpy().flatten(),
'w_r':w_r.detach().cpu().numpy().flatten(),
'w_n':w_n.detach().cpu().numpy().flatten(),
}
else:
d = {
'w_h': param.data.detach().cpu().numpy().flatten()
}
for k,v in d.items():
ax = axes1[idx]
ax.set_title(f'{m._get_name()} - {name}/{k} - {v.size}', {'fontsize':8})
ax.tick_params(axis='both', which='major')
ax.hist(v, bins=100)
idx += 1
if 'bias' in name and 'bias' in types:
d = param.data.detach().cpu().numpy().flatten()
ax = axes1[idx]
ax.set_title(f'{m._get_name()} - {name} - {d.size}', {'fontsize':8})
ax.tick_params(axis='both', which='major')
ax.hist(d, bins=100)
idx += 1
if 'grad' in types and param.grad is not None:
d = param.grad.detach().cpu().numpy().flatten()
ax = axes1[idx]
ax.set_title(f'{m._get_name()} - {name}/grad - {d.size}', {'fontsize':8})
ax.tick_params(axis='both', which='major')
ax.hist(d, bins=100)
idx += 1
fig.savefig(f'{self.config["graph_dir"]}{"Gradients" if "grad" in types else "Parameters"}_{self.config["model_id"]}_F{fold}E{epoch}S{step}_{"Check" if spot_check else "Debug"}.png', facecolor=fig.get_facecolor())
plt.close(fig)
return
##############################################################################################################################
# Supervised Recurrent Model
##############################################################################################################################
class SupervisedRNN(nn.Module):
''' Recurrent network for time-series model of email content '''
def __init__(self, mode:str, config:dict):
super().__init__()
self.batch_size = config['batch_size'] # batch length
self.max_tokens = config['max_tokens'] # sequence length
self.embedding_len = config['embedding_len'] # feature length
self.number_classes = config['number_classes'] # number target classes
self.dropout = config['dropout']
self.epochs = config['epochs']
self.vocab_size = config['vocab_size']
self.bidirectional = config['bidirectional']
self.rnn_layers = config['rnn_layers']
self.config = config
# define network
# Embedding layer - training custom token relationships rather than pretrained
# Note: should train this separately
self.embedding = nn.ModuleList([
nn.Embedding(self.vocab_size, self.embedding_len, scale_grad_by_freq=True),
])
# Recurrent network (Input = BatchSize x MaxTokens x Embedding Length)
ln_input_len = (2 if self.bidirectional else 1) * self.rnn_layers * self.embedding_len
self.rnn = nn.ModuleList([
nn.LSTM(self.embedding_len, self.embedding_len, batch_first=True, num_layers=self.rnn_layers, bidirectional=self.bidirectional) if mode=='lstm'
else nn.GRU(self.embedding_len, self.embedding_len, batch_first=True, num_layers=self.rnn_layers, bidirectional=self.bidirectional) if mode=='gru'
else nn.RNN(self.embedding_len, self.embedding_len, batch_first=True, num_layers=self.rnn_layers, bidirectional=self.bidirectional),
nn.LayerNorm(ln_input_len), # input size is doubled if birectional LSTM
nn.LeakyReLU(),
nn.Dropout(self.dropout),
])
# Fully connected network to reduce recurrent weights to log odds
fc_input_len = (2 if self.bidirectional else 1) * self.rnn_layers * self.embedding_len
self.fc = nn.ModuleList([
nn.Linear(fc_input_len, fc_input_len * 2),
nn.LeakyReLU(),
nn.Dropout(0.3),
nn.Linear(fc_input_len * 2, fc_input_len),
nn.LeakyReLU(),
nn.Dropout(0.3),
nn.Linear(fc_input_len, self.number_classes),
nn.LogSoftmax(),
])
self.reset_weights(mode='init')
self.to(DEVICE) # move to GPU if available
return
def forward(self, text, **kwargs):
''' Forward pass for recurrent network '''
output_checks = {}
output_pos = 0
x = text
# process the embedding layer
for m in self.embedding:
x = m(x)
if self.config['check_outputs']:
output_checks[f'{m.__class__.__name__}-Layer{output_pos}'] = x; output_pos += 1
# spin through the modules defined within the recurrent network portion of the model
for m in self.rnn:
if isinstance(m, (nn.LSTM)):
_, (x,_) = m(x) # use last hidden state since we are labeling the entire sequence
x = torch.transpose(x, 0, 1) # back to batch first
x = x.reshape(-1, np.prod(x.shape[-2:])) if x.ndim == 3 else x # collapse the sequence dimension if bidirectional
elif isinstance(m, (nn.GRU)):
_, x = m(x) # use last hidden state since we are labeling the entire sequence
x = torch.transpose(x, 0, 1) # back to batch first
x = x.reshape(-1, np.prod(x.shape[-2:])) if x.ndim == 3 else x # collapse the sequence dimension if bidirectional
else:
x = m(x)
if self.config['check_outputs']:
output_checks[f'{m.__class__.__name__}-Layer{output_pos}'] = x; output_pos += 1
# finish up the pass regressing the recurrent summarized sequence weights into class log odds
for m in self.fc:
x = m(x)
if self.config['check_outputs']:
output_checks[f'{m.__class__.__name__}-Layer{output_pos}'] = x; output_pos += 1
return x, output_checks
def reset_weights(self, mode='fold'):
''' Method for initial weights and resetting weights between cross-validation folds '''
minit = ModelSupport(self.config)
_ = minit.init_weights(self.embedding) if mode == 'init' else None
minit.init_weights(self.rnn)
minit.init_weights(self.fc)
return
##############################################################################################################################
# Supervised Transformer Model
##############################################################################################################################
class SupervisedTransformer(nn.Module):
''' Transformer network for time-series model of email content '''
def __init__(self, config:dict):
super().__init__()
self.batch_size = config['batch_size'] # batch length
self.max_tokens = config['max_tokens'] # sequence length
self.embedding_len = config['embedding_len'] # feature length
self.number_classes = config['number_classes'] # number target classes
self.dropout = config['dropout']
self.epochs = config['epochs']
self.vocab_size = config['vocab_size']
self.attention_heads = config['attention_heads']
self.encoder_layers = config['encoder_layers']
self.config = config
# define network
# Embedding layer - training custom token relationships rather than pretrained
self.embedding = nn.ModuleList([
nn.Embedding(self.vocab_size, self.embedding_len, scale_grad_by_freq=True),
])
# Positional encoding layer
self.pos_encoder = PositionalEncoding(self.embedding_len, dropout=self.dropout, max_len=self.max_tokens, batch_first=True)
# Encoder network (Input = BatchSize x MaxTokens x Embedding Length)
attention_layer = nn.TransformerEncoderLayer(self.embedding_len, self.attention_heads, dim_feedforward=self.max_tokens*4, dropout=self.dropout, batch_first=True)
self.encoder = nn.ModuleList([
nn.TransformerEncoder(attention_layer, self.encoder_layers),
nn.LayerNorm(self.embedding_len),
nn.LeakyReLU(),
nn.Dropout(0.3),
])
# Fully connected network to reduce encoder weights to log odds
fc_input_len = self.max_tokens * self.embedding_len # 2d size after flatten of 3d transformer output
self.fc = nn.ModuleList([
nn.Linear(fc_input_len, fc_input_len // 4),
nn.LeakyReLU(),
nn.Dropout(0.3),
nn.Linear(fc_input_len // 4, fc_input_len // 2),
nn.LeakyReLU(),
nn.Dropout(0.3),
nn.Linear(fc_input_len // 2, self.number_classes),
nn.LogSoftmax(),
])
self.reset_weights(mode='init')
self.to(DEVICE) # move to GPU if available
return
def forward(self, text, **kwargs):
''' Forward pass for transformer network '''
output_checks = {}
output_pos = 0
x = text
# process the embedding layer
for m in self.embedding:
x = m(x)
if self.config['check_outputs']:
output_checks[f'{m.__class__.__name__}-Layer{output_pos}'] = x; output_pos += 1
# encode with position information
x = self.pos_encoder(x)
# spin through the modules defined within the recurrent network portion of the model
for m in self.encoder:
x = m(x)
if self.config['check_outputs']:
output_checks[f'{m.__class__.__name__}-Layer{output_pos}'] = x; output_pos += 1
# flatten the encoder output
x = torch.flatten(x, start_dim=1) # squash down to 2d
# finish up the pass regressing the sequence weights into class log odds
for m in self.fc:
x = m(x)
if self.config['check_outputs']:
output_checks[f'{m.__class__.__name__}-Layer{output_pos}'] = x; output_pos += 1
return x, output_checks
def reset_weights(self, mode='fold'):
''' Method for initial weights and resetting weights between cross-validation folds '''
minit = ModelSupport(self.config)
_ = minit.init_weights(self.embedding) if mode == 'init' else None
minit.init_weights(self.encoder)
minit.init_weights(self.fc)
return
class PositionalEncoding(nn.Module):
'''
Positional embedding encoder
Modified from Pytorch tutorial -> https://pytorch.org/tutorials/beginner/transformer_tutorial.html
'''
def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000, batch_first=True):
super().__init__()
self.dropout = nn.Dropout(p=dropout)
self.batch_first = batch_first
position = torch.arange(max_len).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model))
if batch_first:
pe = torch.zeros(1, max_len, d_model)
pe[0, :, 0::2] = torch.sin(position * div_term)
pe[0, :, 1::2] = torch.cos(position * div_term)
else:
pe = torch.zeros(max_len, 1, d_model)
pe[:, 0, 0::2] = torch.sin(position * div_term)
pe[:, 0, 1::2] = torch.cos(position * div_term)
self.register_buffer('pe', pe)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
x -> shape [seq_len, batch_size, embedding_dim] if not batch_first else [batch_size, seq_len, embedding_dim]
"""
dim = 1 if self.batch_first else 0
x = x + self.pe[:x.size(dim)]
return self.dropout(x)
##############################################################################################################################
# Prebuilt Supervised Transformer Model
##############################################################################################################################
class SupervisedPrebuilt(nn.Module):
''' Prebuilt fine-tuning of transformer network for time-series model of email content
Using Roberta prebuilt model from Huggingface, api at https://huggingface.co/transformers/model_doc/roberta.html
'''
def __init__(self, config):
super().__init__()
self.number_classes = config['number_classes'] # number target classes
self.dropout = config['dropout']
self.encoder_layers = config['encoder_layers']
self.config = config
self.mconfig = RobertaConfig.from_pretrained(f'{self.config["pretrained_dir"]}{self.config["pretrained_model"]}')
self.mconfig.update({"is_decoder":False, "num_layers":self.encoder_layers, "output_hidden_states":True, "hidden_dropout_prob": self.dropout, "layer_norm_eps": 1e-7})
# self.model is created in reset_weights - if kfold CV is used then model will need to be reset multiple times
# define network
# Normalization layer
ln_input_len = self.mconfig.to_dict()['hidden_size'] * 4
self.norm = nn.ModuleList([
nn.LayerNorm(ln_input_len),
nn.LeakyReLU(),
nn.Dropout(0.2),
])
# Fully connected network to reduce encoder weights to log odds
fc_input_len = self.mconfig.to_dict()['hidden_size'] * 4
self.fc = nn.ModuleList([
nn.Linear(fc_input_len, fc_input_len),
nn.LeakyReLU(),
nn.Dropout(0.3),
nn.Linear(fc_input_len, fc_input_len // 2),
nn.LeakyReLU(),
nn.Dropout(0.3),
nn.Linear(fc_input_len // 2, self.number_classes),
nn.LogSoftmax(),
])
self.reset_weights() # in this case the prebuilt Roberta model is created in reset_weights
self.to(DEVICE) # move to GPU if available
return
def forward(self, input_ids, attention_mask, **kwargs):
''' Forward pass for prebuilt network. '''
output_checks = {}
output_pos = 0
layers = [-4, -3, -2, -1]
outputs = self.model(input_ids, attention_mask)
x = outputs.hidden_states
amask = attention_mask.unsqueeze(-1).expand(x[layers[0]].size())
x = torch.cat(tuple(torch.sum(x[l]*amask, dim=1) for l in layers), dim=1) # sum each of the last four layers by sequence then concatenate
if self.config['check_outputs']:
output_checks[f'{self.model.__class__.__name__}-Layer{output_pos}'] = x; output_pos += 1
# process normalization layer
for m in self.norm:
x = m(x)
if self.config['check_outputs']:
output_checks[f'{m.__class__.__name__}-Layer{output_pos}'] = x; output_pos += 1
# finish up the pass regressing the sequence weights into class log odds
for m in self.fc:
x = m(x)
if self.config['check_outputs']:
output_checks[f'{m.__class__.__name__}-Layer{output_pos}'] = x; output_pos += 1
return x, output_checks
def reset_weights(self, mode=None):
''' Method for initial weights and resetting weights between cross-validation folds '''
# Prebuilt network is instantiated here so if CV is used a fresh model is reintroduced for each new fold.
self.model = RobertaModel.from_pretrained(f'{self.config["pretrained_dir"]}{self.config["pretrained_model"]}', config=self.mconfig).to(DEVICE)
self.model.train()
minit = ModelSupport(self.config)
minit.init_weights(self.norm)
minit.init_weights(self.fc)
return
##############################################################################################################################
# Common model training and eval functions
##############################################################################################################################
class ModelManagement():
''' Common training/eval and state management routines for model creation. '''
def __init__(self, model:nn.Module, config:dict, training_set:ContentDataset, eval_set:ContentDataset):
self.config = config
self.model = model
self.training_set = training_set
self.eval_set = eval_set
# state variables
self.prev_loss = sys.float_info.max
self.prev_acc = 0.0
# metrics
self.train_loss = []
self.test_loss = []
self.train_acc = []
self.test_acc = []
# graphing functions
self.graphing = ModelSupport(self.config)
def training_plot(self):
''' Basic loss/accuracy performance graph - overwrites previous graph '''
# setup plot framework
fig, axes = plt.subplots(2, 1, figsize=(10,8))
fig.subplots_adjust(top=0.90, bottom=0.1, wspace=0.35, hspace=0.80)
axes = axes.flatten()
# loss subplot
ax = axes[0]
ax.grid(True)
ax.tick_params(axis='both', which='major')
ax.set_xlim([-1, 50])
ax.set_ylim([-0.01, 2.0])
ax.set_title('Train Loss -vs- Test Loss', {'fontsize':12})
ax.set_xlabel('Epochs')
ax.set_ylabel('Loss')
ax.plot(self.train_loss,'-bo')
ax.plot(self.test_loss,'-go')
ax.legend(['Train Loss','Test Loss'])
# accuracy subplot
ax = axes[1]
ax.grid(True)
ax.tick_params(axis='both', which='major')
ax.set_xlim([-1, 50])
ax.set_ylim([0.2, 1.0])
ax.set_title('Train Accuracy -vs- Test Accuracy', {'fontsize':12})
ax.set_xlabel('Epochs')
ax.set_ylabel('Accuracy')
ax.plot(self.train_acc,'-ro')
ax.plot(self.test_acc,'-co')
ax.legend(['Train Accuracy','Test Accuracy'])
fig.savefig(f'{self.config["graph_dir"]}{"Metrics"}_{self.config["model_id"]}.png', facecolor=fig.get_facecolor())
plt.close(fig)
return
def state_management(self, fold, epoch, loss_train, loss_test=None, acc_train=None, acc_test=None, plot=True):
''' Save checkpoints and output performance graph. '''
# metrics for performance graph
self.train_loss.append(loss_train)
self.test_loss.append(loss_test)
self.train_acc.append(acc_train)
self.test_acc.append(acc_test)
# calculate progress
working_loss = loss_train if loss_test is None else loss_test # use test loss for comparison if provided
self.prev_loss = working_loss if working_loss < self.prev_loss else self.prev_loss
working_acc = acc_train if acc_test is None else acc_test
self.prev_acc = working_acc if working_acc > self.prev_acc else self.prev_acc
if self.prev_loss == working_loss and self.prev_acc == working_acc and self.config['cv_mode'] == 'blend': # best model output so far, save it if blending CV
fn = self.config['checkpoint_fn'].format(dir=self.config['checkpoint_dir'], fold='0', id=self.config['model_id']) # '{dir}fold_{fold}_{id}_checkpoint.pt',
torch.save(self.model.state_dict(), fn)
if epoch+1 == self.config['epochs'] and self.config['cv_mode'] != 'blend': # save each completed fold if using k-fold CV
fn = self.config['checkpoint_fn'].format(dir=self.config['checkpoint_dir'], fold=str(fold), id=self.config['model_id']) # '{dir}fold_{fold}_{id}_checkpoint.pt',
torch.save(self.model.state_dict(), fn)
if plot:
self.training_plot()
if epoch+1 == self.config['epochs'] and self.config['spot_check']: # spot check weights, biases and gradients
self.graphing.graph_parameters(self.model, fold, epoch, 0, types=['weight','bias'])
self.graphing.graph_parameters(self.model, fold, epoch, 0, types=['grad'])
return # could add patience and divergence checks for early stopping
def training(self):
''' Standard training loop w/ optional cross validation '''
bs = self.config['batch_size']
lr = self.config['learning_rate']
epochs = self.config['epochs']
dataset = self.training_set
step_check = 5 * math.ceil(int((len(dataset) - len(dataset)*self.config['test_size']) // (bs*5)) / 5)
kfolds = KFold(n_splits=self.config['kfolds'])
loss_function = nn.NLLLoss()
optimizer = torch.optim.AdamW(self.model.parameters(), lr=lr)
# spot check initial weights, biases if option set
if self.config['spot_check']:
self.graphing.graph_parameters(self.model, 0, 0, 0, types=['weight','bias'])
print(f'\n--- Training model {self.config["model_id"]} in mode "{self.config["cv_mode"]}" with {1 if self.config["cv_mode"]=="blend" else self.config["kfolds"]} folds\n')
# setup k-fold cross-validation
for fold, (train_idx, test_idx) in enumerate(kfolds.split(dataset)):
train_loader = DataLoader(dataset, batch_size=bs, sampler=SubsetRandomSampler(train_idx), drop_last=True)
test_loader = DataLoader(dataset, batch_size=bs, sampler=SubsetRandomSampler(test_idx), drop_last=True)
# if using k-fold "properly", reset model weights between folds
if self.config['cv_mode'] != 'blend' and fold > 0:
self.model.reset_weights(mode='init')
# epochs per fold
for e in range(0, epochs):
########
# train
########
losses_t = []
acc_t = []
self.model.train()
for step, batch in enumerate(train_loader):
optimizer.zero_grad()
batch = {k:v.to(DEVICE) for k,v in batch.items()}
predictions, outputs = self.model(**batch)
loss = loss_function(predictions, (torch.tensor)(batch['label']).squeeze())
loss.backward()
if self.config['clip_gradients']:
nn.utils.clip_grad_norm_(self.model.parameters(), self.config['clip_max_norm']) # ensure exploding gradients are managed if not debugging
losses_t.append(loss.detach().cpu().item())
acc_t.append(torch.sum(torch.argmax(predictions, dim=1, keepdim=True) == batch['label']).detach().cpu().item())
if step % step_check == 0:
print('- Fold %2d / Epoch %2d / Step %3d --- train nll %.10f (acc %.10f)' % (fold, e, step, np.mean(losses_t), np.sum(acc_t)/((step+1)*bs)), flush=True)
if self.config['check_outputs']: # visualize output distribution
self.graphing.graph_outputs(fold, e, step, outputs)
if self.config['check_weights']: # visualize weight distribution
self.graphing.graph_parameters(self.model, fold, e, step, types=['weight','bias'], spot_check=False)
if self.config['check_gradients']: # visualize gradients or clip
self.graphing.graph_parameters(self.model, fold, e, step, types=['grad'], spot_check=False)
optimizer.step()
del batch
########
# test
########
losses_e = []
acc_e = []
self.model.eval()
for batch in test_loader:
batch = {k:v.to(DEVICE) for k,v in batch.items()}
with torch.no_grad():
predictions, _ = self.model(**batch)
loss = loss_function(predictions, (torch.tensor)(batch['label']).squeeze())
losses_e.append(loss.detach().cpu().item())
acc_e.append(torch.sum(torch.argmax(predictions, dim=1, keepdim=True) == batch['label']).detach().cpu().item())
del batch
# calculate performance
train_nll = np.mean(losses_t)
test_nll = np.mean(losses_e)
train_acc = np.sum(acc_t) / (bs * len(train_loader))
test_acc = np.sum(acc_e) / (bs * len(test_loader))
self.state_management(fold, e, loss_train=train_nll, loss_test=test_nll, acc_train=train_acc, acc_test=test_acc, plot=True)
print('\n--- Fold %2d / Epoch %2d --- train nll %.10f (acc %.10f), --- test nll %.10f (acc %.10f)\n' % (fold, e, train_nll, train_acc, test_nll, test_acc), flush=True)
return
def evaluation(self):
'''
Method for evaluating a single model's effectiveness post training.
Includes logic to aggregate an ensemble outcome if multiple fold checkpoints are available.
'''
bs = self.config['batch_size']
dataset = self.eval_set
eval_loader = DataLoader(dataset, batch_size=bs, drop_last=False)
loss_function = nn.NLLLoss()
# determine model iterations (i.e. using k-fold checkpoints or blended checkpoint)
folds = 1 if self.config['cv_mode'] == 'blend' else self.config['kfolds']
print(f'\n--- Evaluating model {self.config["model_id"]} in mode "{self.config["cv_mode"]}" with {1 if self.config["cv_mode"]=="blend" else self.config["kfolds"]} folds')
ensemble_preds = []
ensemble_labels = []
for f in range(folds):
model_fn = self.config['checkpoint_fn'].format(dir=self.config['checkpoint_dir'], fold=str(f), id=self.config['model_id'])
if not os.path.exists(model_fn):
raise AssertionError('model checkpoint does not exist...something is wrong')
self.model.load_state_dict(torch.load(model_fn, map_location=DEVICE))
########
# eval
########
preds_e = []
labels_e = []
losses_e = []
acc_e = []
self.model.eval()
for batch in eval_loader:
batch = {k:v.to(DEVICE) for k,v in batch.items()}
with torch.no_grad():
predictions, _ = self.model(**batch)
loss = loss_function(predictions, (torch.tensor)(batch['label']).squeeze())
# collect metrics
losses_e.append(loss.detach().cpu().item())
acc_e.append(torch.sum(torch.argmax(predictions, dim=1, keepdim=True) == batch['label']).detach().cpu().item())
preds_e.extend(predictions.detach().cpu().tolist())
labels_e.extend(batch['label'].detach().cpu().tolist())
del batch
# save fold predictions for ensemble calculations
ensemble_preds.append(preds_e)
ensemble_labels = np.array(labels_e).squeeze(axis=1) if len(ensemble_labels) == 0 else ensemble_labels
# calculate metrics
eval_nll = np.mean(losses_e)
eval_acc = np.sum(acc_e) / (len(dataset))
print('\n--- Model "%s" fold "%d" evaluation nll loss of %.10f with %.10f accuracy' % (self.config['model_id'], f, eval_nll, eval_acc))
# ensemble calculations
ensemble_preds = np.transpose(ensemble_preds, (1, 0, 2)) # alter matrix to (samples X folds X prediction probabilities)
ensemble_preds = np.sum(ensemble_preds, axis=1) # sum all the probabilities by class and fold
ensemble_preds = np.argmax(ensemble_preds, axis=1) # select the class with the highest sum
acc_s = np.sum(ensemble_preds == ensemble_labels) / len(dataset) # compare predictions with actuals
print('\n--- Ensemble "%s" accuracy prediction is %.10f' % (self.config['model_id'], acc_s))