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': 9.8838893125425,
  'x1': 'red',
})
{'fitness': 10.627428649043084, 'cost': 9.8838893125425}

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)
config_id, config, fitness, runtime, fidelity, _ = history[0]
print("config id", config_id)
print("config", config)
print("fitness", fitness)
print("runtime", runtime)
print("fidelity", fidelity)
config id 15
config [0.9912680107186143, 0.0]
fitness 10.54248026149207
runtime 9.9388760750303
fidelity 3.0