-
Notifications
You must be signed in to change notification settings - Fork 13
/
MyLSTM.py
76 lines (67 loc) · 3.85 KB
/
MyLSTM.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
from keras.layers import Dense, Input, Flatten, Dropout, Embedding, LSTM, Merge, merge
from keras.models import Model, Sequential
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils.np_utils import to_categorical
import keras.backend as K
class MyLSTM(object):
def __init__(self, embedding_matrix, word_index, MAX_SENT_SEQUENCE_LENGTH, add_features_dim):
self.embedding_matrix = embedding_matrix
self.EMBEDDING_DIM = embedding_matrix.shape[1]
self.word_index = word_index
self.MAX_SENT_SEQUENCE_LENGTH = MAX_SENT_SEQUENCE_LENGTH
self.add_features_dim = add_features_dim
def _gen_embedding_layer(self):
embedding_layer = Embedding(len(self.word_index) + 1,
self.EMBEDDING_DIM,
weights=[self.embedding_matrix],
input_length=self.MAX_SENT_SEQUENCE_LENGTH,
trainable=False)
sequence_input = Input(shape=(self.MAX_SENT_SEQUENCE_LENGTH,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
return sequence_input, embedded_sequences
def init_model(self, lstm_output_dim=64, denses=[], dropouts=[]):
ques_input, ques_embedded = self._gen_embedding_layer()
sent_input, sent_embedded = self._gen_embedding_layer()
shared_lstm = LSTM(lstm_output_dim,dropout_U=0)
ques_encoded = shared_lstm(ques_embedded)
sent_encoded = shared_lstm(sent_embedded)
merged_layer_list = [ques_encoded, sent_encoded]
merged_input = [ques_input, sent_input]
if self.add_features_dim != None:
add_features_input = Input(shape=(self.add_features_dim,))
merged_layer_list.append(add_features_input)
merged_input.append(add_features_input)
merged_vector = merge(merged_layer_list, mode='concat', concat_axis=-1, name='lstm_vec')
for i, d in enumerate(denses):
merged_vector = Dense(d,activation='sigmoid')(merged_vector)
merged_vector = Dropout(dropouts[i])(merged_vector)
predictions = Dense(1, activation='sigmoid')(merged_vector)
self.model = Model(input=merged_input, output=predictions)
self.model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
def init_model_v2(self, lstm_output_dim=64, denses=[], dropouts=[]):
ques_input, ques_embedded = self._gen_embedding_layer()
sent_input, sent_embedded = self._gen_embedding_layer()
shared_lstm = LSTM(lstm_output_dim,dropout_U=0)
ques_encoded = shared_lstm(ques_embedded)
sent_encoded = shared_lstm(sent_embedded)
merged_layer_list = [ques_encoded, sent_encoded]
merged_input = [ques_input, sent_input]
merged_vector = merge(merged_layer_list, mode='concat', concat_axis=-1, name='lstm_vec')
for i, d in enumerate(denses):
merged_vector = Dense(d,activation='sigmoid')(merged_vector)
merged_vector = Dropout(dropouts[i])(merged_vector)
if self.add_features_dim != None:
add_features_input = Input(shape=(self.add_features_dim,))
merged_vector = merge([merged_vector, add_features_input], mode='concat', concat_axis=-1, name='vec')
merged_input.append(add_features_input)
predictions = Dense(1, activation='sigmoid')(merged_vector)
self.model = Model(input=merged_input, output=predictions)
self.model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
def fit(self, X, Y, *args, **kwargs):
self.model.fit(X, Y, *args, **kwargs)
def predict(self, X):
self.preds = self.model.predict(X)
return self.preds
def save(self, fn):
self.model.save(fn)