-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement possibility to run hyperparameter opts in the strategy (#287)
- Loading branch information
Showing
33 changed files
with
849 additions
and
741 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from bofire.runners.hyperoptimize import hyperoptimize | ||
from bofire.runners.run import run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import warnings | ||
from typing import Optional, Tuple | ||
|
||
import pandas as pd | ||
|
||
import bofire.strategies.api as strategies | ||
from bofire.benchmarks.api import Hyperopt | ||
from bofire.data_models.domain.api import Domain | ||
from bofire.data_models.enum import RegressionMetricsEnum | ||
from bofire.data_models.objectives.api import MinimizeObjective | ||
from bofire.data_models.strategies.api import ( | ||
FactorialStrategy, | ||
RandomStrategy, | ||
SoboStrategy, | ||
) | ||
from bofire.data_models.surrogates.api import AnyTrainableSurrogate | ||
from bofire.runners.run import run | ||
|
||
|
||
def hyperoptimize( | ||
surrogate_data: AnyTrainableSurrogate, | ||
training_data: pd.DataFrame, | ||
folds: int, | ||
random_state: Optional[int] = None, | ||
) -> Tuple[AnyTrainableSurrogate, pd.DataFrame]: | ||
if surrogate_data.hyperconfig is None: | ||
warnings.warn( | ||
"No hyperopt is possible as no hyperopt config is available. Returning initial config." | ||
) | ||
return surrogate_data, pd.DataFrame({e.name: [] for e in RegressionMetricsEnum}) | ||
|
||
def best(domain: Domain, experiments: pd.DataFrame) -> float: | ||
return ( | ||
experiments[domain.outputs[0].key].min() | ||
if isinstance(domain.outputs[0].objective, MinimizeObjective) | ||
else experiments[domain.outputs[0].key].max() | ||
) | ||
|
||
def sample(domain): | ||
datamodel = RandomStrategy(domain=domain) | ||
sampler = strategies.map(data_model=datamodel) | ||
sampled = sampler.ask(len(domain.inputs) + 1) | ||
return sampled | ||
|
||
benchmark = Hyperopt( | ||
surrogate_data=surrogate_data, | ||
training_data=training_data, | ||
folds=folds, | ||
random_state=random_state, | ||
) | ||
|
||
if surrogate_data.hyperconfig.hyperstrategy == "FactorialStrategy": # type: ignore | ||
strategy = strategies.map(FactorialStrategy(domain=benchmark.domain)) | ||
experiments = benchmark.f( | ||
strategy.ask(candidate_count=None), return_complete=True | ||
) | ||
else: | ||
experiments = run( | ||
benchmark=benchmark, | ||
strategy_factory=RandomStrategy | ||
if surrogate_data.hyperconfig.hyperstrategy == "RandomStrategy" # type: ignore | ||
else SoboStrategy, # type: ignore | ||
metric=best, | ||
n_runs=1, | ||
n_iterations=surrogate_data.hyperconfig.n_iterations # type: ignore | ||
- len(benchmark.domain.inputs) | ||
- 1, | ||
initial_sampler=sample, | ||
n_procs=1, | ||
)[0][0] | ||
|
||
# analyze the results and get the best | ||
experiments = experiments.sort_values( | ||
by=benchmark.target_metric.name, | ||
ascending=True | ||
if isinstance(benchmark.domain.outputs[0].objective, MinimizeObjective) | ||
else False, | ||
) | ||
|
||
surrogate_data.update_hyperparameters(experiments.iloc[0]) | ||
|
||
return ( | ||
surrogate_data, | ||
experiments[ | ||
surrogate_data.hyperconfig.domain.inputs.get_keys() | ||
+ [e.name for e in RegressionMetricsEnum] | ||
], | ||
) |
Oops, something went wrong.