-
Notifications
You must be signed in to change notification settings - Fork 17
/
models.py
67 lines (50 loc) · 2.49 KB
/
models.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
#!/usr/bin/env python3
from keras.layers import Dense, LSTM, Bidirectional, Merge
from keras.layers.embeddings import Embedding
from keras.models import Sequential
def get_LSTM(input_dim, output_dim, max_lenght, no_activities):
model = Sequential(name='LSTM')
model.add(Embedding(input_dim, output_dim, input_length=max_lenght, mask_zero=True))
model.add(LSTM(output_dim))
model.add(Dense(no_activities, activation='softmax'))
return model
def get_biLSTM(input_dim, output_dim, max_lenght, no_activities):
model = Sequential(name='biLSTM')
model.add(Embedding(input_dim, output_dim, input_length=max_lenght, mask_zero=True))
model.add(Bidirectional(LSTM(output_dim)))
model.add(Dense(no_activities, activation='softmax'))
return model
def get_Ensemble2LSTM(input_dim, output_dim, max_lenght, no_activities):
model1 = Sequential()
model1.add(Embedding(input_dim, output_dim, input_length=max_lenght, mask_zero=True))
model1.add(Bidirectional(LSTM(output_dim)))
model2 = Sequential()
model2.add(Embedding(input_dim, output_dim, input_length=max_lenght, mask_zero=True))
model2.add(LSTM(output_dim))
model = Sequential(name='Ensemble2LSTM')
model.add(Merge([model1, model2], mode='concat'))
model.add(Dense(no_activities, activation='softmax'))
return model
def get_CascadeEnsembleLSTM(input_dim, output_dim, max_lenght, no_activities):
model1 = Sequential()
model1.add(Embedding(input_dim, output_dim, input_length=max_lenght, mask_zero=True))
model1.add(Bidirectional(LSTM(output_dim, return_sequences=True)))
model2 = Sequential()
model2.add(Embedding(input_dim, output_dim, input_length=max_lenght, mask_zero=True))
model2.add(LSTM(output_dim, return_sequences=True))
model = Sequential(name='CascadeEnsembleLSTM')
model.add(Merge([model1, model2], mode='concat'))
model.add(LSTM(output_dim))
model.add(Dense(no_activities, activation='softmax'))
return model
def get_CascadeLSTM(input_dim, output_dim, max_lenght, no_activities):
model = Sequential(name='CascadeLSTM')
model.add(Embedding(input_dim, output_dim, input_length=max_lenght, mask_zero=True))
model.add(Bidirectional(LSTM(output_dim, return_sequences=True)))
model.add(LSTM(output_dim))
model.add(Dense(no_activities, activation='softmax'))
return model
def compileModel(model):
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
return model