ยป Keras API reference / 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 the .build(...) function.

Examples

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

Arguments

  • 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

HyperModel.build(hp)

Builds a model.

Arguments

  • hp: A HyperParameters instance.

Returns

A model instance.