pythontensorflowkeras

Keras Tuner - Model-building function did not return a valid Keras Model instance


I'm trying to search Hyperparameters for a model using Keras Tuner, but I'm getting this error when I run the code: "RuntimeError: Model-building function did not return a valid Keras Model instance, found < keras.engine.sequential.Sequential object at 0x000001E9C2903F28 >"

I've searched on Internet but didn't found anything that could help, also I've followed the tutorial in the Keras Tuner gitHub page (https://github.com/keras-team/keras-tuner), but it dind't work either.

Here is my code:

class MyHyperModel(HyperModel):

    def __init__(self, num_classes):
        self.num_classes = num_classes

    def build(self, hp):
        model=Sequential()
        model.add(Dense(units=hp.Int('units_0', 30, 900, step=30),
                        activation=hp.Choice('act_0', ['relu', 'tanh']),
                        input_dim=12))
        for i in range(hp.Int('layers', 3, 9)):
            model.add(Dense(units=hp.Int('units_' + str(i), 30, 900, step=30),
                            activation=hp.Choice('act_' + str(i), ['relu', 'tanh'])))
        model.add(Dense(6, activation='softmax'))
        model.compile(loss='categorical_crossentropy',
                        optimizer=hp.Choice('optimizer', ['adam', 'sgd']),
                        metrics=['categorical_accuracy'])
        return model


hypermodel = MyHyperModel(num_classes=6)

tuner = kt.tuners.bayesian.BayesianOptimization(
    hypermodel,
    objective='val_accuracy',
    max_trials=5,
    executions_per_trial=3,
    seed=(np.random.seed(1)),
    directory='Tests',
    project_name='test')

tuner.search_space_summary()

tuner.search(data[:200], labels[:200],
             verbose=2,
             epochs=3,
             validation_data=(data[200:], labels[200:]))

models = tuner.get_best_models(num_models=2).summary()
tuner.get_best_hyperparameters()[0].values
tuner.results_summary()

data is an list of 300 vector with 12 values and on labels there are 6 classes which was converted to tensor with the function tensorflow.convert_to_tensor().

I appreciate any help.


Solution

  • I know what's wrong and is not the code, my model have 6 neurons on the last layer and I've used the loss as 'categorical_crossentropy', but this only works when the labels are 0 and 1, so I've changed the loss to 'sparse_categorical_crossentropy' and metrics to 'accuracy' and it worked. Thanks everyone for the reply, I appreciate the help.