1. DeepHyper 101#

Open In Colab

In this tutorial, we present the basics of DeepHyper.

Let us start with installing DeepHyper!

    import deephyper
except (ImportError, ModuleNotFoundError):
    !pip install deephyper

1.1. Optimization Problem#

In the definition of our optimization problem we have two components:

  1. black-box function that we want to optimize

  2. the search space of input variables

1.1.1. Black-Box Function#

DeepHyper is developed to optimize black-box functions. Here, we define the function \(f(x) = - x ^ 2\) that we want to maximise (the maximum being \(f(x=0) = 0\) on \(I_x = [-10;10]\)). The black-box function f takes as input a config dictionary from which we retrieve the variables of interest.

def f(config):
    return - config["x"]**2

1.1.2. Search Space of Input Variables#

In this example, we have only one variable \(x\) for the black-box functin \(f\). We empirically decide to optimize this variable \(x\) on the interval \(I_x = [-10;10]\). To do so we use the HpProblem from DeepHyper and add a real hyperparameter by using a tuple of two floats.

from deephyper.problem import HpProblem

problem = HpProblem()

# define the variable you want to optimize
problem.add_hyperparameter((-10.0, 10.0), "x")

Configuration space object:
    x, Type: UniformFloat, Range: [-10.0, 10.0], Default: 0.0

1.2. Evaluator Interface#

DeepHyper uses an API called Evaluator to distribute the computation of black-box functions and adapt to different backends (e.g., threads, processes, MPI, Ray). An Evaluator object wraps the black-box function f that we want to optimize. Then a method parameter is used to select the backend and method_kwargs defines some available options of this backend.


The method="thread" provides parallel computation only if the black-box is releasing the global interpretor lock (GIL). Therefore, if you want parallelism in Jupyter notebooks you should use the Ray evaluator (method="ray") after installing Ray with pip install ray.

It is possible to define callbacks to extend the behaviour of Evaluator each time a function-evaluation is launched or completed. In this example we use the TqdmCallback to follow the completed evaluations and the evolution of the objective with a progress-bar.

from deephyper.evaluator import Evaluator
from deephyper.evaluator.callback import TqdmCallback

# define the evaluator to distribute the computation
evaluator = Evaluator.create(
        "num_workers": 4,
        "callbacks": [TqdmCallback()]

print(f"Evaluator has {evaluator.num_workers} available worker{'' if evaluator.num_workers == 1 else 's'}")
Evaluator has 4 available workers
/Users/romainegele/Documents/Argonne/deephyper/deephyper/evaluator/_evaluator.py:101: UserWarning: Applying nest-asyncio patch for IPython Shell!

1.3. Search Algorithm#

The next step is to define the search algorithm that we want to use. Here, we choose CBO (Centralized Bayesian Optimization) which is a sampling based Bayesian optimization strategy. This algorithm has the advantage of being asynchronous thanks to a constant liar strategy which is crutial to keep a good utilization of the resources when the number of available workers increases.

from deephyper.search.hps import CBO

# define your search
search = CBO(problem, evaluator)

Then, we can execute the search for a given number of iterations by using the search.search(max_evals=...). It is also possible to use the timeout parameter if one needs a specific time budget (e.g., restricted computational time in machine learning competitions, allocation time in HPC).

results = search.search(max_evals=100)

Finally, let us visualize the results. The search(...) returns a DataFrame also saved locally under results.csv (in case of crash we don’t want to lose the possibly expensive evaluations already performed).

The DataFrame contains as columns: 1. the optimized hyperparameters: such as x in our case. 2. the id of each evaluated function (increased incrementally following the order of created evaluations). 3. the objective maximised which directly match the results of the \(f\)-function in our example. 4. the objective maximised which directly match the results of the \(f\)-function in our example. 5. the time of creation/collection of each task timestamp_submit and timestamp_gather respectively (in secondes, since the creation of the Evaluator).

x job_id objective timestamp_submit timestamp_gather
0 -0.895066 1 -0.801143 2.160425 2.161010
1 -1.138923 3 -1.297145 2.160442 2.189616
2 -6.183421 2 -38.234694 2.160435 2.190187
3 5.892823 4 -34.725360 2.160447 2.190652
4 8.578443 6 -73.589688 2.204828 2.205327
... ... ... ... ... ...
95 0.028766 95 -0.000827 18.217424 18.219099
96 0.048879 97 -0.002389 19.045161 19.045562
97 0.047353 98 -0.002242 19.045171 19.046206
98 0.051878 100 -0.002691 19.045183 19.046600
99 0.048837 99 -0.002385 19.045177 19.046962

100 rows × 5 columns

We can also plot the evolution of the objective to verify that we converge correctly toward \(0\).

import matplotlib.pyplot as plt

plt.ylabel("$y = f(x)$")