Unknown metric val_accuracy using Keras Tuner Error

I am trying to use a Custom Keras Tuner with fashion-mnist dataset in Google Colab, that’s my code:

!pip install keras-tuner
import tensorflow as tf
import kerastuner
import numpy as np

print("TensorFlow version: ", tf.__version__)

(x_train, y_train) , (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()

def create_model(hp):

  num_hidden_layers = 1
  num_units = 8
  dropout_rate = 0.1
  learning_rate = 0.01

  if hp:
    num_hidden_layers = hp.Choice('num_hidden_layers', values = [1,2,3])
    num_units = hp.Choice('num_units', values = [8,16,32])
    dropout_rate = hp.Float('dropout_rate', min_value = 0.1, max_value = 0.5)
    learning_rate = hp.Float('learning_rate', min_value = 0.0001, max_value = 0.01)

  model = tf.keras.models.Sequential()

  model.add(tf.keras.layers.Flatten(input_shape = (28,28)))
  model.add(tf.keras.layers.Lambda(lambda x: x/255.))

  for _ in range(0, num_hidden_layers):
    model.add(tf.keras.layers.Dense(num_units, activation='relu'))
    model.add(tf.keras.layers.Dropout(dropout_rate))

  model.add(tf.keras.layers.Dense(10, activation='softmax'))

  model.compile(
      loss='sparse_categorical_crossentropy',
      optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
      metrics=['accuracy']
  )

  return model

class CustomTuner(kerastuner.tuners.BayesianOptimization):
  def run_trial(self, trial, *args, **kwargs):
    kwargs['batch_size'] = trial.hyperparameters.Int('batch_size', 32, 128, step=32)
    super(CustomTuner, self).run_trial(trial, *args, **kwargs)

tuner = CustomTuner(
    create_model,
    objective='val_accuracy',
    max_trials = 20,
    directory= 'logs',
    project_name='fashion_minist',
    overwrite=True,
)

search = tuner.search(
    x_train, y_train,
    validation_data=(x_test, y_test),
    epochs = 5,
)

And I’m getting the following error:

ValueError: Unknown metric: val_accuracy

Anything I’m doing wrong? I am getting val_accuracy values in training process, but when the first trial finishes it just stops with this error.

I had made somes tests, and looks that the error is in the CustomTuner, if i try a default tuner it’s working normal

I don’t know if it could be related to:

/cc @haifeng

@Bhack It’s probably related, could be due to the same cause, thanks!