-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_model.py
More file actions
49 lines (35 loc) · 1.69 KB
/
Copy pathbuild_model.py
File metadata and controls
49 lines (35 loc) · 1.69 KB
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
from keras.models import Model
from keras.layers import Embedding, Dense, Input, Conv1D, GlobalMaxPool1D, Dropout, GlobalAvgPool1D, concatenate, SpatialDropout1D
'''
This script contains a function for building and compiling a model.
'''
def build_model(hparams):
print('Building model...')
input_ = Input(shape=(hparams['max_length'],))
x = Embedding(input_dim=hparams['max_words'], output_dim=96)(input_)
x = Dropout(0.1)(x)
x_1 = Conv1D(filters=256, kernel_size=7, strides=1, activation='elu')(x)
x_1 = SpatialDropout1D(0.01)(x_1)
max_1 = GlobalMaxPool1D()(x_1)
x_2 = Conv1D(filters=256, kernel_size=4, strides=1, activation='elu')(x)
x_2 = SpatialDropout1D(0.01)(x_2)
max_2 = GlobalMaxPool1D()(x_2)
x_3 = Conv1D(filters=256, kernel_size=1, strides=1, activation='elu')(x)
x_3 = SpatialDropout1D(0.01)(x_3)
max_3 = GlobalMaxPool1D()(x_3)
x_4 = Conv1D(filters=256, kernel_size=3, strides=1, activation='elu')(x)
x_4 = SpatialDropout1D(0.01)(x_4)
max_4 = GlobalMaxPool1D()(x_4)
x_5 = Conv1D(filters=256, kernel_size=2, strides=1, activation='elu')(x)
x_5 = SpatialDropout1D(0.01)(x_5)
max_5 = GlobalMaxPool1D()(x_5)
x_6 = Conv1D(filters=256, kernel_size=6, strides=1, activation='elu')(x)
x_6 = SpatialDropout1D(0.01)(x_6)
max_6 = GlobalMaxPool1D()(x_6)
c = concatenate([max_1, max_2, max_3, max_4, max_5, max_6])
output = Dense(hparams['n_classes'], activation='sigmoid')(c)
model = Model(inputs=[input_], outputs=[output])
model.compile(optimizer=hparams['optimizer'], loss='binary_crossentropy', metrics=['acc'])
print('Parameters:', model.count_params())
# print(model.summary())
return model