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

Should accept Mapping as param_distributions in OptunaSearchCV #171

Open
yu9824 opened this issue Oct 22, 2024 · 0 comments
Open

Should accept Mapping as param_distributions in OptunaSearchCV #171

yu9824 opened this issue Oct 22, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@yu9824
Copy link
Contributor

yu9824 commented Oct 22, 2024

Expected behavior

Should accept Mapping as param_distributions in OptunaSearchCV.

A typing of param_distributions is Mapping;

param_distributions: Mapping[str, distributions.BaseDistribution],

However, if we assign Mapping other than dict (e.g. MappingProxyType), it throws an error.

Environment

  • Optuna version:3.5.0
  • Optuna Integration version:3.5.0
  • Python version:3.11.6
  • OS:macOS-14.7-arm64-arm-64bit

Error messages, stack traces, or logs

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 6
      3 import optuna
      4 from sklearn.cross_decomposition import PLSRegression
----> 6 ocv = optuna.integration.OptunaSearchCV(
      7     PLSRegression(),
      8     param_distributions=MappingProxyType(
      9         dict(n_components=optuna.distributions.IntDistribution(1, 10))
     10     ),
     11 )

File ~/miniforge3/envs/py311/lib/python3.11/site-packages/optuna/_experimental.py:114, in experimental_class.<locals>.decorator.<locals>.wrapper.<locals>.wrapped_init(self, *args, **kwargs)
    103 @functools.wraps(_original_init)
    104 def wrapped_init(self: Any, *args: Any, **kwargs: Any) -> None:
    105     warnings.warn(
    106         "{} is experimental (supported from v{}). "
    107         "The interface can change in the future.".format(
   (...)
    111         stacklevel=2,
    112     )
--> 114     _original_init(self, *args, **kwargs)

File ~/miniforge3/envs/py311/lib/python3.11/site-packages/optuna/integration/sklearn.py:731, in OptunaSearchCV.__init__(self, estimator, param_distributions, cv, enable_pruning, error_score, max_iter, n_jobs, n_trials, random_state, refit, return_train_score, scoring, study, subsample, timeout, verbose, callbacks)
    728 _imports.check()
    730 if not isinstance(param_distributions, dict):
--> 731     raise TypeError("param_distributions must be a dictionary.")
    733 # Rejecting deprecated distributions as they may cause cryptic error
    734 # when cloning OptunaSearchCV instance.
    735 # https://github.com/optuna/optuna/issues/4084
    736 for key, dist in param_distributions.items():

TypeError: param_distributions must be a dictionary.

Steps to reproduce

from types import MappingProxyType

import optuna
from sklearn.cross_decomposition import PLSRegression

ocv = optuna.integration.OptunaSearchCV(
    PLSRegression(),
    param_distributions=MappingProxyType(
        dict(n_components=optuna.distributions.IntDistribution(1, 10))
    ),
)

Additional context (optional)

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant