-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdeep_models.py
421 lines (385 loc) · 15.5 KB
/
deep_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
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
from keras.models import Sequential, load_model
from keras.layers import Dense, Activation, Dropout
from keras.layers import LSTM, Bidirectional
# Keras LSTM text generation example model (simplest model)
# summery of result for model_0 (Not deep model):
#
#
def model_0(input_dim, output_dim):
"""
Total params: 127,584
Trainable params: 127,584
Non-trainable params: 0
:param input_dim:
:param output_dim:
:return:
"""
# build the model: a single LSTM
print('Build model...')
model = Sequential()
model.add(LSTM(128, input_shape=input_dim))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_0'
# summery of result for model_1 (deep 2):
#
#
def model_1(input_dim, output_dim):
"""
Total params: 259,168
Trainable params: 259,168
Non-trainable params: 0
:param input_dim:
:param output_dim:
:return:
"""
model = Sequential()
# model.add(LSTM(128, input_shape=(maxlen, len(chars))))
model.add(LSTM(128, input_shape=input_dim, return_sequences=True))
# model.add(LSTM(128, input_shape=(maxlen, len(chars)), activation='relu', return_sequences=True, dropout=0.2))
model.add(LSTM(128, input_shape=input_dim))
# model.add(LSTM(128, activation='relu', dropout=0.2))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_1'
# Summery of result for model_2 (deep 2):
# Test done!
# Bad model
# Not good:-(. The model loss stop on 1.88 after 18 epoch run on cpu (deepubuntu)
# <><><><><> Model compile config: <><><><><><>
# optimizer = RMSprop(lr=0.01) # [0.01, 0.02, 0.05, 0.1]
# model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
def model_2(input_dim, output_dim):
"""
Total params: 259,168
Trainable params: 259,168
Non-trainable params: 0
:param input_dim:
:param output_dim:
:return:
"""
model = Sequential()
# model.add(LSTM(128, input_shape=(maxlen, len(chars))))
model.add(LSTM(128, input_shape=input_dim, return_sequences=True, dropout=0.2, recurrent_dropout=0.1))
model.add(LSTM(128, input_shape=input_dim, return_sequences=False, dropout=0.2, recurrent_dropout=0.1))
# model.add(LSTM(128, activation='relu', dropout=0.2))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_2'
# Summery of result for this model:
def model_3(input_dim, output_dim):
"""
Total params: 911,456
Trainable params: 911,456
Non-trainable params: 0
:param input_dim:
:param output_dim:
:return:
"""
model = Sequential()
# model.add(LSTM(128, input_shape=(maxlen, len(chars))))
model.add(LSTM(256, input_shape=input_dim, return_sequences=True, dropout=0.4, recurrent_dropout=0.2))
model.add(LSTM(256, input_shape=input_dim, return_sequences=False, dropout=0.4, recurrent_dropout=0.2))
# model.add(LSTM(128, activation='relu', dropout=0.2))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_3'
# Summery of result for this model:
# Try 1:
# * When learning rate is 0.01 and batch select sequentially the loss stuck on 2.3454 after about 6 epochs.
# This is very bad -:(
#
# Try 2:
# So we changed lr and also batch selection:
# Result for: lr=0.001, batch select randomly! (Shuffle), step=3, batch_size=128, maxlen=50
# * Pretty good with above config on small dataset
#
# Try 3:
# Train on large dataset (prefix choose from the train-set not test-set) on generation phase:
# Bad result after 6 epoch (the loss decrease suddenly.
#
# Try 4:
# bach_size=256, lr=0.001, step=1, maxlen=50
#
# Totally not good model
def model_4(input_dim, output_dim):
"""
Total corpus length: 11,530,647
Total corpus chars: 96
Building dictionary index ...
Get model summary ...
model_4 summary ...
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (None, 85, 256) 361472
_________________________________________________________________
dropout_1 (Dropout) (None, 85, 256) 0
_________________________________________________________________
lstm_2 (LSTM) (None, 256) 525312
_________________________________________________________________
dropout_2 (Dropout) (None, 256) 0
_________________________________________________________________
dense_1 (Dense) (None, 96) 24672
_________________________________________________________________
activation_1 (Activation) (None, 96) 0
=================================================================
Total params: 911,456
Trainable params: 911,456
Non-trainable params: 0
_________________________________________________________________
model_4 count_params ...
911456
:param input_dim:
:param output_dim:
:return:
"""
model = Sequential()
model.add(LSTM(256, input_shape=input_dim, return_sequences=True, recurrent_dropout=0.1))
model.add(Dropout(0.2))
model.add(LSTM(256, input_shape=input_dim, return_sequences=False, recurrent_dropout=0.1))
model.add(Dropout(0.2))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_4'
# Summery of result for this model:
# Try 1:
# batch_size=128, lr=0.001
#
#
#
#
def model_5(input_dim, output_dim):
"""
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (None, 50, 64) 41216
_________________________________________________________________
dropout_1 (Dropout) (None, 50, 64) 0
_________________________________________________________________
lstm_2 (LSTM) (None, 50, 64) 33024
_________________________________________________________________
dropout_2 (Dropout) (None, 50, 64) 0
_________________________________________________________________
lstm_3 (LSTM) (None, 64) 33024
_________________________________________________________________
dropout_3 (Dropout) (None, 64) 0
_________________________________________________________________
dense_1 (Dense) (None, 96) 6240
_________________________________________________________________
activation_1 (Activation) (None, 96) 0
=================================================================
Total params: 113,504
Trainable params: 113,504
Non-trainable params: 0
_________________________________________________________________
model_5 count_params ...
113504
:param input_dim:
:param output_dim:
:return:
"""
model = Sequential()
model.add(LSTM(64, return_sequences=True, input_shape=input_dim))
model.add(Dropout(0.2))
model.add(LSTM(64, return_sequences=True, input_shape=input_dim))
model.add(Dropout(0.2))
model.add(LSTM(64, return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_5'
# Summery of result for this model:
# Try 3:
# batch_size=128, lr=0.001
#
#
#
#
def model_6(input_dim, output_dim):
model = Sequential()
model.add(LSTM(128, input_shape=input_dim, return_sequences=True, recurrent_dropout=0.1))
model.add(Dropout(0.3))
model.add(LSTM(128, input_shape=input_dim, return_sequences=False, recurrent_dropout=0.1))
model.add(Dropout(0.3))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_6'
# ------------------------------------------------------------------------
# Unidirectional LSTM (Many to One)
#
# Summery of result for this model:
# Try 3:
# batch_size=128, lr=0.001
# With step 1 and neuron size 128 was very bad. Set step=3 and neuron size=256 and step=3
# With Adam Optimizer, Lr=0.001 and step=3. after 61 epoch is the bset model !!!
# Change from RMSProp to Adam fix the learning process
#
def model_7(input_dim, output_dim):
"""
model_7 summary ...
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (None, 50, 128) 98816
_________________________________________________________________
lstm_2 (LSTM) (None, 128) 131584
_________________________________________________________________
dense_1 (Dense) (None, 64) 8256
_________________________________________________________________
activation_1 (Activation) (None, 64) 0
=================================================================
Total params: 238,656
Trainable params: 238,656
Non-trainable params: 0
_________________________________________________________________
model_7 count_params ...
238656
:param input_dim:
:param output_dim:
:return:
"""
model = Sequential()
model.add(LSTM(128, input_shape=input_dim, return_sequences=True))
model.add(LSTM(128, input_shape=input_dim, return_sequences=False))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_7'
# Unidirectional LSTM (Many to One)
#
#
def model_8(input_dim, output_dim):
"""
model_8 summary ...
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (None, 50, 256) 328704
_________________________________________________________________
dropout_1 (Dropout) (None, 50, 256) 0
_________________________________________________________________
lstm_2 (LSTM) (None, 256) 525312
_________________________________________________________________
dropout_2 (Dropout) (None, 256) 0
_________________________________________________________________
dense_1 (Dense) (None, 64) 16448
_________________________________________________________________
activation_1 (Activation) (None, 64) 0
=================================================================
Total params: 870,464
Trainable params: 870,464
Non-trainable params: 0
_________________________________________________________________
model_8 count_params ...
870464
:param input_dim:
:param output_dim:
:return:
"""
model = Sequential()
model.add(LSTM(256, input_shape=input_dim, return_sequences=True, recurrent_dropout=0.1))
model.add(Dropout(0.3))
model.add(LSTM(256, input_shape=input_dim, return_sequences=False, recurrent_dropout=0.1))
model.add(Dropout(0.3))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_8'
# Bidirectional LSTM (Many to One)
#
#
def model_9(input_dim, output_dim):
"""
model_9 summary ...
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
bidirectional_1 (Bidirection (None, 256) 657408
_________________________________________________________________
dense_1 (Dense) (None, 64) 16448
_________________________________________________________________
activation_1 (Activation) (None, 64) 0
=================================================================
Total params: 673,856
Trainable params: 673,856
Non-trainable params: 0
_________________________________________________________________
model_9 count_params ...
673856
:param input_dim:
:param output_dim:
:return:
"""
model = Sequential()
model.add(Bidirectional(LSTM(256, return_sequences=False),
input_shape=input_dim,
merge_mode='sum'))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_9'
# Bidirectional Deep LSTM (Many to One)
#
#
def model_10(input_dim, output_dim):
"""
model_10 summary ...
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
bidirectional_1 (Bidirection (None, 50, 128) 197632
_________________________________________________________________
bidirectional_2 (Bidirection (None, 128) 263168
_________________________________________________________________
dense_1 (Dense) (None, 64) 8256
_________________________________________________________________
activation_1 (Activation) (None, 64) 0
=================================================================
Total params: 469,056
Trainable params: 469,056
Non-trainable params: 0
_________________________________________________________________
model_10 count_params ...
469056
:param input_dim:
:param output_dim:
:return:
"""
model = Sequential()
model.add(Bidirectional(LSTM(128, return_sequences=True),
input_shape=input_dim,
merge_mode='sum'))
model.add(Bidirectional(LSTM(128, return_sequences=False),
merge_mode='sum'))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model_10'
def model7_laf(input_dim, output_dim):
"""
model7_laf summary ...
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (None, 50, 128) 98816
_________________________________________________________________
lstm_2 (LSTM) (None, 128) 131584
_________________________________________________________________
dense_1 (Dense) (None, 64) 8256
_________________________________________________________________
activation_1 (Activation) (None, 64) 0
=================================================================
Total params: 238,656
Trainable params: 238,656
Non-trainable params: 0
_________________________________________________________________
model7_laf count_params ...
238656
:param input_dim:
:param output_dim:
:return:
"""
model = Sequential()
model.add(LSTM(128, input_shape=input_dim, return_sequences=True))
model.add(LSTM(128, return_sequences=False))
model.add(Dense(output_dim))
model.add(Activation('softmax'))
return model, 'model7_laf'