2

I am currently using the KerasTuner to tune my Artificial Neural Network (ANN) deep learning model for a binary classification project (tabular dataset ). Below is my function to build the model:

def build_model(hp):
    
    # Create a Sequential model
    model = tf.keras.Sequential()
    
    # Input Layer: The now model will take as input arrays of shape (None, 67) 
    model.add(tf.keras.Input(shape = (X_train.shape[1],)))
    
    # Tune number of hidden layers and number of neurons 
    for i in range(hp.Int('num_layers', min_value = 1, max_value = 4)):
        hp_units = hp.Int(f'units_{i}', min_value = 64, max_value = 512, step = 5)
        model.add(Dense(units = hp_units, activation = 'relu'))
    
    # Output Layer
    model.add(Dense(units = 1, activation='sigmoid'))
    
    # Compile the model
    hp_learning_rate = hp.Choice('learning_rate', values = [1e-2, 1e-3, 1e-4])
    model.compile(optimizer = keras.optimizers.Adam(learning_rate = hp_learning_rate), 
                  loss = keras.losses.BinaryCrossentropy(), 
                  metrics = ["accuracy"]
                 )
    
    return model

Codes of creating tuner:

import os

# HyperBand algorithm from keras tuner
hpb_tuner = kt.Hyperband(
    hypermodel = build_model, 
    objective = 'val_accuracy', 
    max_epochs = 500, 
    seed = 42,
    executions_per_trial = 3,
    directory = os.getcwd(),
    project_name = "Medical Claim (ANN)",
)

hpb_tuner.search_space_summary()

The best result shows that I have to use 3 hidden layers. However, why there is a total of 4 hidden layers shown? enter image description here

If I didn't misunderstand, the num_layers parameter indicates how many hidden layers I have to use in my ANN, and parameters units_0 to units_3 indicate how many neurons I have to use in each hidden layer where units_0 refers to the first hidden layer, units_1 refers to the second hidden layer and so forth. The input layer of my ANN should equal the number of features in my dataset which is 67 as shown in my code above (within the build_model function), so I believe the units_0 does not refer to the number of neurons in the input layer.

Is there something wrong with my code? Hope any gurus here can solve my doubt and problem!

Ming Jun Lim
  • 134
  • 2
  • 12

0 Answers0