-
Notifications
You must be signed in to change notification settings - Fork 1
/
boe.py
1174 lines (934 loc) · 40.6 KB
/
boe.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
# -*- coding: utf-8 -*-
"""GRU_sequence+attention.ipynb
# Classifying OUV using GRU sequence model + Attention
## Imports
"""
import sys
sys.executable
from argparse import Namespace
from collections import Counter
import json
import os
import re
import string
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from tqdm import tqdm
from torch.autograd import Variable
import random
from sklearn.metrics import confusion_matrix
from scipy.special import softmax
import pickle
#import matplotlib.pyplot as plt
import torchtext
from torchtext.data import get_tokenizer
#tokenizer = get_tokenizer('spacy')
print("PyTorch version {}".format(torch.__version__))
print("GPU-enabled installation? {}".format(torch.cuda.is_available()))
device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
print(device)
args = Namespace(
# Data and Path information
frequency_cutoff=1,
model_state_file='model.pth',
ouv_csv='Data/ouv_with_splits_full.csv',
#ouv_csv='Data/all_with_splits_full.csv',
prior_csv = 'Data/Coappearance_matrix.csv',
save_dir='model_storage/boe/',
vectorizer_file='vectorizer.json',
# Model hyper parameters
glove_filepath='Data/glove/glove.6B.300d.txt',
use_glove=True,
embedding_size=300,
hidden_dim=200,
# Training hyper parameters
batch_size=64,
early_stopping_criteria=5,
learning_rate=0.0005,
l2=1e-5,
dropout_p=0.1,
k = 3,
fuzzy = True,
fuzzy_how = 'prior',
fuzzy_lambda = 0.01,
num_epochs=100,
seed=1337,
# Runtime options
catch_keyboard_interrupt=True,
cuda=True,
expand_filepaths_to_save_dir=True,
reload_from_files=False,
)
classes = ['Criteria i', 'Criteria ii', 'Criteria iii', 'Criteria iv', 'Criteria v', 'Criteria vi',
'Criteria vii', 'Criteria viii', 'Criteria ix', 'Criteria x', 'Others']
"""## Data Vectorization Classes
### The Vocabulary
"""
class Vocabulary(object):
"""Class to process text and extract vocabulary for mapping"""
def __init__(self, token_to_idx=None):
"""
Args:
token_to_idx (dict): a pre-existing map of tokens to indices
add_unk (bool): a flag that indicates whether to add the UNK token
unk_token (str): the UNK token to add into the Vocabulary
"""
if token_to_idx is None:
token_to_idx = {}
self._token_to_idx = token_to_idx
self._idx_to_token = {idx: token
for token, idx in self._token_to_idx.items()}
def to_serializable(self):
""" returns a dictionary that can be serialized """
return {'token_to_idx': self._token_to_idx}
@classmethod
def from_serializable(cls, contents):
""" instantiates the Vocabulary from a serialized dictionary """
return cls(**contents)
def add_token(self, token):
"""Update mapping dicts based on the token.
Args:
token (str): the item to add into the Vocabulary
Returns:
index (int): the integer corresponding to the token
"""
if token in self._token_to_idx:
index = self._token_to_idx[token]
else:
index = len(self._token_to_idx)
self._token_to_idx[token] = index
self._idx_to_token[index] = token
return index
def add_many(self, tokens):
"""Add a list of tokens into the Vocabulary
Args:
tokens (list): a list of string tokens
Returns:
indices (list): a list of indices corresponding to the tokens
"""
return [self.add_token(token) for token in tokens]
def lookup_token(self, token):
"""Retrieve the index associated with the token
or the UNK index if token isn't present.
Args:
token (str): the token to look up
Returns:
index (int): the index corresponding to the token
Notes:
`unk_index` needs to be >=0 (having been added into the Vocabulary)
for the UNK functionality
"""
return self._token_to_idx[token]
def lookup_index(self, index):
"""Return the token associated with the index
Args:
index (int): the index to look up
Returns:
token (str): the token corresponding to the index
Raises:
KeyError: if the index is not in the Vocabulary
"""
if index not in self._idx_to_token:
raise KeyError("the index (%d) is not in the Vocabulary" % index)
return self._idx_to_token[index]
def __str__(self):
return "<Vocabulary(size=%d)>" % len(self)
def __len__(self):
return len(self._token_to_idx)
class SequenceVocabulary(Vocabulary):
def __init__(self, token_to_idx=None, unk_token="<UNK>",
mask_token="<MASK>", begin_seq_token="<BEGIN>",
end_seq_token="<END>"):
super(SequenceVocabulary, self).__init__(token_to_idx)
self._mask_token = mask_token
self._unk_token = unk_token
self._begin_seq_token = begin_seq_token
self._end_seq_token = end_seq_token
self.mask_index = self.add_token(self._mask_token)
self.unk_index = self.add_token(self._unk_token)
self.begin_seq_index = self.add_token(self._begin_seq_token)
self.end_seq_index = self.add_token(self._end_seq_token)
def to_serializable(self):
contents = super(SequenceVocabulary, self).to_serializable()
contents.update({'unk_token': self._unk_token,
'mask_token': self._mask_token,
'begin_seq_token': self._begin_seq_token,
'end_seq_token': self._end_seq_token})
return contents
def lookup_token(self, token):
"""Retrieve the index associated with the token
or the UNK index if token isn't present.
Args:
token (str): the token to look up
Returns:
index (int): the index corresponding to the token
Notes:
`unk_index` needs to be >=0 (having been added into the Vocabulary)
for the UNK functionality
"""
if self.unk_index >= 0:
return self._token_to_idx.get(token, self.unk_index)
else:
return self._token_to_idx[token]
"""### The Vectorizer"""
class OuvVectorizer(object):
""" The Vectorizer which coordinates the Vocabularies and puts them to use"""
def __init__(self, ouv_vocab):
"""
Args:
review_vocab (Vocabulary): maps words to integers
"""
self.ouv_vocab = ouv_vocab
def vectorize(self, data, vector_length = -1):
"""Create a collapsed one-hit vector for the ouv data
Args:
data (str): the ouv description data
vector_length (int): an argument for forcing the length of index vector
Returns:
the vectorized data (np.ndarray)
"""
indices = []
indices.extend(self.ouv_vocab.lookup_token(token) for token in data.split(' '))
if vector_length < 0:
vector_length = len(indices)
out_vector = np.zeros(vector_length, dtype=np.int64)
out_vector[:len(indices)] = indices
out_vector[len(indices):] = self.ouv_vocab.mask_index
return out_vector, len(indices)
@classmethod
def from_dataframe(cls, ouv_df, cutoff=5):
"""Instantiate the vectorizer from the dataset dataframe
Args:
ouv_df (pandas.DataFrame): the ouv dataset
cutoff (int): the parameter for frequency-based filtering
Returns:
an instance of the OuvVectorizer
"""
# Add top words if count > provided count
word_counts = Counter()
for data in ouv_df.data:
for word in data.split(' '):
if word not in string.punctuation:
word_counts[word] += 1
ouv_vocab = SequenceVocabulary()
for word, count in word_counts.items():
if count > cutoff:
ouv_vocab.add_token(word)
return cls(ouv_vocab)
@classmethod
def from_serializable(cls, contents):
"""Instantiate a OuvVectorizer from a serializable dictionary
Args:
contents (dict): the serializable dictionary
Returns:
an instance of the OuvVectorizer class
"""
ouv_vocab = SequenceVocabulary.from_serializable(contents['ouv_vocab'])
return cls(ouv_vocab=ouv_vocab)
def to_serializable(self):
"""Create the serializable dictionary for caching
Returns:
contents (dict): the serializable dictionary
"""
return {'ouv_vocab': self.ouv_vocab.to_serializable()}
"""### The Dataset"""
class OuvDataset(Dataset):
def __init__(self, ouv_df, vectorizer):
"""
Args:
ouv_df (pandas.DataFrame): the dataset
vectorizer (ReviewVectorizer): vectorizer instantiated from dataset
"""
self.ouv_df = ouv_df
self._vectorizer = vectorizer
# +0 if not using begin_seq and end seq, +1 if only using begin_seq, +2 if using both begin and end seq tokens
measure_len = lambda context: len(context.split(" "))
self._max_seq_length = max(map(measure_len, ouv_df.data)) + 0
self.train_df = self.ouv_df[self.ouv_df.split=='train']
self.train_size = len(self.train_df)
self.val_df = self.ouv_df[self.ouv_df.split=='dev']
self.validation_size = len(self.val_df)
self.test_df = self.ouv_df[self.ouv_df.split=='test']
self.test_size = len(self.test_df)
self._lookup_dict = {'train': (self.train_df, self.train_size),
'val': (self.val_df, self.validation_size),
'test': (self.test_df, self.test_size)}
self.set_split('train')
@classmethod
def load_dataset_and_make_vectorizer(cls, ouv_csv, cutoff):
"""Load dataset and make a new vectorizer from scratch
Args:
ouv_csv (str): location of the dataset
cutoff (int): the boundary to set the words into unknown
Returns:
an instance of OuvDataset
"""
ouv_df = pd.read_csv(ouv_csv)
train_ouv_df = ouv_df[ouv_df.split=='train']
return cls(ouv_df, OuvVectorizer.from_dataframe(train_ouv_df, cutoff=cutoff))
@classmethod
def load_dataset_and_load_vectorizer(cls, ouv_csv, vectorizer_filepath):
"""Load dataset and the corresponding vectorizer.
Used in the case in the vectorizer has been cached for re-use
Args:
ouv_csv (str): location of the dataset
vectorizer_filepath (str): location of the saved vectorizer
Returns:
an instance of OuvDataset
"""
ouv_df = pd.read_csv(ouv_csv)
vectorizer = cls.load_vectorizer_only(vectorizer_filepath)
return cls(ouv_df, vectorizer)
@staticmethod
def load_vectorizer_only(vectorizer_filepath):
"""a static method for loading the vectorizer from file
Args:
vectorizer_filepath (str): the location of the serialized vectorizer
Returns:
an instance of ReviewVectorizer
"""
with open(vectorizer_filepath) as fp:
return OuvVectorizer.from_serializable(json.load(fp))
def save_vectorizer(self, vectorizer_filepath):
"""saves the vectorizer to disk using json
Args:
vectorizer_filepath (str): the location to save the vectorizer
"""
with open(vectorizer_filepath, "w") as fp:
json.dump(self._vectorizer.to_serializable(), fp)
def get_vectorizer(self):
""" returns the vectorizer """
return self._vectorizer
def set_split(self, split="train"):
""" selects the splits in the dataset using a column in the dataframe
Args:
split (str): one of "train", "val", or "test"
"""
self._target_split = split
self._target_df, self._target_size = self._lookup_dict[split]
def __len__(self):
return self._target_size
def __getitem__(self, index):
"""the primary entry point method for PyTorch datasets
Args:
index (int): the index to the data point
Returns:
a dictionary holding the data point's features (x_data) and component for labels (y_target and y_fuzzy)
"""
row = self._target_df.iloc[index]
ouv_vector, vec_length = \
self._vectorizer.vectorize(row.data, self._max_seq_length)
true_label = \
np.fromstring(row.true[1:-1],dtype=float, sep=' ')
if len(true_label)==10:
true_label = np.append(true_label,0.0)
fuzzy_label = \
np.fromstring(row.fuzzy[1:-1],dtype=float, sep=' ')
return {'x_data': ouv_vector,
'y_target': true_label,
'y_fuzzy': fuzzy_label,
'x_length': vec_length
}
def get_num_batches(self, batch_size):
"""Given a batch size, return the number of batches in the dataset
Args:
batch_size (int)
Returns:
number of batches in the dataset
"""
return len(self) // batch_size
def generate_batches(dataset, batch_size, shuffle=True,
drop_last=True, device="cpu"):
"""
A generator function which wraps the PyTorch DataLoader. It will
ensure each tensor is on the write device location.
"""
dataloader = DataLoader(dataset=dataset, batch_size=batch_size,
shuffle=shuffle, drop_last=drop_last)
for data_dict in dataloader:
out_data_dict = {}
for name, tensor in data_dict.items():
out_data_dict[name] = data_dict[name].to(device)
yield out_data_dict
"""## The Model: GRU sequential Model"""
class BoMClassifier(nn.Module):
def __init__(self, embedding_size, num_embeddings, hidden_dim, num_classes, dropout_p,
pretrained_embeddings=None, padding_idx=0):
"""
Args:
embedding_size (int): size of the embedding vectors
num_embeddings (int): number of embedding vectors
hidden_dim (int): the size of the hidden dimension
num_classes (int): the number of classes in classification
dropout_p (float): a dropout parameter
pretrained_embeddings (numpy.array): previously trained word embeddings
default is None. If provided,
padding_idx (int): an index representing a null position
"""
super(BoMClassifier, self).__init__()
if pretrained_embeddings is None:
self.emb = nn.Embedding(embedding_dim=embedding_size,
num_embeddings=num_embeddings,
padding_idx=padding_idx)
else:
pretrained_embeddings = torch.from_numpy(pretrained_embeddings).float()
self.emb = nn.Embedding.from_pretrained(pretrained_embeddings,
padding_idx=padding_idx,
freeze=False)
self._dropout_p = dropout_p
self.dropout = nn.Dropout(dropout_p)
self.padding_idx = padding_idx
#self.prelu = nn.PReLU()
self.fc1 = nn.Linear(embedding_size, hidden_dim)
self.fc2 = nn.Linear(hidden_dim, num_classes)
self.fc0 = nn.Linear(embedding_size, num_classes)
def forward(self, x_in, apply_softmax=False):
"""The forward pass of the classifier
Args:
x_in (torch.Tensor): an input data tensor.
x_in.shape should be (batch, dataset._max_seq_length)
apply_softmax (bool): a flag for the softmax activation
should be false if used with the Cross Entropy losses
Returns:
the resulting tensor. tensor.shape should be (batch, num_classes)
"""
# embed and permute so features are channels
x_embedded = self.emb(x_in)
padding_mask = x_in.eq(self.padding_idx)
length = (~padding_mask).sum(dim=1)
x_embedded = x_embedded.sum(dim=1)/(length.view(x_embedded.shape[0],1))
# mlp classifier
#intermediate_vector = F.relu(F.dropout(self.fc1(x_embedded), p=self._dropout_p))
#prediction_vector = self.fc2(intermediate_vector)
# logistic regression
intermediate_vector = F.relu(self.dropout(self.fc1(x_embedded)))
prediction_vector = self.fc2(intermediate_vector)
if apply_softmax:
prediction_vector = F.softmax(prediction_vector, dim=1)
return prediction_vector
"""## Training Routine
### Helper Functions
"""
def make_train_state(args):
return {'stop_early': False,
'early_stopping_step': 0,
'early_stopping_best_k_acc_val': 0,
'learning_rate': args.learning_rate,
'epoch_index': 0,
'train_loss': [],
'train_1_acc': [],
'train_k_acc': [],
'train_k_jac': [],
'val_loss': [],
'val_1_acc': [],
'val_k_acc': [],
'val_k_jac': [],
'test_loss': -1,
'test_1_acc': -1,
'test_k_acc':-1,
'test_k_jac':-1,
'model_filename': args.model_state_file}
def update_train_state(args, model, train_state):
"""Handle the training state updates.
Components:
- Early Stopping: Prevent overfitting.
- Model Checkpoint: Model is saved if the model is better
:param args: main arguments
:param model: model to train
:param train_state: a dictionary representing the training state values
:returns:
a new train_state
"""
# Save one model at least
if train_state['epoch_index'] == 0:
torch.save(model.state_dict(), train_state['model_filename'])
train_state['stop_early'] = False
# Save model if performance improved
elif train_state['epoch_index'] >= 1:
acc_tm1, acc_t = train_state['val_k_acc'][-2:]
# If accuracy worsened
if acc_t <= train_state['early_stopping_best_k_acc_val']:
# Update step
train_state['early_stopping_step'] += 1
# Loss decreased
else:
# Save the best model from sklearn
if acc_t > train_state['early_stopping_best_k_acc_val']:
train_state['early_stopping_best_k_acc_val'] = acc_t
torch.save(model.state_dict(), train_state['model_filename'])
# Reset early stopping step
train_state['early_stopping_step'] = 0
# Stop early ?
train_state['stop_early'] = \
train_state['early_stopping_step'] >= args.early_stopping_criteria
return train_state
"""### Evaluation Metrics"""
def compute_cross_entropy(y_pred, y_target):
y_target = y_target.cpu().float()
y_pred = y_pred.cpu().float()
criterion = nn.BCEWithLogitsLoss()
return criterion(y_target, y_pred)
def compute_1_accuracy(y_pred, y_target):
y_target_indices = y_target.max(dim=1)[1]
y_pred_indices = y_pred.max(dim=1)[1]
n_correct = torch.eq(y_pred_indices, y_target_indices).sum().item()
return n_correct / len(y_pred_indices) * 100
def compute_k_accuracy(y_pred, y_target, k=3):
y_pred_indices = y_pred.topk(k, dim=1)[1]
y_target_indices = y_target.max(dim=1)[1]
n_correct = torch.tensor([y_pred_indices[i] in y_target_indices[i] for i in range(len(y_pred))]).sum().item()
return n_correct / len(y_pred_indices) * 100
def compute_k_jaccard_index(y_pred, y_target, k=3):
y_target_indices = y_target.topk(k, dim=1)[1]
y_pred_indices = y_pred.max(dim=1)[1]
jaccard = torch.tensor([len(np.intersect1d(y_target_indices[i], y_pred_indices[i]))/
len(np.union1d(y_target_indices[i], y_pred_indices[i]))
for i in range(len(y_pred))]).sum().item()
return jaccard / len(y_pred_indices)
def compute_jaccard_index(y_pred, y_target, k=3, multilabel=False):
threshold = 1.0/(k+1)
threshold_2 = 0.5
if multilabel:
y_pred_indices = y_pred.gt(threshold_2)
else:
y_pred_indices = y_pred.gt(threshold)
y_target_indices = y_target.gt(threshold)
jaccard = ((y_target_indices*y_pred_indices).sum(axis=1)/((y_target_indices+y_pred_indices).sum(axis=1)+1e-8)).sum().item()
return jaccard / len(y_pred_indices)
def softmax_sensitive(T):
T = torch.exp(T) - 1 + 1e-9
if len(T.shape)==1:
return T/T.sum()
return T/(T.sum(axis=1).unsqueeze(1))
def cross_entropy(pred, soft_targets):
logsoftmax = nn.LogSoftmax(dim=1)
return torch.mean(torch.sum(- soft_targets * logsoftmax(pred), 1))
# convert a df to tensor to be used in pytorch
def df_to_tensor(df):
device = args.device
return torch.from_numpy(df.values).float().to(device)
def get_prior():
prior = pd.read_csv(args.prior_csv,sep=';',names=classes[:-1], skiprows=1)
prior['Others'] = 1
prior = prior.T
prior['Others'] = 1
prior = df_to_tensor(prior)
return(prior)
def compute_fuzzy_label(y_target, y_fuzzy, fuzzy=False, how='uni', lbd=0):
'''
Using two sets of prediction labels and fuzziness parameters to compute the fuzzy label in the form as
a distribution over classes
Args:
y_target (torch.Tensor) of shape (n_batch, n_classes): the true label of the ouv description
y_fuzzy (torch.Tensor) of shape (n_batch, n_classes): the fuzzy label of the ouv description
fuzzy (bool): whether or not to turn on the fuzziness option
how (string): the way fuzziness weights are used, one of the options in {'uni', 'prior'}
lbd (float): the scaler applied to the fuzziness of the label
Returns:
A pytorch Tensor of shape (n_batch, n_classes): The processed label in the form of distribution that add to 1
'''
assert y_target.shape == y_fuzzy.shape, 'target labels must have the same size'
assert how in {'uni', 'prior'}, '''how must be one of the two options in {'uni', 'prior'}'''
if not fuzzy:
return softmax_sensitive(y_target)
if how == 'uni':
y_label = y_target + lbd * y_fuzzy
return softmax_sensitive(y_label)
### TO DO ###
elif how == 'prior':
prior = get_prior()
y_inter = torch.matmul(y_target.float(),prior)
y_inter = y_inter/(y_inter.max(dim=1, keepdim=True)[0])
y_label = y_target + lbd * y_fuzzy * y_inter
return softmax_sensitive(y_label).to(args.device)
"""### General Utilities"""
def set_seed_everywhere(seed, cuda):
np.random.seed(seed)
torch.manual_seed(seed)
random.seed(seed)
if cuda:
torch.cuda.manual_seed_all(seed)
def handle_dirs(dirpath):
if not os.path.exists(dirpath):
os.makedirs(dirpath)
def load_glove_from_file(glove_filepath):
"""
Load the GloVe embeddings
Args:
glove_filepath (str): path to the glove embeddings file
Returns:
word_to_index (dict), embeddings (numpy.ndarary)
"""
word_to_index = {}
embeddings = []
with open(glove_filepath, "r") as fp:
for index, line in enumerate(fp):
line = line.split(" ") # each line: word num1 num2 ...
word_to_index[line[0]] = index # word = line[0]
embedding_i = np.array([float(val) for val in line[1:]])
embeddings.append(embedding_i)
return word_to_index, np.stack(embeddings)
def make_embedding_matrix(glove_filepath, words):
"""
Create embedding matrix for a specific set of words.
Args:
glove_filepath (str): file path to the glove embeddigns
words (list): list of words in the dataset
"""
word_to_idx, glove_embeddings = load_glove_from_file(glove_filepath)
embedding_size = glove_embeddings.shape[1]
final_embeddings = np.zeros((len(words), embedding_size))
for i, word in enumerate(words):
if word in word_to_idx:
final_embeddings[i, :] = glove_embeddings[word_to_idx[word]]
else:
embedding_i = torch.ones(1, embedding_size)
torch.nn.init.xavier_uniform_(embedding_i)
final_embeddings[i, :] = embedding_i
return final_embeddings
def initialization(embeddings):
set_seed_everywhere(args.seed, args.cuda)
if args.reload_from_files:
# training from a checkpoint
dataset = OuvDataset.load_dataset_and_load_vectorizer(args.ouv_csv, args.vectorizer_file)
else:
# create dataset and vectorizer
dataset = OuvDataset.load_dataset_and_make_vectorizer(args.ouv_csv, cutoff=args.frequency_cutoff)
dataset.save_vectorizer(args.vectorizer_file)
vectorizer = dataset.get_vectorizer()
classifier = BoMClassifier(embedding_size=args.embedding_size,
num_embeddings=len(vectorizer.ouv_vocab),
hidden_dim=args.hidden_dim,
num_classes=len(classes),
dropout_p=args.dropout_p,
pretrained_embeddings=embeddings,
padding_idx=0)
return dataset, vectorizer, classifier
def training_loop(embeddings, verbose=False):
dataset,vectorizer,classifier = initialization(embeddings=embeddings)
classifier = classifier.to(args.device)
loss_func = cross_entropy
optimizer = optim.Adam(classifier.parameters(), lr=args.learning_rate, weight_decay=args.l2)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer=optimizer,
mode='min', factor=0.5,
patience=1)
train_state = make_train_state(args)
if verbose:
epoch_bar = tqdm(desc='training routine',
total=args.num_epochs)
train_bar = tqdm(desc='split=train',
total=dataset.get_num_batches(args.batch_size),
leave=True)
val_bar = tqdm(desc='split=val',
total=dataset.get_num_batches(args.batch_size),
leave=True)
dataset.set_split('train')
dataset.set_split('val')
try:
for epoch_index in range(args.num_epochs):
train_state['epoch_index'] = epoch_index
# Iterate over training dataset
# setup: batch generator, set loss and acc to 0, set train mode on
dataset.set_split('train')
batch_generator = generate_batches(dataset,
batch_size=args.batch_size,
device=args.device)
running_loss = 0.0
running_1_acc = 0.0
running_k_acc = 0.0
running_k_jac = 0.0
classifier.train()
for batch_index, batch_dict in enumerate(batch_generator):
# step 1. zero the gradients
optimizer.zero_grad()
# step 2. get the data compute fuzzy labels
X = batch_dict['x_data']
y_target = batch_dict['y_target']
y_fuzzy = batch_dict['y_fuzzy']
Y = compute_fuzzy_label(y_target, y_fuzzy, fuzzy= args.fuzzy,
how=args.fuzzy_how, lbd = args.fuzzy_lambda)
# step 3. compute the output
y_pred = classifier(X)
# step 4. compute the loss
loss = loss_func(y_pred, Y)
loss_t = loss.item()
running_loss += (loss_t - running_loss) / (batch_index + 1)
# step 5. use loss to produce gradients
loss.backward()
# step 6. use optimizer to take gradient step
optimizer.step()
# -----------------------------------------
# compute the accuracies
acc_1_t = compute_1_accuracy(y_pred, Y)
acc_k_t = compute_k_accuracy(y_pred, Y, args.k)
jac_k_t = compute_jaccard_index(y_pred, Y, len(classes))
running_1_acc += (acc_1_t - running_1_acc) / (batch_index + 1)
running_k_acc += (acc_k_t - running_k_acc) / (batch_index + 1)
running_k_jac += (jac_k_t - running_k_jac) / (batch_index + 1)
# update bar
if verbose:
train_bar.set_postfix(loss=running_loss,
acc_1=running_1_acc,
acc_k=running_k_acc,
jac_k=running_k_jac,
epoch=epoch_index)
train_bar.update()
train_state['train_loss'].append(running_loss)
train_state['train_1_acc'].append(running_1_acc)
train_state['train_k_acc'].append(running_k_acc)
train_state['train_k_jac'].append(running_k_jac)
# Iterate over val dataset
# setup: batch generator, set loss and acc to 0; set eval mode on
dataset.set_split('val')
batch_generator = generate_batches(dataset,
batch_size=args.batch_size,
device=args.device)
running_loss = 0.0
running_1_acc = 0.0
running_k_acc = 0.0
running_k_jac = 0.0
classifier.eval()
for batch_index, batch_dict in enumerate(batch_generator):
# step 2. get the data compute fuzzy labels
X = batch_dict['x_data']
y_target = batch_dict['y_target']
y_fuzzy = batch_dict['y_fuzzy']
Y = compute_fuzzy_label(y_target, y_fuzzy, fuzzy= args.fuzzy,
how=args.fuzzy_how, lbd = args.fuzzy_lambda)
# step 3. compute the output
with torch.no_grad():
y_pred = classifier(X)
# step 4. compute the loss
loss = loss_func(y_pred, Y)
loss_t = loss.item()
running_loss += (loss_t - running_loss) / (batch_index + 1)
# -----------------------------------------
# compute the accuracies
acc_1_t = compute_1_accuracy(y_pred, Y)
acc_k_t = compute_k_accuracy(y_pred, Y, args.k)
jac_k_t = compute_jaccard_index(y_pred, Y, len(classes))
running_1_acc += (acc_1_t - running_1_acc) / (batch_index + 1)
running_k_acc += (acc_k_t - running_k_acc) / (batch_index + 1)
running_k_jac += (jac_k_t - running_k_jac) / (batch_index + 1)
# update bar
if verbose:
val_bar.set_postfix(loss=running_loss,
acc_1=running_1_acc,
acc_k=running_k_acc,
jac_k=running_k_jac,
epoch=epoch_index)
val_bar.update()
train_state['val_loss'].append(running_loss)
train_state['val_1_acc'].append(running_1_acc)
train_state['val_k_acc'].append(running_k_acc)
train_state['val_k_jac'].append(running_k_jac)
train_state = update_train_state(args=args, model=classifier,
train_state=train_state)
scheduler.step(train_state['val_loss'][-1])
if train_state['stop_early']:
break
if verbose:
train_bar.n = 0
val_bar.n = 0
epoch_bar.update()
except KeyboardInterrupt:
print("Exiting loop")
pass
return train_state
def update_best_config(current_best,train_state, key):
val_k_acc = train_state['early_stopping_best_k_acc_val']
if val_k_acc > current_best['k_acc']:
current_best['k_acc'] = val_k_acc
current_best['args'] = str(args)
#current_best['state'] = [train_state]
current_best['key'] = key
def Hypersearch(hyperdict, current_best, embeddings):
'''
Perform a hyperparameter search using grid search and save into the hyperdict
'''
s_frequency_cutoff = [1,3,5]
s_hidden_dim = [50, 100, 150, 200]
s_batch_size = [64,128,256]
#s_fuzzy_lambda = [0, 0.01, 0.05, 0.1, 0.2, 0.5, 1]
search_bar = tqdm(desc='hyper_searching',
total=len(s_frequency_cutoff)*len(s_hidden_dim)*len(s_batch_size))
i=0
for frq in s_frequency_cutoff:
for dim in s_hidden_dim:
for bs in s_batch_size:
args.frequency_cutoff = frq
args.hidden_dim = dim
args.batch_size = bs
key = (frq, dim, bs)
if not key in hyperdict:
train_state = training_loop(embeddings = embeddings, verbose=True)
hyperdict[key] = train_state
update_best_config(current_best,train_state)
search_bar.set_postfix(best_k_acc = current_best['k_acc'],
config = key)
search_bar.update()
i+=1
if i%100==0:
best_df = pd.DataFrame(current_best)
best_df.to_csv(args.save_dir+'best_config.csv')
best_df = pd.DataFrame(current_best)
best_df.to_csv(args.save_dir+'best_config.csv')
def fuzzy_exp(embeddings, hyperdict,verbose):
'''
Perform a hyperparameter search using grid search and save into the hyperdict
'''
#s_fuzzy_how = ['uni','prior', 'origin']
#s_fuzzy_lambda = [0, 0.01, 0.05, 0.1, 0.2, 0.5, 1]
s_fuzzy_how = ['prior']
s_fuzzy_lambda = [0.01]
search_bar = tqdm(desc='hyper_searching',
total=len(s_fuzzy_lambda)*len(s_fuzzy_how))
i=0
for how in s_fuzzy_how:
for lbd in s_fuzzy_lambda:
args.fuzzy_how = how
args.fuzzy_lambda = lbd
key = (how, lbd)
if not key in hyperdict: