Keras 3 API documentation / KerasTuner / HyperModels / The base HyperModel class

The base HyperModel class


HyperModel class

keras_tuner.HyperModel(name=None, tunable=True)

Defines a search space of models.

A search space is a collection of models. The build function will build one of the models from the space using the given HyperParameters object.

Users should subclass the HyperModel class to define their search spaces by overriding build(), which creates and returns the Keras model. Optionally, you may also override fit() to customize the training process of the model.


In build(), you can create the model using the hyperparameters.

class MyHyperModel(kt.HyperModel):
    def build(self, hp):
        model = keras.Sequential()
            hp.Choice('units', [8, 16, 32]),
        model.add(keras.layers.Dense(1, activation='relu'))
        return model

When overriding, if you use to train your model, which returns the training history, you can return it directly. You may use hp to specify any hyperparameters to tune.

class MyHyperModel(kt.HyperModel):
    def build(self, hp):

    def fit(self, hp, model, *args, **kwargs):
            epochs=hp.Int("epochs", 5, 20),

If you have a customized training process, you can return the objective value as a float.

If you want to keep track of more metrics, you can return a dictionary of the metrics to track.

class MyHyperModel(kt.HyperModel):
    def build(self, hp):

    def fit(self, hp, model, *args, **kwargs):
        return {
            "loss": loss,
            "val_loss": val_loss,
            "val_accuracy": val_accuracy


  • name: Optional string, the name of this HyperModel.
  • tunable: Boolean, whether the hyperparameters defined in this hypermodel should be added to search space. If False, either the search space for these parameters must be defined in advance, or the default values will be used. Defaults to True.


build method

Builds a model.


  • hp: A HyperParameters instance.


A model instance.