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

Add a sampler that automatically picks a sampler based on developer recommendations #168

Merged
merged 54 commits into from
Oct 31, 2024

Conversation

nabenabe0928
Copy link
Contributor

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

In this PR, we provide a more general sampler for a wider range of users.
By using this sampler, users can use the recommended sampler based on the provided search space without the deep knowledge of black-box optimization.

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

@nabenabe0928
Copy link
Contributor Author

@not522 @toshihikoyanase
Could you review this PR?

@toshihikoyanase
Copy link
Member

I'm sorry, but I'm unavailable at the moment. @c-bata, could you please review this PR? Thank you!

@nabenabe0928
Copy link
Contributor Author

@not522 @c-bata
I addressed all your comments:)
But if you come up with any additional suggestions, please feel free to add them!

Copy link
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

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

Changes look almost good to me. However, it looks that there is a concurrency issue probably caused by GPSampler. Could you take a look please?

import math
import numpy as np
import optuna
import optunahub

optuna.logging.set_verbosity(optuna.logging.DEBUG)


def objective(trial: optuna.Trial) -> float:
    x = 10 ** trial.suggest_float("log10_x", -5, 0)
    y = 2 ** trial.suggest_float("log2_y", 1, 10)
    return x**2 + y**2


if __name__ == "__main__":
    module = optunahub.load_local_module(package="samplers/auto_sampler", registry_root="./package")
    sampler = module.AutoSampler()
    study = optuna.create_study(sampler=sampler)
    study.optimize(objective, n_trials=260, n_jobs=10)
    print(study.best_trial.value, study.best_trial.params)
error traces
$ python example_auto_sampler.py
:
[I 2024-10-30 17:55:38,829] Trial 9 finished with value: 5.756463197970189 and parameters: {'log10_x': -0.9301663717569495, 'log2_y': 1.2608608210121335}. Best is trial 9 with value: 5.756463197970189.
[I 2024-10-30 17:55:38,831] Trial 11 finished with value: 1293.2027666641377 and parameters: {'log10_x': -1.5116363820199834, 'log2_y': 5.168365863169032}. Best is trial 9 with value: 5.756463197970189.
[D 2024-10-30 17:55:38,831] Sample trial#12 with GPSampler.
[I 2024-10-30 17:55:38,832] Trial 10 finished with value: 77.03732990068637 and parameters: {'log10_x': -0.8844302887115854, 'log2_y': 3.133583445415238}. Best is trial 9 with value: 5.756463197970189.
[D 2024-10-30 17:55:38,833] Sample trial#21 with GPSampler.
[I 2024-10-30 17:55:38,834] Trial 12 finished with value: 10.630071734643709 and parameters: {'log10_x': -3.288115982382003, 'log2_y': 1.705039695741501}. Best is trial 9 with value: 5.756463197970189.
[D 2024-10-30 17:55:38,835] Sample trial#22 with GPSampler.
[D 2024-10-30 17:55:38,836] Sample trial#23 with GPSampler.
[D 2024-10-30 17:55:38,837] Sample trial#24 with GPSampler.
Traceback (most recent call last):
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
Traceback (most recent call last):
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
Traceback (most recent call last):
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
ValueError: got differing extents in dimension 0 (got 29 and 17)
Traceback (most recent call last):
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
Traceback (most recent call last):
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
Traceback (most recent call last):
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
ValueError: got differing extents in dimension 0 (got 29 and 17)
Traceback (most recent call last):
ValueError: got differing extents in dimension 0 (got 29 and 17)
Exception ignored in: 'scipy.stats._sobol._initialize_v'
Traceback (most recent call last):
Exception ignored in: 'scipy.stats._sobol._initialize_v'
Traceback (most recent call last):
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
Traceback (most recent call last):
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
Traceback (most recent call last):
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
Exception ignored in: 'scipy.stats._sobol._initialize_v'
Traceback (most recent call last):
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
ValueError: got differing extents in dimension 0 (got 29 and 17)
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
ValueError: got differing extents in dimension 0 (got 29 and 17)
  File "<stringsource>", line 1249, in View.MemoryView._err_extents
ValueError: got differing extents in dimension 0 (got 29 and 17)
ValueError: got differing extents in dimension 0 (got 29 and 17)
Exception ignored in: 'scipy.stats._sobol._initialize_v'
Traceback (most recent call last):
ValueError: got differing extents in dimension 0 (got 29 and 17)
ValueError: got differing extents in dimension 0 (got 29 and 17)
Exception ignored in: 'scipy.stats._sobol._initialize_v'
Traceback (most recent call last):
  File "<stringsource>", line 1294, in View.MemoryView.memoryview_copy_contents

:
[D 2024-10-30 17:56:43,980] Sample trial#259 with CmaEsSampler.
[I 2024-10-30 17:56:43,998] Trial 254 finished with value: 22.78847730915046 and parameters: {'log10_x': -5.0, 'log2_y': 2.2551163120253923}. Best is trial 24 with value: 4.0000000001.
[I 2024-10-30 17:56:44,002] Trial 259 finished with value: 34.58067968677882 and parameters: {'log10_x': -1.4001358515209974, 'log2_y': 2.555914119530404}. Best is trial 24 with value: 4.0000000001.
[I 2024-10-30 17:56:44,166] Trial 250 finished with value: 23.150778844607334 and parameters: {'log10_x': -5.0, 'log2_y': 2.266494412355062}. Best is trial 24 with value: 4.0000000001.
[I 2024-10-30 17:56:44,277] Trial 253 finished with value: 23.16306513095228 and parameters: {'log10_x': -5.0, 'log2_y': 2.26687713517129}. Best is trial 24 with value: 4.0000000001.
[I 2024-10-30 17:56:44,305] Trial 251 finished with value: 23.171298911344138 and parameters: {'log10_x': -5.0, 'log2_y': 2.2671335071860943}. Best is trial 24 with value: 4.0000000001.
[I 2024-10-30 17:56:44,345] Trial 249 finished with value: 11.990664074969528 and parameters: {'log10_x': -5.0, 'log2_y': 1.791919828070403}. Best is trial 24 with value: 4.0000000001.
[I 2024-10-30 17:56:44,453] Trial 255 finished with value: 23.06595549414097 and parameters: {'log10_x': -5.0, 'log2_y': 2.2638465756737034}. Best is trial 24 with value: 4.0000000001.
[I 2024-10-30 17:56:44,520] Trial 256 finished with value: 64.4372072980525 and parameters: {'log10_x': 0.0, 'log2_y': 2.993628677716093}. Best is trial 24 with value: 4.0000000001.
[I 2024-10-30 17:56:44,635] Trial 257 finished with value: 23.27703533210791 and parameters: {'log10_x': -5.0, 'log2_y': 2.270417708349971}. Best is trial 24 with value: 4.0000000001.
[I 2024-10-30 17:56:44,742] Trial 258 finished with value: 64.47728862564898 and parameters: {'log10_x': 0.0, 'log2_y': 2.994084300488396}. Best is trial 24 with value: 4.0000000001.
4.0000000001 {'log10_x': -5.0, 'log2_y': 1.0}

@c-bata
Copy link
Member

c-bata commented Oct 31, 2024

Changes look almost good to me. However, it looks that there is a concurrency issue probably caused by GPSampler. Could you take a look please?

import math
import numpy as np
import optuna
import optunahub

optuna.logging.set_verbosity(optuna.logging.DEBUG)


def objective(trial: optuna.Trial) -> float:
    x = 10 ** trial.suggest_float("log10_x", -5, 0)
    y = 2 ** trial.suggest_float("log2_y", 1, 10)
    return x**2 + y**2


if __name__ == "__main__":
    module = optunahub.load_local_module(package="samplers/auto_sampler", registry_root="./package")
    sampler = module.AutoSampler()
    study = optuna.create_study(sampler=sampler)
    study.optimize(objective, n_trials=260, n_jobs=10)
    print(study.best_trial.value, study.best_trial.params)

I opened a pull request to address this concurrency issue.
optuna/optuna#5737

Copy link
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

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

LGTM!

@not522 not522 merged commit 6024f49 into optuna:main Oct 31, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-package New packages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants