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

Use SimpleBaseSampler instead of SimpleSampler in tutorial #85

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions package/samplers/simple/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,3 @@ def _default_sample_independent(
return self._random_sampler.sample_independent(
study, trial, param_name, param_distribution
)


class SimpleSampler(SimpleBaseSampler):
pass
14 changes: 10 additions & 4 deletions recipes/001_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@
# `force_reload=True` argument forces downloading the sampler from the registry.
# If we set `force_reload` to `False`, we use the cached data in our local storage if available.

SimpleSampler = optunahub.load_module("samplers/simple").SimpleSampler
SimpleBaseSampler = optunahub.load_module("samplers/simple").SimpleBaseSampler


class MySampler(SimpleSampler): # type: ignore
class MySampler(SimpleBaseSampler): # type: ignore
# By default, search space will be estimated automatically like Optuna's built-in samplers.
# You can fix the search spacd by `search_space` argument of `SimpleSampler` class.
def __init__(self, search_space: dict[str, optuna.distributions.BaseDistribution]) -> None:
def __init__(
self, search_space: dict[str, optuna.distributions.BaseDistribution] | None = None
) -> None:
super().__init__(search_space)
self._rng = np.random.RandomState()

Expand All @@ -67,6 +69,10 @@ def sample_relative(
# `search_space` argument must be identical to `search_space` argument input to `__init__` method.
# This method is automatically invoked by Optuna and `SimpleBaseSampler`.

# If search space is empty, all parameter values are sampled randomly by SimpleBaseSampler.
if search_space == {}:
return {}

params = {} # type: dict[str, Any]
for n, d in search_space.items():
if isinstance(d, optuna.distributions.FloatDistribution):
Expand All @@ -92,7 +98,7 @@ def objective(trial: optuna.trial.Trial) -> float:
###################################################################################################
# This sampler can be used in the same way as other Optuna samplers.
# In the following example, we create a study and optimize it using `MySampler` class.
sampler = MySampler({"x": optuna.distributions.FloatDistribution(-10, 10)})
sampler = MySampler()
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=100)

Expand Down