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