2

I an trying implement both classification problem and the regression problem with Keras tuner. here is my code for the regression problem:

    def build_model(hp):
        model = keras.Sequential()
        for i in range(hp.Int('num_layers', 2, 20)):
            model.add(layers.Dense(units=hp.Int('units_' + str(i),
                                                min_value=32,
                                                max_value=512,
                                                step=32),
                                   activation='relu'))
            if hp.Boolean("dropout"):
              model.add(layers.Dropout(rate=0.5))
        # Tune whether to use dropout.
    
        model.add(layers.Dense(1, activation='linear'))
        model.compile(
            optimizer=keras.optimizers.Adam(
                hp.Choice('learning_rate', [1e-4, 1e-3, 1e-5])),
            loss='mean_absolute_error',
            metrics=['mean_absolute_error'])
        return model
tuner = RandomSearch(
    build_model,
    objective='val_mean_absolute_error',
    max_trials=5,
    executions_per_trial=2,
    # overwrite=True,
    directory='projects',
    project_name='Air Quality Index')

In order to apply this code for a classification problem, which parameters(loss, objective, metrices etc.) have to be changed?

Rezuana Haque
  • 608
  • 4
  • 14

1 Answers1

0

To use this code for a classification problem, you will have to change the loss function, the objective function and the activation function of your output layer. Depending on the number of classes, you will use different functions:

Number of classes Two More than two
Loss binary_crossentropy categorical_crossentropy
Tuner objective val_binary_crossentropy val_categorical_crossentropy
Last layer activation sigmoid softmax
leleogere
  • 908
  • 2
  • 15