forked from fastmachinelearning/keras-training
-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
366 lines (320 loc) · 17.6 KB
/
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
from __future__ import print_function
from keras.layers import Dense, Dropout, Flatten, Convolution2D, merge, Convolution1D, Conv2D, Conv1D, Input, SpatialDropout1D, GRU, MaxPooling1D, AveragePooling1D, SimpleRNN, LSTM, BatchNormalization, Activation
from keras.models import Model, Sequential
from keras.regularizers import l1
import h5py
from constraints import *
from quantized_layers import BinaryDense, TernaryDense, QuantizedDense
from quantized_ops import binary_tanh as binary_tanh_op
from quantized_ops import ternarize
from quantized_ops import quantized_relu as quantize_op
def binary_tanh(x):
return binary_tanh_op(x)
def ternary_tanh(x):
x = K.clip(x, -1, 1)
return ternarize(x)
def quantized_relu(x):
return quantize_op(x,nb=4)
def dense_model(Inputs, nclasses, l1Reg=0, dropoutRate=0.25):
"""
Dense matrix, defaults similar to 2016 DeepCSV training
"""
x = Dense(200, activation='relu', kernel_initializer='lecun_uniform', name='fc1_relu', W_regularizer=l1(l1Reg))(Inputs)
x = Dropout(dropoutRate)(x)
x = Dense(200, activation='relu', kernel_initializer='lecun_uniform', name='fc2_relu', W_regularizer=l1(l1Reg))(x)
x = Dropout(dropoutRate)(x)
x = Dense(200, activation='relu', kernel_initializer='lecun_uniform', name='fc3_relu', W_regularizer=l1(l1Reg))(x)
x = Dropout(dropoutRate)(x)
x = Dense(200, activation='relu', kernel_initializer='lecun_uniform', name='fc4_relu', W_regularizer=l1(l1Reg))(x)
x = Dropout(dropoutRate)(x)
x = Dense(200, activation='relu', kernel_initializer='lecun_uniform', name='fc5_relu', W_regularizer=l1(l1Reg))(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform', name = 'output_softmax')(x)
model = Model(inputs=Inputs, outputs=predictions)
return model
def two_layer_model(Inputs, nclasses, l1Reg=0):
"""
One hidden layer model
"""
x = Dense(32, activation='relu', kernel_initializer='lecun_uniform',
name='fc1_relu', W_regularizer=l1(l1Reg))(Inputs)
predictions = Dense(nclasses, activation='sigmoid', kernel_initializer='lecun_uniform',
name = 'output_sigmoid', W_regularizer=l1(l1Reg))(x)
model = Model(inputs=Inputs, outputs=predictions)
return model
def two_layer_model_constraint(Inputs, nclasses, l1Reg=0, h5fName=None):
"""
One hidden layer model
"""
x = Dense(32, activation='relu', kernel_initializer='lecun_uniform',
name='fc1_relu', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['fc1_relu'][()].tolist()))(Inputs)
predictions = Dense(nclasses, activation='sigmoid', kernel_initializer='lecun_uniform',
name = 'output_sigmoid', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['output_softmax'][()].tolist()))(x)
model = Model(inputs=Inputs, outputs=predictions)
return model
def three_layer_model(Inputs, nclasses, l1Reg=0):
"""
Two hidden layers model
"""
x = Dense(64, activation='relu', kernel_initializer='lecun_uniform',
name='fc1_relu', W_regularizer=l1(l1Reg))(Inputs)
x = Dense(32, activation='relu', kernel_initializer='lecun_uniform',
name='fc2_relu', W_regularizer=l1(l1Reg))(x)
x = Dense(32, activation='relu', kernel_initializer='lecun_uniform',
name='fc3_relu', W_regularizer=l1(l1Reg))(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform',
name='output_softmax', W_regularizer=l1(l1Reg))(x)
model = Model(inputs=Inputs, outputs=predictions)
return model
def three_layer_model_batch_norm(Inputs, nclasses, l1Reg=0):
"""
Two hidden layers model
"""
x = Dense(64, kernel_initializer='lecun_uniform',
name='fc1_relu', W_regularizer=l1(l1Reg))(Inputs)
x = BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn1')(x)
x = Activation(activation='relu', name='relu1')(x)
x = Dense(32, kernel_initializer='lecun_uniform',
name='fc2_relu', W_regularizer=l1(l1Reg))(x)
x = BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn2')(x)
x = Activation(activation='relu', name='relu2')(x)
x = Dense(32, kernel_initializer='lecun_uniform',
name='fc3_relu', W_regularizer=l1(l1Reg))(x)
x = BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn3')(x)
x = Activation(activation='relu', name='relu3')(x)
x = Dense(nclasses, kernel_initializer='lecun_uniform',
name='output_softmax', W_regularizer=l1(l1Reg))(x)
x = BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn4')(x)
predictions = Activation(activation='softmax', name='softmax')(x)
model = Model(inputs=Inputs, outputs=predictions)
return model
def three_layer_model_binary(Inputs, nclasses, l1Reg=0):
"""
Three hidden layers model
"""
model = Sequential()
model.add(BinaryDense(64, H=1, use_bias=False, name='fc1', input_shape=(16,)))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn1'))
model.add(Activation(binary_tanh, name='act{}'.format(1)))
model.add(BinaryDense(32, H=1, use_bias=False, name='fc2'))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn2'))
model.add(Activation(binary_tanh, name='act{}'.format(2)))
model.add(BinaryDense(32, H=1, use_bias=False, name='fc3'))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn3'))
model.add(Activation(binary_tanh, name='act{}'.format(3)))
model.add(BinaryDense(nclasses, H=1, use_bias=False, name='output'))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn'))
return model
def three_layer_model_ternary(Inputs, nclasses, l1Reg=0):
"""
Three hidden layers model
"""
model = Sequential()
model.add(TernaryDense(64, H=1, use_bias=False, name='fc1', input_shape=(16,)))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn1'))
model.add(Activation(ternary_tanh, name='act{}'.format(1)))
model.add(TernaryDense(32, H=1, use_bias=False, name='fc2'))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn2'))
model.add(Activation(ternary_tanh, name='act{}'.format(2)))
model.add(TernaryDense(32, H=1, use_bias=False, name='fc3'))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn3'))
model.add(Activation(ternary_tanh, name='act{}'.format(3)))
model.add(TernaryDense(nclasses, H=1, use_bias=False, name='output'))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn'))
return model
def three_layer_model_qnn(Inputs, nclasses, l1Reg=0):
"""
Three hidden layers model
"""
model = Sequential()
model.add(QuantizedDense(64, nb=4, H='Glorot', kernel_lr_multiplier='Glorot', use_bias=False, name='fc1', input_shape=(16,)))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn1'))
model.add(Activation(quantized_relu, name='act{}'.format(1)))
model.add(QuantizedDense(32, nb=4, H='Glorot', kernel_lr_multiplier='Glorot', use_bias=False, name='fc2'))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn2'))
model.add(Activation(quantized_relu, name='act{}'.format(2)))
model.add(QuantizedDense(32, nb=4, H='Glorot', kernel_lr_multiplier='Glorot', use_bias=False, name='fc3'))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn3'))
model.add(Activation(quantized_relu, name='act{}'.format(3)))
model.add(QuantizedDense(nclasses, nb=4, H='Glorot', kernel_lr_multiplier='Glorot', use_bias=False, name='output'))
model.add(BatchNormalization(epsilon=1e-6, momentum=0.9, name='bn'))
return model
def three_layer_model_tanh(Inputs, nclasses, l1Reg=0):
"""
Two hidden layers model
"""
x = Dense(64, activation='tanh', kernel_initializer='lecun_uniform',
name='fc1_tanh', W_regularizer=l1(l1Reg))(Inputs)
x = Dense(32, activation='tanh', kernel_initializer='lecun_uniform',
name='fc2_tanh', W_regularizer=l1(l1Reg))(x)
x = Dense(32, activation='tanh', kernel_initializer='lecun_uniform',
name='fc3_tanh', W_regularizer=l1(l1Reg))(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform',
name='output_softmax', W_regularizer=l1(l1Reg))(x)
model = Model(inputs=Inputs, outputs=predictions)
return model
def three_layer_model_constraint(Inputs, nclasses, l1Reg=0, h5fName=None):
"""
Two hidden layers model
"""
h5f = h5py.File(h5fName)
x = Dense(64, activation='relu', kernel_initializer='lecun_uniform',
name='fc1_relu', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['fc1_relu'][()].tolist()))(Inputs)
x = Dense(32, activation='relu', kernel_initializer='lecun_uniform',
name='fc2_relu', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['fc2_relu'][()].tolist()))(x)
x = Dense(32, activation='relu', kernel_initializer='lecun_uniform',
name='fc3_relu', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['fc3_relu'][()].tolist()))(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform',
name='output_softmax', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['output_softmax'][()].tolist()))(x)
model = Model(inputs=Inputs, outputs=predictions)
return model
def three_layer_model_tanh_constraint(Inputs, nclasses, l1Reg=0, h5fName=None):
"""
Two hidden layers model
"""
h5f = h5py.File(h5fName)
x = Dense(64, activation='tanh', kernel_initializer='lecun_uniform',
name='fc1_tanh', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['fc1_tanh'][()].tolist()))(Inputs)
x = Dense(32, activation='tanh', kernel_initializer='lecun_uniform',
name='fc2_tanh', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['fc2_tanh'][()].tolist()))(x)
x = Dense(32, activation='tanh', kernel_initializer='lecun_uniform',
name='fc3_tanh', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['fc3_tanh'][()].tolist()))(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform',
name='output_softmax', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['output_softmax'][()].tolist()))(x)
model = Model(inputs=Inputs, outputs=predictions)
return model
def linear_model(Inputs, nclasses, l1Reg=0):
"""
Linear model
"""
predictions = Dense(nclasses, activation='linear', kernel_initializer='lecun_uniform', name='output_linear')(Inputs)
model = Model(inputs=Inputs, outputs=predictions)
return model
def conv1d_model(Inputs, nclasses, l1Reg=0):
"""
Conv1D model, kernel size 4
"""
x = Conv1D(filters=8, kernel_size=4, strides=1, padding='same',
kernel_initializer='he_normal', use_bias=True, name='conv1_relu',
activation = 'relu', W_regularizer=l1(l1Reg))(Inputs)
x = Conv1D(filters=4, kernel_size=4, strides=2, padding='same',
kernel_initializer='he_normal', use_bias=True, name='conv2_relu',
activation = 'relu', W_regularizer=l1(l1Reg))(x)
x = Conv1D(filters=2, kernel_size=4, strides=3, padding='same',
kernel_initializer='he_normal', use_bias=True, name='conv3_relu',
activation = 'relu', W_regularizer=l1(l1Reg))(x)
x = Flatten()(x)
x = Dense(32, activation='relu', kernel_initializer='lecun_uniform',
name='fc1_relu', W_regularizer=l1(l1Reg))(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform',
name='output_softmax', W_regularizer=l1(l1Reg))(x)
model = Model(inputs=Inputs, outputs=predictions)
print(model.summary())
return model
def conv1d_model_constraint(Inputs, nclasses, l1Reg=0, h5fName=None):
"""
Conv1D model, kernel size 4
"""
h5f = h5py.File(h5fName)
x = Conv1D(filters=8, kernel_size=4, strides=1, padding='same',
kernel_initializer='he_normal', use_bias=True, name='conv1_relu',
activation = 'relu', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['conv1_relu'][()].tolist()))(Inputs)
x = Conv1D(filters=8, kernel_size=4, strides=1, padding='same',
kernel_initializer='he_normal', use_bias=True, name='conv2_relu',
activation = 'relu', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['conv2_relu'][()].tolist()))(x)
x = Conv1D(filters=8, kernel_size=4, strides=1, padding='same',
kernel_initializer='he_normal', use_bias=True, name='conv3_relu',
activation = 'relu', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['conv3_relu'][()].tolist()))(x)
x = Flatten()(x)
x = Dense(32, activation='relu', kernel_initializer='lecun_uniform',
name='fc1_relu', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['fc1_relu'][()].tolist()))(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform',
name='output_softmax', W_regularizer=l1(l1Reg),
kernel_constraint = zero_some_weights(binary_tensor=h5f['output_softmax'][()].tolist()))(x)
model = Model(inputs=Inputs, outputs=predictions)
print(model.summary())
return model
def conv2d_model(Inputs, nclasses, l1Reg=0):
"""
Conv2D model, kernel size (11,11), (3,3), (3,3)
"""
x = Conv2D(filters=8, kernel_size=(11,11), strides=(1,1), padding='same',
kernel_initializer='he_normal', use_bias=True, name='conv1_relu',
activation = 'relu')(Inputs)
x = Conv2D(filters=4, kernel_size=(3,3), strides=(2,2), padding='same',
kernel_initializer='he_normal', use_bias=True, name='conv2_relu',
activation = 'relu')(x)
x = Conv2D(filters=2, kernel_size=(3,3), strides=(2,2), padding='same',
kernel_initializer='he_normal', use_bias=True, name='conv3_relu',
activation = 'relu')(x)
x = Flatten()(x)
x = Dense(32, activation='relu')(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform', name='output_softmax')(x)
model = Model(inputs=Inputs, outputs=predictions)
print(model.summary())
return model
def rnn_model(Inputs, nclasses, l1Reg=0):
"""
Simple RNN model
"""
x = SimpleRNN(72,return_sequences=True)(x)
x = Flatten()(x)
x = Dropout(0.1)(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform', name='rnn_densef')(x)
model = Model(inputs=Inputs, outputs=predictions)
print(model.summary())
return model
def lstm_model(Inputs, nclasses, l1Reg=0,l1RegR=0):
"""
Basic LSTM model
"""
x = LSTM(16,return_sequences=False, kernel_regularizer=l1(l1Reg),recurrent_regularizer=l1(l1RegR),activation='relu',kernel_initializer='lecun_uniform',name='lstm_lstm')(Inputs)
#x = Flatten()(x)
x = Dropout(0.1)(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform', name='rnn_densef')(x)
model = Model(inputs=Inputs, outputs=predictions)
print(model.summary())
return model
def lstm_model_constraint(Inputs, nclasses, l1Reg=0,l1RegR=0,h5fName=None):
"""
Basic LSTM model
"""
h5f = h5py.File(h5fName)
x = LSTM(16,return_sequences=False,kernel_regularizer=l1(l1Reg),recurrent_regularizer=l1(l1RegR),name='lstm_lstm',recurrent_constraint = zero_some_weights(binary_tensor=h5f['lstm_lstm'][()].tolist()))(Inputs)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform', kernel_constraint = zero_some_weights(binary_tensor=h5f['rnn_densef'][()].tolist()), name='rnn_densef')(x)
model = Model(inputs=Inputs, outputs=predictions)
print(model.summary())
return model
def lstm_model_full(Inputs, nclasses, l1Reg=0):
"""
LSTM model akin to what Sid is using for his studies
"""
x = Conv1D(32, 2, activation='relu', name='particles_conv0', kernel_initializer='lecun_uniform', padding='same')(Inputs)
x = Conv1D(16, 4, activation='relu', name='particles_conv1', kernel_initializer='lecun_uniform', padding='same')(x)
x = LSTM(72,return_sequences=True)(x)
x = Flatten()(x)
x = Dropout(0.1)(x)
x = Dense(128, activation='softmax', kernel_initializer='lecun_uniform', name='rnn_dense2')(x)
x = Dropout(0.1)(x)
x = Dense(128, activation='softmax', kernel_initializer='lecun_uniform', name='rnn_dense3')(x)
x = Dropout(0.1)(x)
predictions = Dense(nclasses, activation='softmax', kernel_initializer='lecun_uniform', name='rnn_densef')(x)
model = Model(inputs=Inputs, outputs=predictions)
print(model.summary())
return model
if __name__ == '__main__':
print(conv1d_model(Input(shape=(100,10,)), 2).summary())
print(conv2d_model(Input(shape=(10,10,3,)), 2).summary())