pythontensorflowmachine-learningkeraskeras-tuner

Error regarding "accuracy" in hyper-parameter tuning using Keras-tuner


My original MLP model is as follows:

def create_model(n_hidden_1, n_hidden_2, num_classes, num_features):
    # create the model
    model = Sequential()
    model.add(tf.keras.layers.InputLayer(input_shape=(num_features,)))
    model.add(tf.keras.layers.Dense(n_hidden_1, activation='sigmoid'))
    model.add(tf.keras.layers.Dense(n_hidden_2, activation='sigmoid'))
    model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))
    # instantiate the optimizer
    opt = keras.optimizers.SGD(learning_rate=LEARNING_RATE)
    # compile the model
    model.compile(
        optimizer=opt,
        loss="categorical_crossentropy",
        metrics="categorical_accuracy"
    )
    # return model
    return model

In order to tune the above model I created a Keras-tuner model as follows:

def _model(hp):
    model = keras.Sequential()
    model.add(tf.keras.layers.InputLayer(input_shape=(6)))
    model.add(tf.keras.layers.Dense(
            hp.Int("dense_1_units", min_value=32, max_value=2048, step=32, default=128),
            activation="sigmoid"
        ))
    model.add(tf.keras.layers.Dense(
        hp.Int("dense_2_units", min_value=32, max_value=2048, step=32, default=128),
        activation="sigmoid"
    ))
    model.add(tf.keras.layers.Dense(3, activation="softmax"))
    model.compile(
        optimizer=tf.keras.optimizers.SGD(
            hp.Choice("learning_rate", values=[1e-1, 1e-2, 1e-3])
        ),
        loss="categorical_crossentropy",
        metrics="categorical_accuracy"
    )
    return model

   tuner = RandomSearch(
        _model,
        objective="val_accuracy",
        max_trials=10,
        overwrite=True,
        directory="tuner_random_directory",
        project_name="tuner_random_project_name",
    )

I received the following output:

user@server:~/ $ python3 tuner.py

training data size :  1120988
validation data size :  280246
Search space summary
Default search space size: 3
dense_1_units (Int)
{'default': 128, 'conditions': [], 'min_value': 32, 'max_value': 2048, 'step': 32, 'sampling': None}
dense_2_units (Int)
{'default': 128, 'conditions': [], 'min_value': 32, 'max_value': 2048, 'step': 32, 'sampling': None}
learning_rate (Choice)
{'default': 0.1, 'conditions': [], 'values': [0.1, 0.01, 0.001], 'ordered': True}

Search: Running Trial #1

Hyperparameter    |Value             |Best Value So Far
dense_1_units     |1504              |?
dense_2_units     |1440              |?
learning_rate     |0.1               |?

Epoch 1/2
35031/35031 [==============================] - 811s 23ms/step - loss: 0.5475 - categorical_accuracy: 0.7495 - val_loss: 0.5155 - val_categorical_accuracy: 0.7599
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
Epoch 2/2
35031/35031 [==============================] - 807s 23ms/step - loss: 0.5091 - categorical_accuracy: 0.7650 - val_loss: 0.4943 - val_categorical_accuracy: 0.7751
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
Traceback (most recent call last):
  File "my_tuner_app_tuner_random.py", line 229, in <module>
    tuner.search(train_x, train_y, epochs=2, validation_data=(validate_x, validate_y))
  File "/home/user/.local/lib/python3.7/site-packages/keras_tuner/engine/base_tuner.py", line 144, in search
    self.run_trial(trial, *fit_args, **fit_kwargs)
  File "/home/user/.local/lib/python3.7/site-packages/keras_tuner/engine/multi_execution_tuner.py", line 103, in run_trial
    trial.trial_id, metrics=averaged_metrics, step=self._reported_step
  File "/home/user/.local/lib/python3.7/site-packages/keras_tuner/engine/oracle.py", line 224, in update_trial
    self._check_objective_found(metrics)
  File "/home/user/.local/lib/python3.7/site-packages/keras_tuner/engine/oracle.py", line 407, in _check_objective_found
    objective_names, metrics.keys()

ValueError: Objective value missing in metrics reported to the Oracle, expected: ['val_accuracy'], found: dict_keys(['loss', 'categorical_accuracy', 'val_loss', 'val_categorical_accuracy'])

user@server:~/ $

Why am getting one warning and a value error?

How can I resolve these issues?


Solution

  • I can imagine you are getting this warning and error because you have to use exactly the same metric from your model and Keras Tuner model in your RandomSearch target, which is categorical_accuracy. So, perhaps, try:

    tuner = RandomSearch(
            _model,
            objective="val_categorical_accuracy",
            max_trials=10,
            overwrite=True,
            directory="tuner_random_directory",
            project_name="tuner_random_project_name",
    )
    

    since you want to maximize your validation categorical accuracy.