generated from aaivu/aaivu-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline_with_eval_With_Nltk.py
1176 lines (1002 loc) · 45.1 KB
/
baseline_with_eval_With_Nltk.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
#Please use python 3.5 or above
import numpy as np
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Reshape, Flatten, RepeatVector, Embedding, LSTM, Concatenate, merge, Dropout, CuDNNLSTM, \
Convolution1D, MaxPooling1D, Activation, GRU, GlobalMaxPooling1D, GlobalAveragePooling1D, Conv1D, Bidirectional, \
Conv2D, MaxPooling2D, MaxPool2D
from keras.layers import Input, concatenate
from keras.models import Model
from keras import optimizers
from keras.models import load_model
from nltk.tokenize import TweetTokenizer
import json, argparse, os
import re
import io
import sys
import fasttext
import emoji
import keras
from emoji import UNICODE_EMOJI
# from keras_self_attention import SeqSelfAttention
import string
# from nltk.corpus import stopwords
# import regex
# Path to training and testing data file. This data can be downloaded from a link, details of which will be provided.
trainDataPath = ""
testDataPath = ""
# Output file that will be generated. This file can be directly submitted.
solutionPath = ""
# Path to directory where GloVe file is saved.
gloveDir = ""
emojiDir = ""
NUM_FOLDS = None # Value of K in K-fold Cross Validation
NUM_CLASSES = None # Number of classes - Happy, Sad, Angry, Others
MAX_NB_WORDS = None # To set the upper limit on the number of tokens extracted using keras.preprocessing.text.Tokenizer
MAX_SEQUENCE_LENGTH = None # All sentences having lesser number of words than this will be padded
EMBEDDING_DIM = None # The dimension of the word embeddings
BATCH_SIZE = None # The batch size to be chosen for training the model.
LSTM_DIM = None # The dimension of the representations learnt by the LSTM model
DROPOUT = None # Fraction of the units to drop for the linear transformation of the inputs. Ref - https://keras.io/layers/recurrent/
NUM_EPOCHS = None # Number of epochs to train a model for
label2emotion = {0:"others", 1:"happy", 2: "sad", 3:"angry"}
emotion2label = {"others":0, "happy":1, "sad":2, "angry":3}
emoji2emoticons = {'😑': ':|', '😖': ':(', '😯': ':o', '😝': ':p', '😐': ':|',
'😈': ':)', '🙁': ':(', '😎': ':)', '😞': ':(', '♥️': '<3', '💕': 'love',
'😀': ':d', '😢': ":(", '👍': 'ok', '😇': ':)', '😜': ':p',
'💙': 'love', '☹️': ':(', '😘': ':)', '🤔': 'hmm', '😲': ':o',
'🙂': ':)', '\U0001f923': ':d', '😂': ':d', '👿': ':(', '😛': ':p',
'😉': ';)', '🤓': '8-)'}
def preprocessData(dataFilePath, mode):
"""Load data from a file, process and return indices, conversations and labels in separate lists
Input:
dataFilePath : Path to train/test file to be processed
mode : "train" mode returns labels. "test" mode doesn't return labels.
Output:
indices : Unique conversation ID list
conversations : List of 3 turn conversations, processed and each turn separated by the <eos> tag
labels : [Only available in "train" mode] List of labels
"""
indices = []
conversations = []
labels = []
n = 0
with io.open(dataFilePath, encoding="utf8") as finput:
finput.readline()
for line in finput:
# Convert multiple instances of . ? ! , to single instance
# okay...sure -> okay . sure
# okay???sure -> okay ? sure
# Add whitespace around such punctuation
# okay!sure -> okay ! sure
# line.lower()
allchars = [str for str in line]
emoji_list = [c for c in allchars if c in emoji.UNICODE_EMOJI]
# clean_text = ' '.join([str for str in line.split() if not any(i in str for i in emoji_list)])
repeatedChars = ['.', '?', '!', ',']
repeatedChars = repeatedChars + emoji_list
for c in repeatedChars:
lineSplit = line.split(c)
while True:
try:
lineSplit.remove('')
except:
break
cSpace = ' ' + c + ' '
line = cSpace.join(lineSplit)
line = line.strip().split('\t')
if mode == "train":
# Train data contains id, 3 turns and label
label = emotion2label[line[4]]
labels.append(label)
conv = ' <eos> '.join(line[1:4])
# Remove any duplicate spaces
duplicateSpacePattern = re.compile(r'\ +')
conv = re.sub(duplicateSpacePattern, ' ', conv)
indices.append(int(line[0]))
conversations.append(conv.lower())
if mode == "train":
return indices, conversations, labels
else:
return indices, conversations
def preprocessDataV(dataFilePath, mode, preprocess=False):
"""Load data from a file, process and return indices, conversations and labels in separate lists
Input:
dataFilePath : Path to train/test file to be processed
mode : "train" mode returns labels. "test" mode doesn't return labels.
Output:
indices : Unique conversation ID list
conversations : List of 3 turn conversations, processed and each turn separated by the <eos> tag
labels : [Only available in "train" mode] List of labels
"""
indices = []
conversations = []
labels = []
with io.open(dataFilePath, encoding="utf8") as finput:
finput.readline()
for line in finput:
# Convert multiple instances of . ? ! , to single instance
# okay...sure -> okay . sure
# okay???sure -> okay ? sure
# Add whitespace around such punctuation
# okay!sure -> okay ! sure
repeatedChars = ['.', '?', '!', ',']
for c in repeatedChars:
lineSplit = line.split(c)
while True:
try:
lineSplit.remove('')
except:
break
cSpace = ' ' + c + ' '
line = cSpace.join(lineSplit)
line = line.strip().split('\t')
if mode == "train":
# Train data contains id, 3 turns and label
label = emotion2label[line[4]]
labels.append(label)
conv = ' <eos> '.join(line[1:4])
# Remove any duplicate spaces
duplicateSpacePattern = re.compile(r'\ +')
conv = re.sub(duplicateSpacePattern, ' ', conv)
if preprocess:
stray_punct = ['‑', '-', "^", ":",
";", "#", ")", "(", "*", "=", "\\", "/"]
for punct in stray_punct:
conv = conv.replace(punct, "")
if preprocess:
processedData = regex.cleanText(conv.lower(), remEmojis=1).lower()
processedData = processedData.replace("'", "")
# Remove numbers
processedData = ''.join([i for i in processedData if not i.isdigit()])
else:
processedData = conv.lower()
indices.append(int(line[0]))
conversations.append(processedData)
if mode == "train":
return indices, conversations, labels
else:
return indices, conversations
def getMetrics(predictions, ground):
"""Given predicted labels and the respective ground truth labels, display some metrics
Input: shape [# of samples, NUM_CLASSES]
predictions : Model output. Every row has 4 decimal values, with the highest belonging to the predicted class
ground : Ground truth labels, converted to one-hot encodings. A sample belonging to Happy class will be [0, 1, 0, 0]
Output:
accuracy : Average accuracy
microPrecision : Precision calculated on a micro level. Ref - https://datascience.stackexchange.com/questions/15989/micro-average-vs-macro-average-performance-in-a-multiclass-classification-settin/16001
microRecall : Recall calculated on a micro level
microF1 : Harmonic mean of microPrecision and microRecall. Higher value implies better classification
"""
# [0.1, 0.3 , 0.2, 0.1] -> [0, 1, 0, 0]
discretePredictions = to_categorical(predictions.argmax(axis=1))
truePositives = np.sum(discretePredictions*ground, axis=0)
falsePositives = np.sum(np.clip(discretePredictions - ground, 0, 1), axis=0)
falseNegatives = np.sum(np.clip(ground-discretePredictions, 0, 1), axis=0)
print("True Positives per class : ", truePositives)
print("False Positives per class : ", falsePositives)
print("False Negatives per class : ", falseNegatives)
# ------------- Macro level calculation ---------------
macroPrecision = 0
macroRecall = 0
# We ignore the "Others" class during the calculation of Precision, Recall and F1
for c in range(1, NUM_CLASSES):
precision = truePositives[c] / (truePositives[c] + falsePositives[c])
macroPrecision += precision
recall = truePositives[c] / (truePositives[c] + falseNegatives[c])
macroRecall += recall
f1 = ( 2 * recall * precision ) / (precision + recall) if (precision+recall) > 0 else 0
print("Class %s : Precision : %.3f, Recall : %.3f, F1 : %.3f" % (label2emotion[c], precision, recall, f1))
macroPrecision /= 3
macroRecall /= 3
macroF1 = (2 * macroRecall * macroPrecision ) / (macroPrecision + macroRecall) if (macroPrecision+macroRecall) > 0 else 0
print("Ignoring the Others class, Macro Precision : %.4f, Macro Recall : %.4f, Macro F1 : %.4f" % (macroPrecision, macroRecall, macroF1))
# ------------- Micro level calculation ---------------
truePositives = truePositives[1:].sum()
falsePositives = falsePositives[1:].sum()
falseNegatives = falseNegatives[1:].sum()
print("Ignoring the Others class, Micro TP : %d, FP : %d, FN : %d" % (truePositives, falsePositives, falseNegatives))
microPrecision = truePositives / (truePositives + falsePositives)
microRecall = truePositives / (truePositives + falseNegatives)
microF1 = ( 2 * microRecall * microPrecision ) / (microPrecision + microRecall) if (microPrecision+microRecall) > 0 else 0
# -----------------------------------------------------
predictions = predictions.argmax(axis=1)
ground = ground.argmax(axis=1)
accuracy = np.mean(predictions==ground)
print("Accuracy : %.4f, Micro Precision : %.4f, Micro Recall : %.4f, Micro F1 : %.4f" % (accuracy, microPrecision, microRecall, microF1))
return accuracy, microPrecision, microRecall, microF1
def writeNormalisedData(dataFilePath, texts):
"""Write normalised data to a file
Input:
dataFilePath : Path to original train/test file that has been processed
texts : List containing the normalised 3 turn conversations, separated by the <eos> tag.
"""
normalisedDataFilePath = dataFilePath.replace(".txt", "_normalised.txt")
with io.open(normalisedDataFilePath, 'w', encoding='utf8') as fout:
with io.open(dataFilePath, encoding='utf8') as fin:
fin.readline()
for lineNum, line in enumerate(fin):
line = line.strip().split('\t')
normalisedLine = texts[lineNum].strip().split('<eos>')
fout.write(line[0] + '\t')
# Write the original turn, followed by the normalised version of the same turn
fout.write(line[1] + '\t' + normalisedLine[0] + '\t')
fout.write(line[2] + '\t' + normalisedLine[1] + '\t')
fout.write(line[3] + '\t' + normalisedLine[2] + '\t')
try:
# If label information available (train time)
fout.write(line[4] + '\n')
except:
# If label information not available (test time)
fout.write('\n')
def cleanWord (word):
final_list = []
temp_word = ""
for k in range(len(word)):
if word[k] in UNICODE_EMOJI:
# check if word[k] is an emoji
if (word[k] in emoji2emoticons):
final_list.append(emoji2emoticons[word[k]])
else:
temp_word = temp_word + word[k]
# if len(temp_word) > 0:
# # "righttttttt" -> "right"
# temp_word = re.sub(r'(.)\1+', r'\1\1', temp_word)
# # correct spelling
# spell = SpellChecker()
# temp_word = spell.correction(temp_word)
# # lemmatize
# temp_word = WordNetLemmatizer().lemmatize(temp_word)
# final_list.append(temp_word)
return final_list
def getEmbeddingMatrix(wordIndex):
"""Populate an embedding matrix using a word-index. If the word "happy" has an index 19,
the 19th row in the embedding matrix should contain the embedding vector for the word "happy".
Input:
wordIndex : A dictionary of (word : index) pairs, extracted using a tokeniser
Output:
embeddingMatrix : A matrix where every row has 100 dimensional GloVe embedding
"""
embeddingsIndex = {}
# Load the embedding vectors from ther GloVe file
with io.open(os.path.join(gloveDir, 'datastories.twitter.300d.txt'), encoding="utf8") as f:
for line in f:
values = line.split()
word = values[0]
embeddingVector = np.asarray(values[1:], dtype='float32')
embeddingsIndex[word] = embeddingVector
print('Found %s word vectors.' % len(embeddingsIndex))
bad_words_glove_1 = set([])
bad_words_glove_2 = set([])
counter = 0
# Minimum word index of any word is 1.
embeddingMatrix = np.zeros((len(wordIndex) + 1, EMBEDDING_DIM))
for word, i in wordIndex.items():
embeddingVector = embeddingsIndex.get(word)
if embeddingVector is not None:
# words not found in embedding index will be all-zeros.
embeddingMatrix[i] = embeddingVector
else:
bad_words_glove_1.add(word)
good_from_bad = cleanWord(word)
# sum all word vectors, obtained after clean_word
temp_embedding_vector = np.zeros((1, EMBEDDING_DIM))
for www in good_from_bad:
eee = embeddingsIndex.get(www)
if eee is not None:
temp_embedding_vector = temp_embedding_vector + eee
if not temp_embedding_vector.all():
bad_words_glove_2.add(word)
embeddingMatrix[i] = temp_embedding_vector
if (counter % 1000 == 0):
print(counter)
counter += 1
# print("Bad words in GloVe 1 - %d" % len(bad_words_glove_1))
# print("Bad words in GloVe 2 - %d" % len(bad_words_glove_2))
return embeddingMatrix
def getEmbeddingByBin(wordIndex):
model = fasttext.load_model("250D_322M_tweets.bin")
embeddingMatrix = np.zeros((len(wordIndex) + 1, EMBEDDING_DIM))
for word, i in wordIndex.items():
v = model.get_word_vector(word)
embeddingMatrix[i] = v
return embeddingMatrix
def buildModel(embeddingMatrix):
"""Constructs the architecture of the model
Input:
embeddingMatrix : The embedding matrix to be loaded in the embedding layer.
Output:
model : A basic LSTM model
"""
embeddingLayer = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
# embeddingLayerEmoji = Embedding(embeddingMatrixEmoji.shape[0],
# EMBEDDING_DIM,
# weights=[embeddingMatrix],
# input_length=MAX_SEQUENCE_LENGTH,
# trainable=False)
#inp = Concatenate(axis=-1)([embeddingLayer,embeddingLayerEmoji])
model = Sequential()
# modelEmoji = Sequential()
model.add(embeddingLayer)
# modelEmoji.add(embeddingLayerEmoji)
# modelFinal = Concatenate(axis=-1)([model, modelEmoji])
#model.add(inp)
# model.add(LSTM(LSTM_DIM, dropout=DROPOUT,return_sequences=True))
model.add(LSTM(LSTM_DIM, dropout=DROPOUT))
model.add(Dense(NUM_CLASSES, activation='sigmoid'))
rmsprop = optimizers.rmsprop(lr=LEARNING_RATE)
model.compile(loss='categorical_crossentropy',
optimizer=rmsprop,
metrics=['acc'])
return model
def build3CnnModel(embeddingMatrix):
"""Constructs the architecture of the model
Input:
embeddingMatrix : The embedding matrix to be loaded in the embedding layer.
Output:
model : A basic LSTM model
"""
embeddingLayer = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
# model = Sequential()
# model.add(embeddingLayer)
# model.add(LSTM(LSTM_DIM, dropout=DROPOUT))
# model.add(Dense(NUM_CLASSES, activation='sigmoid'))
#
# rmsprop = optimizers.rmsprop(lr=LEARNING_RATE)
# model.compile(loss='categorical_crossentropy',
# optimizer=rmsprop,
# metrics=['acc'])
########################################################
print('Training model.')
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
embedded_sequences = embeddingLayer(sequence_input)
# add first conv filter
embedded_sequences = Reshape((MAX_SEQUENCE_LENGTH, EMBEDDING_DIM, 1))(embedded_sequences)
x = Conv2D(100, (5, EMBEDDING_DIM), activation='relu')(embedded_sequences)
x = MaxPooling2D((MAX_SEQUENCE_LENGTH - 5 + 1, 1))(x)
# add second conv filter.
y = Conv2D(100, (4, EMBEDDING_DIM), activation='relu')(embedded_sequences)
y = MaxPooling2D((MAX_SEQUENCE_LENGTH - 4 + 1, 1))(y)
# add third conv filter.
z = Conv2D(100, (3, EMBEDDING_DIM), activation='relu')(embedded_sequences)
z = MaxPooling2D((MAX_SEQUENCE_LENGTH - 3 + 1, 1))(z)
# concate the conv layers
alpha = concatenate([x, y, z])
# flatted the pooled features.
alpha = Flatten()(alpha)
# dropout
alpha = Dropout(0.5)(alpha)
# predictions
preds = Dense(NUM_CLASSES, activation='softmax')(alpha)
# build model
model = Model(sequence_input, preds)
adadelta = optimizers.Adadelta()
model.compile(loss='categorical_crossentropy',
optimizer=adadelta,
metrics=['acc'])
return model
def build3Cnn_LstmModel(embeddingMatrix):
"""Constructs the architecture of the model
Input:
embeddingMatrix : The embedding matrix to be loaded in the embedding layer.
Output:
model : A basic LSTM model
"""
embeddingLayer = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
print('Training model.')
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
embedded_sequences = embeddingLayer(sequence_input)
# add first conv filter
embedded_sequences = Reshape((MAX_SEQUENCE_LENGTH, EMBEDDING_DIM, 1))(embedded_sequences)
x = Conv2D(100, (5, EMBEDDING_DIM), activation='relu')(embedded_sequences)
x = MaxPooling2D((MAX_SEQUENCE_LENGTH - 5 + 1, 1))(x)
x = LSTM(50, dropout=0.2)(x)
# add second conv filter.
y = Conv2D(100, (4, EMBEDDING_DIM), activation='relu')(embedded_sequences)
y = MaxPooling2D((MAX_SEQUENCE_LENGTH - 4 + 1, 1))(y)
y = LSTM(50, dropout=0.2)(y)
# add third conv filter.
z = Conv2D(100, (3, EMBEDDING_DIM), activation='relu')(embedded_sequences)
z = MaxPooling2D((MAX_SEQUENCE_LENGTH - 3 + 1, 1))(z)
z = LSTM(50, dropout=0.2)(z)
# concate the conv layers
alpha = concatenate([x, y, z])
# flatted the pooled features.
alpha = Flatten()(alpha)
# dropout
alpha = Dropout(0.5)(alpha)
# predictions
preds = Dense(NUM_CLASSES, activation='softmax')(alpha)
# build model
model = Model(sequence_input, preds)
adadelta = optimizers.Adadelta()
model.compile(loss='categorical_crossentropy',
optimizer=adadelta,
metrics=['acc'])
return model
def build3LSTMModel(embeddingMatrix):
"""Constructs the architecture of the model
Input:
embeddingMatrix : The embedding matrix to be loaded in the embedding layer.
Output:
model : A basic LSTM model
"""
embeddingLayer = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
model = Sequential()
model.add(embeddingLayer)
model.add(LSTM(LSTM_DIM, dropout=0.2, return_sequences=True))
model.add(LSTM(LSTM_DIM, dropout=0.2, return_sequences=True))
model.add(LSTM(LSTM_DIM, dropout=0.2))
model.add(Dense(NUM_CLASSES, activation='sigmoid'))
model.add(Activation('relu'))
model.add(Activation('linear'))
rmsprop = optimizers.rmsprop(lr=LEARNING_RATE)
model.compile(loss='categorical_crossentropy',
optimizer=rmsprop,
metrics=['acc'])
return model
def build_CNNLSTM_Model(embeddingMatrix):
"""Constructs the architecture of the model
Input:
embeddingMatrix : The embedding matrix to be loaded in the embedding layer.
Output:
model : A basic CNN-LSTM model
"""
# Convolution parameters
filter_length = 3
nb_filter = 256
pool_length = 2
cnn_activation = 'relu'
border_mode = 'same'
# RNN parameters
output_size = 50
rnn_activation = 'tanh'
recurrent_activation = 'hard_sigmoid'
# Compile parameters
loss = 'binary_crossentropy'
optimizer = 'rmsprop'
def swish(x):
beta = 1.5 # 1, 1.5 or 2
return beta * x * keras.backend.sigmoid(x)
embeddingLayer = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
model = Sequential()
model.add(embeddingLayer)
model.add(Dropout(0.2))
model.add(Convolution1D(nb_filter=nb_filter,
filter_length=filter_length,
border_mode=border_mode,
activation=cnn_activation,
subsample_length=1))
model.add(MaxPooling1D(pool_length=pool_length))
# model.add(Dropout(0.2))
# model.add(SeqSelfAttention(attention_activation='sigmoid'))
model.add(Bidirectional(LSTM(dropout=0.2,output_dim=output_size)))
# model.add(Bidirectional(LSTM(dropout=0.2, output_dim=output_size, activation=rnn_activation,recurrent_activation=recurrent_activation)))
# model.add(Flatten(Reshape((1, filters,))))
# model.add(Flatten())
model.add(Dense(NUM_CLASSES, activation ="sigmoid"))
model.add(Dropout(0.25))
sgd = optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(loss=loss,
optimizer=optimizer,
metrics=['accuracy'])
print(model.summary())
return model
#working on
def build_CNNLSTM_Model_Concat(embeddingMatrix):
# Convolution parameters
filter_length = 3
nb_filter = 256
pool_length = 2
cnn_activation = 'relu'
border_mode = 'same'
# RNN parameters
output_size = 50
rnn_activation = 'tanh'
recurrent_activation = 'hard_sigmoid'
# Compile parameters
loss = 'binary_crossentropy'
optimizer = 'rmsprop'
x1 = Input(shape=(MAX_SEQUENCE_LENGTH,), sparse=False, dtype='int32', name='main_input1')
e0 = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
emb1 = e0(x1)
d=Dropout(0.2)
o=d(emb1)
c0 = Convolution1D(nb_filter=nb_filter,
filter_length=filter_length,
border_mode=border_mode,
activation=cnn_activation,
subsample_length=1)(o)
c1 = Convolution1D(nb_filter=nb_filter,
filter_length=5,
border_mode=border_mode,
activation=cnn_activation,
subsample_length=1)(o)
# c2 = Convolution1D(nb_filter=nb_filter,
# filter_length=7,
# border_mode=border_mode,
# activation=cnn_activation,
# subsample_length=1)(o)
c=concatenate([c0,c1])
p0=MaxPooling1D(pool_length=pool_length)(c)
# p0 = MaxPooling1D(pool_length=pool_length)(p0)
# p0=Reshape((1))(p0
# emb1=Flatten()(emb1)
# p0=Flatten()(p0)
# print(p0.get_shape())
# p0 = concatenate([emb1, p0])
# p0=Reshape((1,))(p0)
lstm = Bidirectional(LSTM(dropout=0.2,output_dim=output_size))
out = lstm(p0)
opt = Dense(NUM_CLASSES, activation='sigmoid')(out)
dropout=Dropout(0.25)(opt)
model = Model([x1], dropout)
model.compile(loss=loss, optimizer=optimizer, metrics=['accuracy'])
return model
def build3LSTMModel(embeddingMatrix):
"""Constructs the architecture of the model
Input:
embeddingMatrix : The embedding matrix to be loaded in the embedding layer.
Output:
model : A basic LSTM model
"""
embeddingLayer = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
model = Sequential()
model.add(embeddingLayer)
model.add(LSTM(LSTM_DIM, dropout=0.2, return_sequences=True))
model.add(LSTM(LSTM_DIM, dropout=0.2, return_sequences=True))
model.add(LSTM(LSTM_DIM, dropout=0.2))
model.add(Dense(NUM_CLASSES, activation='sigmoid'))
model.add(Activation('relu'))
model.add(Activation('linear'))
rmsprop = optimizers.rmsprop(lr=LEARNING_RATE)
model.compile(loss='categorical_crossentropy',
optimizer=rmsprop,
metrics=['acc'])
return model
def model_cnn(embedding_matrix):
filter_sizes = [1, 2, 3, 5]
num_filters = 36
inp = Input(shape=(MAX_SEQUENCE_LENGTH,))
x = Embedding(embedding_matrix.shape[0],EMBEDDING_DIM, weights=[embedding_matrix])(inp)
x = Reshape((MAX_SEQUENCE_LENGTH, EMBEDDING_DIM, 1))(x)
maxpool_pool = []
for i in range(len(filter_sizes)):
conv = Conv2D(num_filters, kernel_size=(filter_sizes[i], EMBEDDING_DIM),
kernel_initializer='he_normal', activation='elu')(x)
maxpool_pool.append(MaxPool2D(pool_size=(MAX_SEQUENCE_LENGTH - filter_sizes[i] + 1, 1))(conv))
z = Concatenate(axis=1)(maxpool_pool)
z = Flatten()(z)
z=LSTM(LSTM_DIM,dropout=0.2)
# z = Dropout(0.1)(z)
outp = Dense(NUM_CLASSES, activation="sigmoid")(z)
outp=Dropout(0.25)(outp)
model = Model(inputs=inp, outputs=outp)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
def build_CNNLSTM_Model_With_Different_FilterSize(embeddingMatrix):
# Convolution parameters
filter_length = [1, 2, 3, 5]
nb_filter = 256
pool_length = 2
cnn_activation = 'relu'
border_mode = 'same'
# RNN parameters
output_size = 50
rnn_activation = 'tanh'
recurrent_activation = 'hard_sigmoid'
# Compile parameters
loss = 'binary_crossentropy'
optimizer = 'rmsprop'
x1 = Input(shape=(MAX_SEQUENCE_LENGTH,), sparse=False, dtype='int32', name='main_input1')
e0 = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
emb1 = e0(x1)
d=Dropout(0.2)
o=d(emb1)
maxpool_pool = []
for i in range(len(filter_length)):
conv = Conv2D(nb_filter, kernel_size=(filter_length[i], EMBEDDING_DIM),
kernel_initializer='he_normal', activation='elu')(o)
maxpool_pool.append(MaxPool2D(pool_size=(MAX_SEQUENCE_LENGTH - filter_length[i] + 1, 1))(conv))
z = concatenate(maxpool_pool)
# z = Flatten()(z)
z = Dropout(0.1)(z)
# c0 = Convolution1D(nb_filter=nb_filter,
# filter_length=filter_length,
# border_mode=border_mode,
# activation=cnn_activation,
# subsample_length=1)(o)
# p0=MaxPooling1D(pool_length=pool_length)(c0)
lstm = Bidirectional(LSTM(dropout=0.2,output_dim=output_size))
out = lstm(z)
opt = Dense(NUM_CLASSES, activation='sigmoid')(out)
dropout=Dropout(0.25)(opt)
model = Model([x1], dropout)
model.compile(loss=loss, optimizer=optimizer, metrics=['accuracy'])
return model
#workin on end
def build_CNNGRU_Model(embeddingMatrix):
"""Constructs the architecture of the model
Input:
embeddingMatrix : The embedding matrix to be loaded in the embedding layer.
Output:
model : A basic CNN-GRU model
"""
# Convolution parameters
filter_length = 3
nb_filter = 150
pool_length = 2
cnn_activation = 'relu'
border_mode = 'same'
# RNN parameters
output_size = 50
rnn_activation = 'tanh'
recurrent_activation = 'hard_sigmoid'
# Compile parameters
loss = 'binary_crossentropy'
optimizer = 'rmsprop'
embeddingLayer = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
model = Sequential()
model.add(embeddingLayer)
model.add(Dropout(0.5))
model.add(Convolution1D(nb_filter=nb_filter,
filter_length=filter_length,
border_mode=border_mode,
activation=cnn_activation,
subsample_length=1))
model.add(MaxPooling1D(pool_length=pool_length))
model.add(GRU(output_dim=output_size, activation=rnn_activation, recurrent_activation=recurrent_activation))
model.add(Dropout(0.25))
model.add(Dense(NUM_CLASSES))
model.add(Activation('sigmoid'))
model.compile(loss=loss,
optimizer=optimizer,
metrics=['accuracy'])
print('CNN-GRU')
# model = Sequential()
# model.add(embeddingLayer)
# model.add(GRU(output_dim=output_size, activation=rnn_activation, recurrent_activation=recurrent_activation))
# model.add(Dropout(0.25))
# model.add(Dense(1))
# model.add(Activation('sigmoid'))
#
# model.compile(loss=loss,
# optimizer=optimizer,
# metrics=['accuracy'])
return model
def build_LSTMCNN_Model_New(embeddingMatrix):
filters = 250
kernel_size = 3
pooling = 'max'
dropout = None
hidden_dims = None
lstm_units = 1800
x1 = Input(shape=(MAX_SEQUENCE_LENGTH,), sparse=False, dtype='int32', name='main_input1')
e0 = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
emb1 = e0(x1)
# lstm0=LSTM(LSTM_DIM, dropout=DROPOUT,return_sequences=True)
# out0=lstm0(emb1)
lstm = Bidirectional(LSTM(lstm_units, return_sequences=True))
out = lstm(emb1)
d=Dropout(0.25)(out)
c0 = Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1)(d)
p0 = {'max': GlobalMaxPooling1D()(c0), 'avg': GlobalAveragePooling1D()(c0)}.get(pooling, c0)
opt = Dense(NUM_CLASSES, activation='softmax')(p0)
# final=Dense(NUM_CLASSES, activation='softmax')(opt)
model = Model([x1], opt)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
def build_LSTMCNN_Model(embeddingMatrix):
"""Constructs the architecture of the model
Input:
embeddingMatrix : The embedding matrix to be loaded in the embedding layer.
Output:
model : A basic CNN-LSTM model
Compiles the LSTM CNN model
:return: compiled classifier
"""
filters = 250
kernel_size = 3
pooling = 'max'
dropout = None
hidden_dims = None
lstm_units = 1800
x1 = Input(shape=(MAX_SEQUENCE_LENGTH,), sparse=False, dtype='int32', name='main_input1')
# x2 = Input(shape=(MAX_SEQUENCE_LENGTH,), sparse=False, dtype='int32', name='main_input2')
# x3 = Input(shape=(MAX_SEQUENCE_LENGTH,), sparse=False, dtype='int32', name='main_input3')
e0 = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
emb1 = e0(x1)
# emb2 = e0(x2)
# emb3 = e0(x3)
# lstm = LSTM(lstm_units, return_sequences=True)
# lstm = Bidirectional(LSTM(lstm_units, dropout=DROPOUT))
# lstm1 = lstm(emb1)
# lstm2 = lstm(emb2)
# lstm3 = lstm(emb3)
# inp = Concatenate(axis=-1)([emb1, emb2, emb3])
# print(inp.shape)
# inp = Reshape((1,lstm_units,))(inp)
lstm = LSTM(lstm_units, return_sequences=True)
# lstm_up = LSTM(lstm_units, dropout=DROPOUT)
out = lstm(emb1)
# ipt = Input(shape=(MAX_SEQUENCE_LENGTH,), sparse=False, dtype='int32')
# opt = ipt
#
# opt = e0(opt)
# opt = lstm(opt)
p1 = []
# # Convolution1D Layer
for ks in range(1, kernel_size + 1):
# Convolution1D Layer
c0 = Conv1D(filters, ks, padding='valid', activation='relu', strides=1)(out)
# Pooling layer:
p0 = {'max': GlobalMaxPooling1D()(c0), 'avg': GlobalAveragePooling1D()(c0)}.get(pooling, c0)
p1.append(p0)
if len(p1) > 1:
# opt = keras.concatenate(p1, axis=1)
opt = Concatenate(axis=1)(p1)
else:
opt = p1[0]
# Dropout layers
if dropout is not None:
d0 = Dropout(dropout)
opt = d0(opt)
# Output layer with sigmoid activation:
opt = Dense(NUM_CLASSES, activation='softmax')(opt)
# model = Model(inputs=ipt, outputs=opt)
model = Model([x1], opt)
# Compile
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
def build_GRU_Model(embeddingMatrix):
"""Constructs the architecture of the model
Input:
embeddingMatrix : The embedding matrix to be loaded in the embedding layer.
Output:
model : A basic CNN-GRU model
"""
# Convolution parameters
filter_length = 3
nb_filter = 150
pool_length = 2
cnn_activation = 'relu'
border_mode = 'same'
# RNN parameters
output_size = 50
rnn_activation = 'tanh'
recurrent_activation = 'hard_sigmoid'
# Compile parameters
loss = 'binary_crossentropy'
optimizer = 'rmsprop'
embeddingLayer = Embedding(embeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[embeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
model = Sequential()
model.add(embeddingLayer)
model.add(Dropout(0.5))
model.add(Convolution1D(nb_filter=nb_filter,
filter_length=filter_length,
border_mode=border_mode,
activation=cnn_activation,
subsample_length=1))
model.add(MaxPooling1D(pool_length=pool_length))
model.add(GRU(output_dim=output_size, activation=rnn_activation, recurrent_activation=recurrent_activation))
model.add(Dropout(0.25))
model.add(Dense(NUM_CLASSES))
model.add(Activation('sigmoid'))
model.compile(loss=loss,
optimizer=optimizer,
metrics=['accuracy'])
print('CNN-GRU')
# model = Sequential()
# model.add(embeddingLayer)
# model.add(GRU(output_dim=output_size, activation=rnn_activation, recurrent_activation=recurrent_activation))
# model.add(Dropout(0.25))
# model.add(Dense(1))
# model.add(Activation('sigmoid'))
#
# model.compile(loss=loss,
# optimizer=optimizer,
# metrics=['accuracy'])
return model
def build_BiLSTMCNNwithSelfAttention_Model(wordEmbeddingMatrix):
filters = 250
pooling = 'max'
inputs = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32', name='main_input1')
embd = Embedding(wordEmbeddingMatrix.shape[0],
EMBEDDING_DIM,
weights=[wordEmbeddingMatrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
emb = embd(inputs)