pythontensorflowkerashyperparameters

Stop Keras Tuner if it has found a good configuration


I know that I can stop single trials using EarlyStopping or special callbacks if the accuracy is high enough, but is there a way to stop the whole hyperparameter tuning in that case?

        tuner = RandomSearch(
            hypermodel=model,
            objective=Objective(config.metric, direction=config.metric_direction),
            max_trials=config.max_trials,
            overwrite=False,
            directory=config.log_directory,
            project_name=config.project_name,
        )

        tuner.search(
            x=X_train,
            y=y_train,
            epochs=config.epochs,
            validation_data=data_test,
            callbacks=callbacks,  # This contains EarlyStopping and a callback that terminates when a certain acc has been reached
            verbose=1,
            class_weight=class_weights,
        )

Solution

  • Okay, there is a solution:

    If you subclass the tuner class (e.g. RandomSearch), you can set a flag in on_epoch_end when the desired accuracy is reached.

    If you then overwrite the search function, you can interrupt the while loop as soon as the flag is set.