Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HEBO sampler supports Define-by-Run manner, maximization, parallelization, and constant_liar #196

Merged
merged 28 commits into from
Dec 10, 2024

Conversation

eukaryo
Copy link
Contributor

@eukaryo eukaryo commented Dec 4, 2024

Contributor Agreements

Please read the contributor agreements and if you agree, please click the checkbox below.

  • I agree to the contributor agreements.

Tip

Please follow the Quick TODO list to smoothly merge your PR.

Motivation

I would like to see a HEBO implementation that can be a drop-in replacement of Samplers in Optuna code that is written in Define-by-Run (its search space is not conditional).

I also want it to support parallelization.

Description of the changes

Modified existing HEBO package.

TODO List towards PR Merge

Please remove this section if this PR is not an addition of a new package.
Otherwise, please check the following TODO list:

  • Copy ./template/ to create your package
  • Replace <COPYRIGHT HOLDER> in LICENSE of your package with your name
  • Fill out README.md in your package
  • Add import statements of your function or class names to be used in __init__.py
  • (Optional) Add from __future__ import annotations at the head of any Python files that include typing to support older Python versions
  • Apply the formatter based on the tips in README.md
  • Check whether your module works as intended based on the tips in README.md

@eukaryo
Copy link
Contributor Author

eukaryo commented Dec 4, 2024

#195
In the previous PR, I tried making another package, and some reviews were done.

@y0z
Copy link
Member

y0z commented Dec 4, 2024

  • Could you provide the brief API documentation in README.md based on the latest README template?
  • Making the example executable by copying and pasting it would be also helpful.

Current example (L50-L58).

search_space = {
    "x": FloatDistribution(-10, 10),
    "y": IntDistribution(0, 10),
}
sampler = HEBOSampler(search_space)
study = optuna.create_study(sampler=sampler)

Suggestion.

import optuna
import optunahub


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


module = optunahub.load_module("samplers/hebo")
sampler = module.HEBOSampler(search_space={
    "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, study.best_trial.value)

@y0z y0z requested review from y0z and nabenabe0928 December 4, 2024 11:59
@y0z y0z removed request for y0z and nabenabe0928 December 4, 2024 11:59
@y0z
Copy link
Member

y0z commented Dec 5, 2024

I've updated my comment (#196 (comment)).

Copy link
Member

@y0z y0z left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nabenabe0928 If you have no further concerns, please merge this PR.

@eukaryo
Copy link
Contributor Author

eukaryo commented Dec 5, 2024

Note: I added "stateless" mode for parallelization; in the mode, it creates entire HEBO model in every trial and thus requires more computational cost. I preserved previous "define-and-run" mode for compartibility. It is not stateless but faster.

Copy link
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR, I left some comments:)

package/samplers/hebo/sampler.py Outdated Show resolved Hide resolved
package/samplers/hebo/sampler.py Outdated Show resolved Hide resolved
package/samplers/hebo/sampler.py Outdated Show resolved Hide resolved
package/samplers/hebo/sampler.py Outdated Show resolved Hide resolved
package/samplers/hebo/sampler.py Outdated Show resolved Hide resolved
package/samplers/hebo/sampler.py Show resolved Hide resolved
package/samplers/hebo/README.md Outdated Show resolved Hide resolved
package/samplers/hebo/README.md Outdated Show resolved Hide resolved
package/samplers/hebo/sampler.py Outdated Show resolved Hide resolved
package/samplers/hebo/README.md Show resolved Hide resolved
@eukaryo
Copy link
Contributor Author

eukaryo commented Dec 10, 2024

@nabenabe0928 Thank you for comments! I revised the code. PTAL.

Copy link
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some comments, but you do not have to address all!
In the worst case, we can once merge this PR and can make another PR!

package/samplers/hebo/sampler.py Outdated Show resolved Hide resolved
package/samplers/hebo/sampler.py Outdated Show resolved Hide resolved
package/samplers/hebo/sampler.py Outdated Show resolved Hide resolved
package/samplers/hebo/sampler.py Outdated Show resolved Hide resolved
@eukaryo
Copy link
Contributor Author

eukaryo commented Dec 10, 2024

Thank you for additional comments!
I checked the current code works in the following senarios: (1)search_space is given (2) search_space=None (3) search_space=None, constant_liar=True, and n_jobs=3.
@nabenabe0928 would you please merge this PR if you have no further critical concerns?

Copy link
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I will check if the performance is fine just in case!

params = pd.DataFrame([t.params for t in trials])
values[np.isnan(values)] = worst_value
values *= sign
hebo.observe(params, values)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check if the shape is correct here (seems (1, N) is correct?)
Anyways, I will benchmark your code and merge this PR!

Copy link
Contributor Author

@eukaryo eukaryo Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! fyi: I confirmed that simply changing line134 to hebo.observe(params, values[None, :]) or hebo.observe(params, values[None, :].T) fails.

@nabenabe0928 nabenabe0928 merged commit 035036e into optuna:main Dec 10, 2024
4 checks passed
@nabenabe0928
Copy link
Contributor

I confirmed it works!

@eukaryo eukaryo deleted the hebo-package-improvement branch December 10, 2024 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants