Published on Monday 28 March 2022
[SOLVED] Hyperopt Early Stopping
Hyperopt is used for black-box optimization. Let's see how to set up conditions for early stopping once results are satisfying enough.
In a previous post, we've seen how to use Hyperopt to carry out the bayesian optimization.
But is it possible to set an early stopping condition to break the loop before reaching max number of iterations? The answer is yes, using the early_stop_fn parameter.
Let's see how it works with an example. The following code shows how to stop the min search as soon as the best loss drops below -200.
from hyperopt import fmin, hp, tpe
def objFnc(paramList):
# Implement your loss function body here
# ...
return myLoss
def customStopCondition(x, *kwargs):
return x.best_trial["result"]["loss"] < -200, kwargs
# Define the search space here:
# searchSpace = ...
x_mins_dict = fmin(
fn = objFnc,
space = searchSpace,
algo = tpe.suggest,
max_evals = 100,
verbose = 2,
early_stop_fn = customStopCondition
)
That's it!
Note: don't forget to return kwargs in customStopCondition, otherwise you could get a TypeError: cannot unpack non-iterable bool object.