Skip to content

Single Worker

Basic single worker setup#

A basic setup for optimizing can be done as follows. Please note, that this is example should solely show a simple setup of dehb. More in-depth examples can be found in the examples folder. First we need to setup a ConfigurationSpace, from which Configurations will be sampled:

Configuration Space
from ConfigSpace import ConfigurationSpace, Configuration

cs = ConfigurationSpace({"x0": (3.0, 10.0), "x1": ["red", "green"]})
print(cs)
Configuration space object:
  Hyperparameters:
    x0, Type: UniformFloat, Range: [3.0, 10.0], Default: 6.5
    x1, Type: Categorical, Choices: {red, green}, Default: red

Next, we need an object_function, which we are aiming to optimize:

Configuration Space
import numpy as np

def objective_function(x: Configuration, fidelity: float, **kwargs):
    # Replace this with your actual objective value (y) and cost.
    cost = (10 if x["x1"] == "red" else 100) + fidelity
    y = x["x0"] + np.random.uniform()
    return {"fitness": y, "cost": x["x0"]}

sample_config = cs.sample_configuration()
print(sample_config)

result = objective_function(sample_config, fidelity=10)
print(result)
Configuration(values={
  'x0': 6.688912745377312,
  'x1': 'red',
})
{'fitness': 6.704499334151075, 'cost': 6.688912745377312}

Finally, we can setup our optimizer and run DEHB:

Configuration Space
from dehb import DEHB

dim = len(cs.get_hyperparameters())
optimizer = DEHB(
    f=objective_function,
    cs=cs,
    dimensions=dim,
    min_fidelity=3,
    max_fidelity=27,
    eta=3,
    n_workers=1,
    output_path="./logs",
)

# Run optimization for 1 bracket. Output files will be saved to ./logs
traj, runtime, history = optimizer.run(brackets=1, verbose=True)
config_id, config, fitness, runtime, fidelity, _ = history[0]
print("config", config)
print("fitness", fitness)
print("runtime", runtime)
print("fidelity", fidelity)
config [0.6689425911404523, 0.0]
fitness 8.672768001442854
runtime 7.682598137983166
fidelity 3.0