-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from HideakiImamura/add-hebo-package
Add HEBO sampler
- Loading branch information
Showing
7 changed files
with
191 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Hideaki Imamura | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,42 @@ | ||
--- | ||
author: HideakiImamura | ||
title: HEBO (Heteroscedastic and Evolutionary Bayesian Optimisation) | ||
description: HEBO addresses the problem of noisy and heterogeneous objective functions by using a heteroscedastic Gaussian process and an evolutionary algorithm. | ||
tags: ["sampler", "Bayesian optimization", "Heteroscedastic Gaussian process", "Evolutionary algorithm"] | ||
optuna_versions: ["3.6.1"] | ||
license: "MIT License" | ||
--- | ||
|
||
## Class or Function Names | ||
- HEBOSampler | ||
|
||
## Installation | ||
```bash | ||
pip install -r requirements.txt | ||
git clone [email protected]:huawei-noah/HEBO.git | ||
cd HEBO/HEBO | ||
pip install -e . | ||
``` | ||
|
||
## Example | ||
```python | ||
search_space = { | ||
"x": FloatDistribution(-10, 10), | ||
"y": IntDistribution(0, 10), | ||
|
||
} | ||
sampler = HEBOSampler(search_space) | ||
study = optuna.create_study(sampler=sampler) | ||
``` | ||
See [`example.py`](https://github.com/optuna/optunahub-registry/blob/main/package/samplers/hebo/example.py) for a full example. | ||
![History Plot](images/hebo_optimization_history.png "History Plot") | ||
|
||
|
||
## Others | ||
|
||
HEBO is the winning submission to the [NeurIPS 2020 Black-Box Optimisation Challenge](https://bbochallenge.com/leaderboard). | ||
Please refer to [the official repository of HEBO](https://github.com/huawei-noah/HEBO/tree/master/HEBO) for more details. | ||
|
||
### Reference | ||
|
||
Cowen-Rivers, Alexander I., et al. "An Empirical Study of Assumptions in Bayesian Optimisation." arXiv preprint arXiv:2012.03826 (2021). |
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,4 @@ | ||
from .sampler import HEBOSampler | ||
|
||
|
||
__all__ = ["HEBOSampler"] |
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,27 @@ | ||
import optuna | ||
import optunahub | ||
|
||
|
||
module = optunahub.load_module("samplers/hebo") | ||
HEBOSampler = module.HEBOSampler | ||
|
||
|
||
def objective(trial: optuna.trial.Trial) -> float: | ||
x = trial.suggest_float("x", -10, 10) | ||
y = trial.suggest_int("y", -10, 10) | ||
return x**2 + y**2 | ||
|
||
|
||
if __name__ == "__main__": | ||
sampler = HEBOSampler( | ||
{ | ||
"x": optuna.distributions.FloatDistribution(-10, 10), | ||
"y": optuna.distributions.IntDistribution(-10, 10), | ||
} | ||
) | ||
study = optuna.create_study(sampler=sampler) | ||
study.optimize(objective, n_trials=100) | ||
print(study.best_trial.params) | ||
|
||
fig = optuna.visualization.plot_optimization_history(study) | ||
fig.write_image("hebo_optimization_history.png") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,3 @@ | ||
optuna | ||
optunahub | ||
hebo@git+https://github.com/huawei-noah/HEBO.git#subdirectory=HEBO |
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,94 @@ | ||
from __future__ import annotations | ||
|
||
from optuna.distributions import BaseDistribution | ||
from optuna.distributions import CategoricalDistribution | ||
from optuna.distributions import FloatDistribution | ||
from optuna.distributions import IntDistribution | ||
from optuna.study import Study | ||
from optuna.trial import FrozenTrial | ||
import optunahub | ||
|
||
from hebo.design_space.design_space import DesignSpace | ||
from hebo.optimizers.hebo import HEBO | ||
|
||
|
||
SimpleSampler = optunahub.load_module("samplers/simple").SimpleSampler | ||
|
||
|
||
class HEBOSampler(SimpleSampler): # type: ignore | ||
def __init__(self, search_space: dict[str, BaseDistribution]) -> None: | ||
super().__init__(search_space) | ||
self._hebo = HEBO(self._convert_to_hebo_design_space(search_space)) | ||
|
||
def sample_relative( | ||
self, study: Study, trial: FrozenTrial, search_space: dict[str, BaseDistribution] | ||
) -> dict[str, float]: | ||
params_pd = self._hebo.suggest() | ||
|
||
params = {} | ||
for name in search_space.keys(): | ||
params[name] = params_pd[name].to_numpy()[0] | ||
return params | ||
|
||
def _convert_to_hebo_design_space( | ||
self, search_space: dict[str, BaseDistribution] | ||
) -> DesignSpace: | ||
design_space = [] | ||
for name, distribution in search_space.items(): | ||
if isinstance(distribution, FloatDistribution) and not distribution.log: | ||
design_space.append( | ||
{ | ||
"name": name, | ||
"type": "num", | ||
"lb": distribution.low, | ||
"ub": distribution.high, | ||
} | ||
) | ||
elif isinstance(distribution, FloatDistribution) and distribution.log: | ||
design_space.append( | ||
{ | ||
"name": name, | ||
"type": "pow", | ||
"lb": distribution.low, | ||
"ub": distribution.high, | ||
} | ||
) | ||
elif isinstance(distribution, IntDistribution) and distribution.log: | ||
design_space.append( | ||
{ | ||
"name": name, | ||
"type": "pow_int", | ||
"lb": distribution.low, | ||
"ub": distribution.high, | ||
} | ||
) | ||
elif isinstance(distribution, IntDistribution) and distribution.step: | ||
design_space.append( | ||
{ | ||
"name": name, | ||
"type": "step_int", | ||
"lb": distribution.low, | ||
"ub": distribution.high, | ||
"step": distribution.step, | ||
} | ||
) | ||
elif isinstance(distribution, IntDistribution): | ||
design_space.append( | ||
{ | ||
"name": name, | ||
"type": "int", | ||
"lb": distribution.low, | ||
"ub": distribution.high, | ||
} | ||
) | ||
elif isinstance(distribution, CategoricalDistribution): | ||
design_space.append( | ||
{ | ||
"name": name, | ||
"type": "cat", | ||
"categories": distribution.choices, | ||
} | ||
) | ||
else: | ||
raise NotImplementedError(f"Unsupported distribution: {distribution}") | ||
return DesignSpace().parse(design_space) |