1
|
1 |
|
from keras.models import Sequential |
2
|
1 |
|
from keras.layers import Dense, Activation, Convolution1D, Lambda, Convolution2D, Flatten, \ |
|
|
|
|
3
|
|
|
Reshape, LSTM, Dropout, TimeDistributed, BatchNormalization |
4
|
1 |
|
from keras.regularizers import l2 |
5
|
1 |
|
from keras.optimizers import Adam |
6
|
1 |
|
import numpy as np |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
def generate_models( |
10
|
|
|
x_shape, number_of_classes, number_of_models=5, model_type=None, |
11
|
|
|
cnn_min_layers=1, cnn_max_layers=10, |
12
|
|
|
cnn_min_filters=10, cnn_max_filters=100, |
13
|
|
|
cnn_min_fc_nodes=10, cnn_max_fc_nodes=2000, |
14
|
|
|
deepconvlstm_min_conv_layers=1, deepconvlstm_max_conv_layers=10, |
15
|
|
|
deepconvlstm_min_conv_filters=10, deepconvlstm_max_conv_filters=100, |
16
|
|
|
deepconvlstm_min_lstm_layers=1, deepconvlstm_max_lstm_layers=5, |
17
|
|
|
deepconvlstm_min_lstm_dims=10, deepconvlstm_max_lstm_dims=100, |
18
|
|
|
low_lr=1, high_lr=4, low_reg=1, high_reg=4 |
19
|
|
|
): |
20
|
|
|
""" |
21
|
|
|
Generate one or multiple Keras models with random hyperparameters. |
22
|
|
|
|
23
|
|
|
Parameters |
24
|
|
|
---------- |
25
|
|
|
x_shape |
26
|
|
|
Shape of the input dataset: (num_samples, num_timesteps, num_channels) |
27
|
|
|
number_of_classes |
28
|
|
|
Number of classes for classification task |
29
|
|
|
number_of_models |
30
|
|
|
Number of models to generate |
31
|
|
|
model_type : str (optional) |
32
|
|
|
Type of model to build: 'CNN' or 'DeepConvLSTM'. Default option None generates both models. |
|
|
|
|
33
|
|
|
cnn_min_layers : int |
34
|
|
|
minimum of Conv layers in CNN model |
35
|
|
|
cnn_max_layers : int |
36
|
|
|
maximum of Conv layers in CNN model |
37
|
|
|
cnn_min_filters : int |
38
|
|
|
minimum number of filters per Conv layer in CNN model |
39
|
|
|
cnn_max_filters : int |
40
|
|
|
maximum number of filters per Conv layer in CNN model |
41
|
|
|
cnn_min_fc_nodes : int |
42
|
|
|
minimum number of hidden nodes per Dense layer in CNN model |
43
|
|
|
cnn_max_fc_nodes : int |
44
|
|
|
maximum number of hidden nodes per Dense layer in CNN model |
45
|
|
|
deepconvlstm_min_conv_layers : int |
46
|
|
|
minimum number of Conv layers in DeepConvLSTM model |
47
|
|
|
deepconvlstm_max_conv_layers : int |
48
|
|
|
maximum number of Conv layers in DeepConvLSTM model |
49
|
|
|
deepconvlstm_min_conv_filters : int |
50
|
|
|
minimum number of filters per Conv layer in DeepConvLSTM model |
51
|
|
|
deepconvlstm_max_conv_filters : int |
52
|
|
|
maximum number of filters per Conv layer in DeepConvLSTM model |
53
|
|
|
deepconvlstm_min_lstm_layers : int |
54
|
|
|
minimum number of Conv layers in DeepConvLSTM model |
55
|
|
|
deepconvlstm_max_lstm_layers : int |
56
|
|
|
maximum number of Conv layers in DeepConvLSTM model |
57
|
|
|
deepconvlstm_min_lstm_dims : int |
58
|
|
|
minimum number of hidden nodes per LSTM layer in DeepConvLSTM model |
59
|
|
|
deepconvlstm_max_lstm_dims : int |
60
|
|
|
maximum number of hidden nodes per LSTM layer in DeepConvLSTM model |
61
|
|
|
low_lr : float |
62
|
|
|
minimum of log range for learning rate: learning rate is sampled between `10**(-low_reg)` and `10**(-high_reg)` |
|
|
|
|
63
|
|
|
high_lr : float |
64
|
|
|
maximum of log range for learning rate: learning rate is sampled between `10**(-low_reg)` |
|
|
|
|
65
|
|
|
and `10**(-high_reg)` |
66
|
|
|
low_reg : float |
67
|
|
|
minimum of log range for regularization rate: regularization rate is sampled between `10**(-low_reg)` |
|
|
|
|
68
|
|
|
and `10**(-high_reg)` |
69
|
|
|
high_reg : float |
70
|
|
|
maximum of log range for regularization rate: regularization rate is sampled between `10**(-low_reg)` |
|
|
|
|
71
|
|
|
and `10**(-high_reg)` |
72
|
|
|
|
73
|
|
|
Returns |
74
|
|
|
------- |
75
|
|
|
List of compiled models |
76
|
|
|
""" |
77
|
1 |
|
models = [] |
78
|
1 |
|
for _ in range(0, number_of_models): |
79
|
1 |
|
if model_type is None: # random model choice: |
80
|
1 |
|
current_model_type = 'CNN' if np.random.random( |
81
|
|
|
) < 0.5 else 'DeepConvLSTM' |
82
|
|
|
else: # user-defined model choice: |
83
|
|
|
current_model_type = model_type |
84
|
1 |
|
generate_model = None |
85
|
1 |
|
if current_model_type == 'CNN': |
86
|
1 |
|
generate_model = generate_CNN_model # object is a function |
87
|
1 |
|
hyperparameters = generate_CNN_hyperparameter_set( |
88
|
|
|
min_layers=cnn_min_layers, max_layers=cnn_max_layers, |
89
|
|
|
min_filters=cnn_min_filters, max_filters=cnn_max_filters, |
90
|
|
|
min_fc_nodes=cnn_min_fc_nodes, max_fc_nodes=cnn_max_fc_nodes, |
91
|
|
|
low_lr=low_lr, high_lr=high_lr, low_reg=low_reg, high_reg=high_reg) |
|
|
|
|
92
|
1 |
|
if current_model_type == 'DeepConvLSTM': |
93
|
|
|
generate_model = generate_DeepConvLSTM_model # object is a function |
|
|
|
|
94
|
|
|
hyperparameters = generate_DeepConvLSTM_hyperparameter_set( |
95
|
|
|
min_conv_layers=deepconvlstm_min_conv_layers, max_conv_layers=deepconvlstm_max_conv_layers, |
|
|
|
|
96
|
|
|
min_conv_filters=deepconvlstm_min_conv_filters, max_conv_filters=deepconvlstm_max_conv_filters, |
|
|
|
|
97
|
|
|
min_lstm_layers=deepconvlstm_min_lstm_layers, max_lstm_layers=deepconvlstm_max_lstm_layers, |
|
|
|
|
98
|
|
|
min_lstm_dims=deepconvlstm_min_lstm_dims, max_lstm_dims=deepconvlstm_max_lstm_dims, |
|
|
|
|
99
|
|
|
low_lr=low_lr, high_lr=high_lr, low_reg=low_reg, high_reg=high_reg) |
|
|
|
|
100
|
1 |
|
models.append( |
101
|
|
|
(generate_model(x_shape, number_of_classes, **hyperparameters), hyperparameters, current_model_type)) |
|
|
|
|
102
|
1 |
|
return models |
103
|
|
|
|
104
|
|
|
|
105
|
1 |
|
def generate_DeepConvLSTM_model( |
|
|
|
|
106
|
|
|
x_shape, class_number, filters, lstm_dims, learning_rate=0.01, |
107
|
|
|
regularization_rate=0.01): |
108
|
|
|
""" |
109
|
|
|
Generate a model with convolution and LSTM layers. |
110
|
|
|
See Ordonez et al., 2016, http://dx.doi.org/10.3390/s16010115 |
111
|
|
|
|
112
|
|
|
Parameters |
113
|
|
|
---------- |
114
|
|
|
x_shape : tuple |
115
|
|
|
Shape of the input dataset: (num_samples, num_timesteps, num_channels) |
116
|
|
|
class_number : int |
117
|
|
|
Number of classes for classification task |
118
|
|
|
filters : list of ints |
119
|
|
|
number of filters for each convolutional layer |
120
|
|
|
lstm_dims : list of ints |
121
|
|
|
number of hidden nodes for each LSTM layer |
122
|
|
|
learning_rate : float |
123
|
|
|
regularization_rate : float |
124
|
|
|
|
125
|
|
|
Returns |
126
|
|
|
------- |
127
|
|
|
The compiled Keras model |
128
|
|
|
""" |
129
|
1 |
|
dim_length = x_shape[1] # number of samples in a time series |
130
|
1 |
|
dim_channels = x_shape[2] # number of channels |
131
|
1 |
|
output_dim = class_number # number of classes |
132
|
1 |
|
weightinit = 'lecun_uniform' # weight initialization |
133
|
1 |
|
model = Sequential() # initialize model |
134
|
1 |
|
model.add(BatchNormalization(input_shape=(dim_length, dim_channels))) |
135
|
|
|
# reshape a 2 dimensional array per file/person/object into a |
136
|
|
|
# 3 dimensional array |
137
|
1 |
|
model.add( |
138
|
|
|
Reshape(target_shape=(1, dim_length, dim_channels))) |
139
|
1 |
|
for filt in filters: |
140
|
|
|
# filt: number of filters used in a layer |
141
|
|
|
# filters: vector of filt values |
142
|
1 |
|
model.add( |
143
|
|
|
Convolution2D(filt, nb_row=3, nb_col=1, border_mode='same', |
144
|
|
|
W_regularizer=l2(regularization_rate), init=weightinit)) |
|
|
|
|
145
|
1 |
|
model.add(BatchNormalization()) |
146
|
1 |
|
model.add(Activation('relu')) |
147
|
|
|
# reshape 3 dimensional array back into a 2 dimensional array, |
148
|
|
|
# but now with more dept as we have the the filters for each channel |
149
|
1 |
|
model.add(Reshape(target_shape=(dim_length, filters[-1] * dim_channels))) |
150
|
|
|
|
151
|
1 |
|
for lstm_dim in lstm_dims: |
152
|
1 |
|
model.add(LSTM(output_dim=lstm_dim, return_sequences=True, |
153
|
|
|
activation='tanh')) |
154
|
|
|
|
155
|
1 |
|
model.add(Dropout(0.5)) # dropout before the dense layer |
156
|
|
|
# set up final dense layer such that every timestamp is given one |
157
|
|
|
# classification |
158
|
1 |
|
model.add( |
159
|
|
|
TimeDistributed(Dense(output_dim, W_regularizer=l2(regularization_rate)))) |
|
|
|
|
160
|
1 |
|
model.add(Activation("softmax")) |
161
|
|
|
# Final classification layer - per timestep |
162
|
1 |
|
model.add(Lambda(lambda x: x[:, -1, :], output_shape=[output_dim])) |
163
|
|
|
|
164
|
1 |
|
model.compile(loss='categorical_crossentropy', |
165
|
|
|
optimizer=Adam(lr=learning_rate), |
166
|
|
|
metrics=['accuracy']) |
167
|
|
|
|
168
|
1 |
|
return model |
169
|
|
|
|
170
|
|
|
|
171
|
1 |
|
def generate_CNN_model(x_shape, class_number, filters, fc_hidden_nodes, |
|
|
|
|
172
|
|
|
learning_rate=0.01, regularization_rate=0.01): |
173
|
|
|
""" |
174
|
|
|
Generate a convolutional neural network (CNN) model. |
175
|
|
|
|
176
|
|
|
The compiled Keras model is returned. |
177
|
|
|
|
178
|
|
|
Parameters |
179
|
|
|
---------- |
180
|
|
|
x_shape : tuple |
181
|
|
|
Shape of the input dataset: (num_samples, num_timesteps, num_channels) |
182
|
|
|
class_number : int |
183
|
|
|
Number of classes for classification task |
184
|
|
|
filters : list of ints |
185
|
|
|
number of filters for each convolutional layer |
186
|
|
|
fc_hidden_nodes : int |
187
|
|
|
number of hidden nodes for the hidden dense layer |
188
|
|
|
learning_rate : float |
189
|
|
|
regularization_rate : float |
190
|
|
|
|
191
|
|
|
Returns |
192
|
|
|
------- |
193
|
|
|
The compiled Keras model |
194
|
|
|
""" |
195
|
1 |
|
dim_length = x_shape[1] # number of samples in a time series |
196
|
1 |
|
dim_channels = x_shape[2] # number of channels |
197
|
1 |
|
outputdim = class_number # number of classes |
198
|
1 |
|
weightinit = 'lecun_uniform' # weight initialization |
199
|
1 |
|
model = Sequential() |
200
|
1 |
|
model.add( |
201
|
|
|
BatchNormalization( |
202
|
|
|
input_shape=( |
203
|
|
|
dim_length, |
204
|
|
|
dim_channels), |
205
|
|
|
mode=0, |
206
|
|
|
axis=2)) |
207
|
1 |
|
for filter_number in filters: |
208
|
1 |
|
model.add(Convolution1D(filter_number, 3, border_mode='same', |
209
|
|
|
W_regularizer=l2(regularization_rate), init=weightinit)) |
|
|
|
|
210
|
1 |
|
model.add(BatchNormalization()) |
211
|
1 |
|
model.add(Activation('relu')) |
212
|
1 |
|
model.add(Flatten()) |
213
|
1 |
|
model.add(Dense(output_dim=fc_hidden_nodes, |
214
|
|
|
W_regularizer=l2(regularization_rate), init=weightinit)) # Fully connected layer |
|
|
|
|
215
|
1 |
|
model.add(Activation('relu')) # Relu activation |
216
|
1 |
|
model.add(Dense(output_dim=outputdim, init=weightinit)) |
217
|
1 |
|
model.add(BatchNormalization()) |
218
|
1 |
|
model.add(Activation("softmax")) # Final classification layer |
219
|
|
|
|
220
|
1 |
|
model.compile(loss='categorical_crossentropy', |
221
|
|
|
optimizer=Adam(lr=learning_rate), |
222
|
|
|
metrics=['accuracy']) |
223
|
|
|
|
224
|
1 |
|
return model |
225
|
|
|
|
226
|
|
|
|
227
|
1 |
|
def generate_CNN_hyperparameter_set(min_layers=1, max_layers=10, |
|
|
|
|
228
|
|
|
min_filters=10, max_filters=100, |
229
|
|
|
min_fc_nodes=10, max_fc_nodes=2000, |
230
|
|
|
low_lr=1, high_lr=4, low_reg=1, high_reg=4): |
|
|
|
|
231
|
|
|
""" Generate a hyperparameter set that define a CNN model. |
232
|
|
|
|
233
|
|
|
Parameters |
234
|
|
|
---------- |
235
|
|
|
min_layers : int |
236
|
|
|
minimum of Conv layers |
237
|
|
|
max_layers : int |
238
|
|
|
maximum of Conv layers |
239
|
|
|
min_filters : int |
240
|
|
|
minimum number of filters per Conv layer |
241
|
|
|
max_filters : int |
242
|
|
|
maximum number of filters per Conv layer |
243
|
|
|
min_fc_nodes : int |
244
|
|
|
minimum number of hidden nodes per Dense layer |
245
|
|
|
max_fc_nodes : int |
246
|
|
|
maximum number of hidden nodes per Dense layer |
247
|
|
|
low_lr : float |
248
|
|
|
minimum of log range for learning rate: learning rate is sampled between `10**(-low_reg)` |
|
|
|
|
249
|
|
|
and `10**(-high_reg)` |
250
|
|
|
high_lr : float |
251
|
|
|
maximum of log range for learning rate: learning rate is sampled between `10**(-low_reg)` |
|
|
|
|
252
|
|
|
and `10**(-high_reg)` |
253
|
|
|
low_reg : float |
254
|
|
|
minimum of log range for regularization rate: regularization rate is sampled between `10**(-low_reg)` |
|
|
|
|
255
|
|
|
and `10**(-high_reg)` |
256
|
|
|
high_reg : float |
257
|
|
|
maximum of log range for regularization rate: regularization rate is sampled between `10**(-low_reg)` |
|
|
|
|
258
|
|
|
and `10**(-high_reg)` |
259
|
|
|
|
260
|
|
|
Returns |
261
|
|
|
---------- |
262
|
|
|
hyperparameters : dict |
263
|
|
|
parameters for a CNN model |
264
|
|
|
""" |
265
|
1 |
|
hyperparameters = generate_base_hyper_parameter_set( |
266
|
|
|
low_lr, high_lr, low_reg, high_reg) |
267
|
1 |
|
number_of_layers = np.random.randint(min_layers, max_layers + 1) |
268
|
1 |
|
hyperparameters['filters'] = np.random.randint( |
269
|
|
|
min_filters, max_filters + 1, number_of_layers) |
270
|
1 |
|
hyperparameters['fc_hidden_nodes'] = np.random.randint( |
271
|
|
|
min_fc_nodes, max_fc_nodes + 1) |
272
|
1 |
|
return hyperparameters |
273
|
|
|
|
274
|
|
|
|
275
|
1 |
|
def generate_DeepConvLSTM_hyperparameter_set( |
|
|
|
|
276
|
|
|
min_conv_layers=1, max_conv_layers=10, |
277
|
|
|
min_conv_filters=10, max_conv_filters=100, |
278
|
|
|
min_lstm_layers=1, max_lstm_layers=5, |
279
|
|
|
min_lstm_dims=10, max_lstm_dims=100, |
280
|
|
|
low_lr=1, high_lr=4, low_reg=1, high_reg=4): |
281
|
|
|
""" Generate a hyperparameter set that defines a DeepConvLSTM model. |
282
|
|
|
|
283
|
|
|
Parameters |
284
|
|
|
---------- |
285
|
|
|
min_conv_layers : int |
286
|
|
|
minimum number of Conv layers in DeepConvLSTM model |
287
|
|
|
max_conv_layers : int |
288
|
|
|
maximum number of Conv layers in DeepConvLSTM model |
289
|
|
|
min_conv_filters : int |
290
|
|
|
minimum number of filters per Conv layer in DeepConvLSTM model |
291
|
|
|
max_conv_filters : int |
292
|
|
|
maximum number of filters per Conv layer in DeepConvLSTM model |
293
|
|
|
min_lstm_layers : int |
294
|
|
|
minimum number of Conv layers in DeepConvLSTM model |
295
|
|
|
max_lstm_layers : int |
296
|
|
|
maximum number of Conv layers in DeepConvLSTM model |
297
|
|
|
min_lstm_dims : int |
298
|
|
|
minimum number of hidden nodes per LSTM layer in DeepConvLSTM model |
299
|
|
|
max_lstm_dims : int |
300
|
|
|
maximum number of hidden nodes per LSTM layer in DeepConvLSTM model |
301
|
|
|
low_lr : float |
302
|
|
|
minimum of log range for learning rate: learning rate is sampled between `10**(-low_reg)` |
|
|
|
|
303
|
|
|
and `10**(-high_reg)` |
304
|
|
|
high_lr : float |
305
|
|
|
maximum of log range for learning rate: learning rate is sampled between `10**(-low_reg)` |
|
|
|
|
306
|
|
|
and `10**(-high_reg)` |
307
|
|
|
low_reg : float |
308
|
|
|
minimum of log range for regularization rate: regularization rate is sampled between `10**(-low_reg)` |
|
|
|
|
309
|
|
|
and `10**(-high_reg)` |
310
|
|
|
high_reg : float |
311
|
|
|
maximum of log range for regularization rate: regularization rate is sampled between `10**(-low_reg)` |
|
|
|
|
312
|
|
|
and `10**(-high_reg)` |
313
|
|
|
|
314
|
|
|
Returns |
315
|
|
|
---------- |
316
|
|
|
hyperparameters: dict |
317
|
|
|
hyperparameters for a DeepConvLSTM model |
318
|
|
|
""" |
319
|
1 |
|
hyperparameters = generate_base_hyper_parameter_set( |
320
|
|
|
low_lr, high_lr, low_reg, high_reg) |
321
|
1 |
|
number_of_conv_layers = np.random.randint( |
322
|
|
|
min_conv_layers, max_conv_layers + 1) |
323
|
1 |
|
hyperparameters['filters'] = np.random.randint( |
324
|
|
|
min_conv_filters, max_conv_filters + 1, number_of_conv_layers) |
325
|
1 |
|
number_of_lstm_layers = np.random.randint( |
326
|
|
|
min_lstm_layers, max_lstm_layers + 1) |
327
|
1 |
|
hyperparameters['lstm_dims'] = np.random.randint( |
328
|
|
|
min_lstm_dims, max_lstm_dims + 1, number_of_lstm_layers) |
329
|
1 |
|
return hyperparameters |
330
|
|
|
|
331
|
|
|
|
332
|
1 |
|
def generate_base_hyper_parameter_set( |
|
|
|
|
333
|
|
|
low_lr=1, |
334
|
|
|
high_lr=4, |
335
|
|
|
low_reg=1, |
336
|
|
|
high_reg=4): |
337
|
|
|
""" Generate a base set of hyperparameters that are necessary for any model, but sufficient for none. |
|
|
|
|
338
|
|
|
|
339
|
|
|
Parameters |
340
|
|
|
---------- |
341
|
|
|
low_lr : float |
342
|
|
|
minimum of log range for learning rate: learning rate is sampled between `10**(-low_reg)` and `10**(-high_reg)` |
|
|
|
|
343
|
|
|
high_lr : float |
344
|
|
|
maximum of log range for learning rate: learning rate is sampled between `10**(-low_reg)` |
|
|
|
|
345
|
|
|
and `10**(-high_reg)` |
346
|
|
|
low_reg : float |
347
|
|
|
minimum of log range for regularization rate: regularization rate is sampled between `10**(-low_reg)` |
|
|
|
|
348
|
|
|
and `10**(-high_reg)` |
349
|
|
|
high_reg : float |
350
|
|
|
maximum of log range for regularization rate: regularization rate is sampled between `10**(-low_reg)` |
|
|
|
|
351
|
|
|
and `10**(-high_reg)` |
352
|
|
|
|
353
|
|
|
Returns |
354
|
|
|
------- |
355
|
|
|
hyperparameters : dict |
356
|
|
|
basis hyperpameters |
357
|
|
|
""" |
358
|
1 |
|
hyperparameters = {} |
359
|
1 |
|
hyperparameters['learning_rate'] = get_learning_rate(low_lr, high_lr) |
360
|
1 |
|
hyperparameters['regularization_rate'] = get_regularization( |
361
|
|
|
low_reg, high_reg) |
362
|
1 |
|
return hyperparameters |
363
|
|
|
|
364
|
|
|
|
365
|
1 |
|
def get_learning_rate(low=1, high=4): |
366
|
|
|
""" Return random learning rate 10^-n where n is sampled uniformly between low and high bounds. |
|
|
|
|
367
|
|
|
|
368
|
|
|
Parameters |
369
|
|
|
---------- |
370
|
|
|
low : float |
371
|
|
|
low bound |
372
|
|
|
high : float |
373
|
|
|
high bound |
374
|
|
|
""" |
375
|
1 |
|
result = 10 ** (-np.random.uniform(low, high)) |
376
|
1 |
|
return result |
377
|
|
|
|
378
|
|
|
|
379
|
1 |
|
def get_regularization(low=1, high=4): |
380
|
|
|
""" Return random regularization rate 10^-n where n is sampled uniformly between low and high bounds. |
|
|
|
|
381
|
|
|
|
382
|
|
|
Parameters |
383
|
|
|
---------- |
384
|
|
|
low : float |
385
|
|
|
low bound |
386
|
|
|
high : float |
387
|
|
|
high bound |
388
|
|
|
""" |
389
|
|
|
return 10 ** (-np.random.uniform(low, high)) |
390
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.