forked from antoniovilela/pps-dilepton-analysis-notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keras_model.py
39 lines (30 loc) · 1.66 KB
/
keras_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
from tensorflow import keras
class Model:
def __init__(self, input_shape, learning_rate):
self.input_shape = input_shape
self.learning_rate = learning_rate
def __call__(self, n_hidden, n_neurons, dropout):
input_shape = self.input_shape
learning_rate = self.learning_rate
print( "Building model with:" )
print( "Input shape: {}".format(input_shape) )
print( "Learning rate: {}".format(learning_rate) )
print( "Number of hidden layers: {}".format(n_hidden) )
print( "Number of neurons per layer: {}".format(n_neurons) )
print( "Dropout rate: {}".format(dropout) )
model = keras.models.Sequential()
model.add( keras.layers.InputLayer(input_shape=input_shape) )
for layer in range(n_hidden):
if dropout > 0.:
model.add( keras.layers.Dropout(rate=dropout) )
model.add( keras.layers.Dense(n_neurons, activation="elu", kernel_initializer="he_normal") )
if dropout > 0.:
model.add( keras.layers.Dropout(rate=dropout) )
model.add( keras.layers.Dense(1, activation="sigmoid") )
#optimizer = keras.optimizers.SGD(lr=learning_rate, momentum=0.9, nesterov=True)
optimizer = keras.optimizers.Nadam(lr=learning_rate)
model.compile( loss="binary_crossentropy", optimizer=optimizer, metrics=["accuracy"])
return model
def build_model(input_shape, learning_rate=5e-4, n_hidden=1, n_neurons=50, dropout=0.20 ):
build_fn_ = Model( input_shape=input_shape, learning_rate=learning_rate )
return build_fn_( n_hidden, n_neurons, dropout )