tune_mlp_keras_tuner.py 2.03 KB
Newer Older
1
from tools import data_loader
2
3
4
import tensorflow as tf
import kerastuner as kt
from pathlib import Path
5
import paths as pt
6
import shutil
7
8

CASE = "Complete"
9
10
COMPLETE_FILENAME = "complete_with_embeddings.csv"
FALL_FILENAME = "fall_with_embeddings.csv"
11
SCALING_STRATEGY = "Standard"
12

13
def create_model(hp, input_dim=14):
14
    model = tf.keras.models.Sequential()
15
16
    hp_activation = hp.Choice('activation', ['softmax', 'relu', 'sigmoid', 'linear'])
    for i in range(hp.Int('num_layers', 0, 2)):
17
        model.add(tf.keras.layers.Dense(units=hp.Int('units_' + str(i),
18
19
                                                     min_value=32,
                                                     max_value=128,
20
21
                                                     step=8),
                                        input_dim=input_dim,
22
23
                                        activation=hp_activation))
    hp_dropout = hp.Choice('dropout', values=[0.1, 0.2, 0.5])
24
25
    model.add(tf.keras.layers.Dropout(hp_dropout))
    model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
26
    model.compile(optimizer=tf.keras.optimizers.Adam(),
27
28
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
29
30
31
32
    return model

def main():
    if CASE == "Complete":
33
        X_train, X_test, y_train, y_test = data_loader.CompleteDataLoader(
34
            COMPLETE_FILENAME).load_data().prepare_data_split(SCALING_STRATEGY, 0.3)
35
    else:
36
        X_train, X_test, y_train, y_test = data_loader.FallDataLoader(
37
            FALL_FILENAME).load_data().prepare_data_split(SCALING_STRATEGY, 0.3)
38

39
40
    tuner = kt.BayesianOptimization(create_model,
     objective='val_accuracy', max_trials=20, executions_per_trial=2,
41
     directory=Path.joinpath(pt.REPORTS_DIR, 'keras_tuner'),
42
43
     project_name='complete_mlp',
     seed=0)
44

45
    tuner.search(X_train, y_train, epochs=50,
46
47
48
49
                validation_data=(X_test, y_test))

    print(tuner.get_best_hyperparameters(num_trials=1)[0].values)

50
    shutil.rmtree(Path.joinpath(pt.REPORTS_DIR, 'keras_tuner'))
51

52
53
54
55
if __name__ == '__main__':
    main()