-
Notifications
You must be signed in to change notification settings - Fork 15
/
model.py
53 lines (39 loc) · 1.52 KB
/
model.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
"""
Keras Model
"""
from keras.models import Sequential
from keras.layers import Embedding, LSTM, TimeDistributed, Dense, Dropout
from keras.layers.wrappers import Bidirectional
from keras.optimizers import Adam
import constant
class Model(object):
def __init__(self, hyper_params):
# Sequential model
model = Sequential()
# Embedding layer
model.add(Embedding(constant.NUM_CHARS, 5,
input_length=hyper_params.num_step))
# LSTM Layer #1
lstm = LSTM(256, return_sequences=True, unroll=True,
dropout=0.1, recurrent_dropout=0.1)
model.add(Bidirectional(lstm))
model.add(Dropout(0.1))
# LSTM Layer #2
lstm = LSTM(256, return_sequences=True, unroll=True,
dropout=0.1, recurrent_dropout=0.1)
model.add(Bidirectional(lstm))
model.add(Dropout(0.1))
# LSTM Layer #3
lstm = LSTM(128, return_sequences=True, unroll=True,
dropout=0.25, recurrent_dropout=0.25)
model.add(Bidirectional(lstm))
model.add(Dropout(0.25))
# RNN
model.add(TimeDistributed(Dense(constant.NUM_TAGS, activation="softmax"),
input_shape=(hyper_params.num_step, 128)))
# Optimizer
optimizer = Adam(hyper_params.learning_rate)
# Compile
model.compile(loss="categorical_crossentropy", optimizer=optimizer,
metrics=["categorical_accuracy"])
self.model = model