-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreateModel.py
More file actions
50 lines (39 loc) · 1.59 KB
/
createModel.py
File metadata and controls
50 lines (39 loc) · 1.59 KB
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
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
input_dim = 160
output_dim = 256
epochs = 2500
batch_size = 256
def createAndSaveModel(firstDenseDimension, hiddenDenseLayers, heightDenseLayer, file, x, y):
sequential_array = []
# Input layer – relu instead sigmoid (faster, no Vanishing-Gradient-Problem)
sequential_array.append(layers.Dense(firstDenseDimension, activation='relu', input_shape=(input_dim,)))
sequential_array.append(layers.BatchNormalization())
# Hidden layers with BatchNorm and Dropout
for i in range(hiddenDenseLayers):
sequential_array.append(layers.Dense(heightDenseLayer, activation='relu'))
sequential_array.append(layers.BatchNormalization())
sequential_array.append(layers.Dropout(0.2))
# Output layer – sigmoid, output binary (0/1 per Bit)
sequential_array.append(layers.Dense(output_dim, activation='sigmoid'))
model = Sequential(sequential_array)
model.compile(
optimizer='adamax',
loss='binary_crossentropy',
metrics=['accuracy', 'mae']
)
model.summary()
callbacks = [
# Stopp Training if 50 Epochs do not deliver better resulta
EarlyStopping(patience=50, restore_best_weights=True, verbose=1),
# Save the model
ModelCheckpoint(file, save_best_only=True, verbose=1)
]
model.fit(
x, y,
epochs=epochs,
batch_size=batch_size,
validation_split=0.1, # 10% of Data for validation
callbacks=callbacks
)