(
        X,
        y,
        num_classes,
        max_layers=10,
        min_layers=2,
        min_dense=32,
        max_dense=512,
        executions_per_trial=1,
        max_trials=3,
        activation='relu',
        directory='my_dir',
        loss='categorical_crossentropy',
        metrics='accuracy',
        epochs=10,
        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 array
        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 array
        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 array history : history of the data executed from the given model
def tuneClass(
        X,
        y,
        num_classes,
        max_layers=10,
        min_layers=2,
        min_dense=32,
        max_dense=512,
        executions_per_trial=1,
        max_trials=3,
        activation='relu',
        directory='my_dir',
        loss='categorical_crossentropy',
        metrics='accuracy',
        epochs=10,
        step=32,
        verbose=0,
        test_size=0.2):
    # function build model using hyperparameter
    le = preprocessing.LabelEncoder()
    y = tf.keras.utils.to_categorical(
        le.fit_transform(y), num_classes=num_classes)

    def build_model(hp):
        model = keras.Sequential()
        for i in range(hp.Int('num_layers', min_layers, max_layers)):
            model.add(Dense(units=hp.Int('units_' + str(i),
                                         min_value=min_dense,
                                         max_value=max_dense,
                                         step=step),
                            activation=activation))
        model.add(Dense(num_classes, activation='softmax'))
        model.compile(
            optimizer=keras.optimizers.Adam(
                                       hp.Float('learning_rate',
                                                min_value=1e-5,
                                                max_value=1e-2,
                                                sampling='LOG',
                                                default=1e-3)),
            loss=loss,
            metrics=[metrics])
        return model
    
    # tuners, establish the object to look through the tuner search space
    tuner = RandomSearch(
        build_model,
        objective='val_accuracy',
        max_trials=max_trials,
        executions_per_trial=executions_per_trial,
        directory=directory,
        project_name='class_tuned')

    # tuner.search_space_summary()
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, 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))
    models = tuner.get_best_models(num_models=1)[0]
    hyp = tuner.get_best_hyperparameters(num_trials=1)[0]
    #hyp = tuner.oracle.get_best_trials(num_trials=1)[0].hyperparameters.values
    #best_hps = np.stack(hyp).astype(None)
    history = tuner_hist(
        X,
        y,
        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 array
        history : history of the data executed from the given model
    """
    return models, hyp, history, X_test, y_test
def tuneClass ( X , y , num_classes , max_layers = 10 , min_layers = 2 , min_dense = 32 , max_dense = 512 , executions_per_trial = 1 , max_trials = 3 , activation = 'relu' , directory = 'my_dir' , loss = 'categorical_crossentropy' , metrics = 'accuracy' , epochs = 10 , step = 32 , verbose = 0 , test_size = 0.2 ) : # function build model using hyperparameter le = preprocessing . LabelEncoder ( ) y = tf . keras . utils . to_categorical ( le . fit_transform ( y ) , num_classes = num_classes ) def build_model ( hp ) : model = keras . Sequential ( ) for i in range ( hp . Int ( 'num_layers' , min_layers , max_layers ) ) : model . add ( Dense ( units = hp . Int ( 'units_' + str ( i ) , min_value = min_dense , max_value = max_dense , step = step ) , activation = activation ) ) model . add ( Dense ( num_classes , activation = 'softmax' ) ) model . compile ( optimizer = keras . optimizers . Adam ( hp . Float ( 'learning_rate' , min_value = 1e-5 , max_value = 1e-2 , sampling = 'LOG' , default = 1e-3 ) ) , loss = loss , metrics = [ metrics ] ) return model # tuners, establish the object to look through the tuner search space tuner = RandomSearch ( build_model , objective = 'val_accuracy' , max_trials = max_trials , executions_per_trial = executions_per_trial , directory = directory , project_name = 'class_tuned' ) # tuner.search_space_summary() X_train , X_test , y_train , y_test = train_test_split ( X , y , 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 ) ) models = tuner . get_best_models ( num_models = 1 ) [ 0 ] hyp = tuner . get_best_hyperparameters ( num_trials = 1 ) [ 0 ] #hyp = tuner.oracle.get_best_trials(num_trials=1)[0].hyperparameters.values #best_hps = np.stack(hyp).astype(None) history = tuner_hist ( X , y , tuner , hyp , epochs = epochs , verbose = verbose , test_size = test_size ) return models , hyp , history , X_test , y_test