(
        data,
        target,
        max_layers=10,
        min_layers=2,
        min_dense=32,
        max_dense=512,
        executions_per_trial=1,
        max_trials=5,
        epochs=10,
        activation='relu',
        directory='my_dir',
        step=32,
        verbose=0,
        test_size=0.2
)
return models, hyp, history, X_test, y_test
Return:
        models[0] : best model obtained after tuning
        best_hps : best Hyperprameters obtained after tuning, stored as map
        history : history of the data executed from the given model
Return:
        models[0] : best model obtained after tuning
        best_hps : best Hyperprameters obtained after tuning, stored as map
        history : history of the data executed from the given model
Return : models [ 0 ] : best model obtained after tuning best_hps : best Hyperprameters obtained after tuning stored as map history : history of the data executed from the given model
def tuneReg(
        data,
        target,
        max_layers=10,
        min_layers=2,
        min_dense=32,
        max_dense=512,
        executions_per_trial=1,
        max_trials=5,
        epochs=10,
        activation='relu',
        directory='my_dir',
        step=32,
        verbose=0,
        test_size=0.2
):
    # function build model using hyperparameter
    def build_model(hp):
        model = keras.Sequential()
        model.add(Dense(units=hp.Int('units_0',
                                     min_value=min_dense,
                                     max_value=max_dense,
                                     step=step),
                                     input_dim=data.shape[1],
                                     activation=activation))
        for i in range(hp.Int('num_layers', min_layers, max_layers)):
            model.add(Dense(units=hp.Int('units_' + str(i+1),
                                         min_value=min_dense,
                                         max_value=max_dense,
                                         step=step),
                            activation=activation))
            model.add(Dropout(rate=hp.Float(
                              'dropout_'+ str(i),
                              min_value=0.0,
                              max_value=0.5,
                              default=0.20,
                              step=0.05)))
        model.add(Dense(1, activation='linear'))
        lrate=hp.Float('learning_rate',
                       min_value=1e-5,
                       max_value=1e-1,
                       sampling='LOG',
                       default=1e-3)
        model.compile(
            optimizer=hp.Choice('optimizer', values=[
                               keras.optimizers.Adam(learning_rate = lrate),
                               keras.optimizers.SGD(learning_rate = lrate),
                               keras.optimizers.RMSprop(learning_rate = lrate),
                               keras.optimizers.Adamax(learning_rate = lrate)
                               ]),
            loss=hp.Choice('loss', values=['mean_squared_logarithmic_error',
                                           'mean_squared_error',
                                           'huber_loss',
                                           'mean_absolute_error',
                                           'cosine_similarity',
                                           'log_cosh'],
                                     default='mean_squared_error'),
            metrics=['accuracy'])
        return model

    # random search for the model
    tuner = RandomSearch(
        build_model,
        objective='val_accuracy',
        max_trials=max_trials,
        executions_per_trial=executions_per_trial,
        directory=directory)
    # tuner.search_space_summary()
    # del data[target]

    X_train, X_test, y_train, y_test = train_test_split(
        data, target, test_size=0.2, random_state=49)

    # searches the tuner space defined by hyperparameters (hp) and returns the
    # best model

    tuner.search(X_train, y_train,
                 epochs=epochs,
                 validation_data=(X_test, y_test),
                 callbacks=[tf.keras.callbacks.TensorBoard('my_dir')])

    models = tuner.get_best_models(num_models=1)[0]
    hyp = tuner.get_best_hyperparameters(num_trials=1)[0]
    history = tuner_hist(
        data,
        target,
        tuner,
        hyp,
        epochs=epochs,
        verbose=verbose,
        test_size=test_size)
    """
    Return:
        models[0] : best model obtained after tuning
        best_hps : best Hyperprameters obtained after tuning, stored as map
        history : history of the data executed from the given model
    """
    return models, hyp, history, X_test, y_test
def tuneReg ( data , target , max_layers = 10 , min_layers = 2 , min_dense = 32 , max_dense = 512 , executions_per_trial = 1 , max_trials = 5 , epochs = 10 , activation = 'relu' , directory = 'my_dir' , step = 32 , verbose = 0 , test_size = 0.2 ) : # function build model using hyperparameter def build_model ( hp ) : model = keras . Sequential ( ) model . add ( Dense ( units = hp . Int ( 'units_0' , min_value = min_dense , max_value = max_dense , step = step ) , input_dim = data . shape [ 1 ] , activation = activation ) ) for i in range ( hp . Int ( 'num_layers' , min_layers , max_layers ) ) : model . add ( Dense ( units = hp . Int ( 'units_' + str ( i + 1 ) , min_value = min_dense , max_value = max_dense , step = step ) , activation = activation ) ) model . add ( Dropout ( rate = hp . Float ( 'dropout_' + str ( i ) , min_value = 0.0 , max_value = 0.5 , default = 0.20 , step = 0.05 ) ) ) model . add ( Dense ( 1 , activation = 'linear' ) ) lrate = hp . Float ( 'learning_rate' , min_value = 1e-5 , max_value = 1e-1 , sampling = 'LOG' , default = 1e-3 ) model . compile ( optimizer = hp . Choice ( 'optimizer' , values = [ keras . optimizers . Adam ( learning_rate = lrate ) , keras . optimizers . SGD ( learning_rate = lrate ) , keras . optimizers . RMSprop ( learning_rate = lrate ) , keras . optimizers . Adamax ( learning_rate = lrate ) ] ) , loss = hp . Choice ( 'loss' , values = [ 'mean_squared_logarithmic_error' , 'mean_squared_error' , 'huber_loss' , 'mean_absolute_error' , 'cosine_similarity' , 'log_cosh' ] , default = 'mean_squared_error' ) , metrics = [ 'accuracy' ] ) return model # random search for the model tuner = RandomSearch ( build_model , objective = 'val_accuracy' , max_trials = max_trials , executions_per_trial = executions_per_trial , directory = directory ) # tuner.search_space_summary() # del data[target] X_train , X_test , y_train , y_test = train_test_split ( data , target , test_size = 0.2 , random_state = 49 ) # searches the tuner space defined by hyperparameters (hp) and returns the # best model tuner . search ( X_train , y_train , epochs = epochs , validation_data = ( X_test , y_test ) , callbacks = [ tf . keras . callbacks . TensorBoard ( 'my_dir' ) ] ) models = tuner . get_best_models ( num_models = 1 ) [ 0 ] hyp = tuner . get_best_hyperparameters ( num_trials = 1 ) [ 0 ] history = tuner_hist ( data , target , tuner , hyp , epochs = epochs , verbose = verbose , test_size = test_size ) return models , hyp , history , X_test , y_test