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

[Bug] ValueError: The dtype of argument operate_on needs to be int, but is int64 #1042

Open
Shakesbeery opened this issue Jun 14, 2023 · 1 comment · Fixed by #1044
Open
Assignees
Labels

Comments

@Shakesbeery
Copy link

Description

I should be able to instantiate a BlackBoxFacade for use in optimization, but it fails on a toy example.

Steps/Code to Reproduce

Minimum example to reproduce:

from smac import Scenario
from smac import HyperparameterOptimizationFacade, BlackBoxFacade

from ConfigSpace import Categorical, Float, Integer, ConfigurationSpace

def foobar(cost):
    return cost

config_space = ConfigurationSpace()
config_space.add_hyperparameter(Float(**{"name": "x1", "bounds": [-10, 10]}))
config_space.add_hyperparameter(Float(**{"name": "x2", "bounds": [-10, 10]}))

config_dict = {
        "n_trials": 1,
        "name": "error_run",
        "seed": 1
    }
scenario = Scenario(config_space, **config_dict)

optimizer = BlackBoxFacade
acq = optimizer.get_acquisition_function(scenario)
intensifier = optimizer.get_intensifier(scenario, max_config_calls=1)
optimizer = optimizer(scenario, foobar, acquisition_function=acq,
                    intensifier=intensifier, overwrite=True)

Expected Results

It should work.

Actual Results

ValueError                                Traceback (most recent call last)
Cell In[1], line 23
     21 acq = optimizer.get_acquisition_function(scenario)
     22 intensifier = optimizer.get_intensifier(scenario, max_config_calls=1)
---> 23 optimizer = optimizer(scenario, foobar, acquisition_function=acq,
     24                     intensifier=intensifier, overwrite=True)

File ~\anaconda3\envs\badass\lib\site-packages\smac\facade\abstract_facade.py:125, in AbstractFacade.__init__(self, scenario, target_function, model, acquisition_function, acquisition_maximizer, initial_design, random_design, intensifier, multi_objective_algorithm, runhistory_encoder, config_selector, logging_level, callbacks, overwrite, dask_client)
    122 setup_logging(logging_level)
    124 if model is None:
--> 125     model = self.get_model(scenario)
    127 if acquisition_function is None:
    128     acquisition_function = self.get_acquisition_function(scenario)

File ~\anaconda3\envs\badass\lib\site-packages\smac\facade\blackbox_facade.py:77, in BlackBoxFacade.get_model(scenario, model_type, kernel)
     74     raise ValueError(f"The model_type `{model_type}` is not supported. Choose one of {', '.join(types)}")
     76 if kernel is None:
---> 77     kernel = BlackBoxFacade.get_kernel(scenario=scenario)
     79 if model_type is None or model_type == "vanilla":
     80     return GaussianProcess(
     81         configspace=scenario.configspace,
     82         kernel=kernel,
     83         normalize_y=True,
     84         seed=scenario.seed,
     85     )

File ~\anaconda3\envs\badass\lib\site-packages\smac\facade\blackbox_facade.py:135, in BlackBoxFacade.get_kernel(scenario)
    133 exp_kernel, ham_kernel = 0.0, 0.0
    134 if len(cont_dims) > 0:
--> 135     exp_kernel = MaternKernel(
    136         np.ones([len(cont_dims)]),
    137         [(np.exp(-6.754111155189306), np.exp(0.0858637988771976)) for _ in range(len(cont_dims))],
    138         nu=2.5,
    139         operate_on=cont_dims,
    140     )
    141 if len(cat_dims) > 0:
    142     ham_kernel = HammingKernel(
    143         np.ones([len(cat_dims)]),
    144         [(np.exp(-6.754111155189306), np.exp(0.0858637988771976)) for _ in range(len(cat_dims))],
    145         operate_on=cat_dims,
    146     )

File ~\anaconda3\envs\badass\lib\site-packages\smac\model\gaussian_process\kernels\matern_kernel.py:30, in MaternKernel.__init__(self, length_scale, length_scale_bounds, nu, operate_on, has_conditions, prior)
     21 def __init__(
     22     self,
     23     length_scale: float | tuple[float, ...] | np.ndarray = 1.0,
   (...)
     28     prior: AbstractPrior | None = None,
     29 ) -> None:
---> 30     super().__init__(
     31         operate_on=operate_on,
     32         has_conditions=has_conditions,
     33         prior=prior,
     34         length_scale=length_scale,
     35         length_scale_bounds=length_scale_bounds,
     36         nu=nu,
     37     )

File ~\anaconda3\envs\badass\lib\site-packages\smac\model\gaussian_process\kernels\base_kernels.py:56, in AbstractKernel.__init__(self, operate_on, has_conditions, prior, **kwargs)
     54 self.has_conditions = has_conditions
     55 self.prior = prior
---> 56 self._set_active_dims(operate_on)
     58 # Since this class is a mixin, we just pass all the other parameters to the next class.
     59 super().__init__(**kwargs)

File ~\anaconda3\envs\badass\lib\site-packages\smac\model\gaussian_process\kernels\base_kernels.py:258, in AbstractKernel._set_active_dims(self, operate_on)
    255     raise TypeError("The argument `operate_on` needs to be of type np.ndarray but is %s" % type(operate_on))
    257 if operate_on.dtype != int:
--> 258     raise ValueError("The dtype of argument `operate_on` needs to be int, but is %s" % operate_on.dtype)
    260 self.operate_on = operate_on
    261 self._len_active = len(operate_on)

ValueError: The dtype of argument `operate_on` needs to be int, but is int64

Versions

2.0.1

Fix

I'm currently working through the issue by changing smac\model\gaussian_process\kernels\base_kernels.py:258 from

if operate_on.dtype != int:

to

if not np.issubdtype(operate_on.dtype, np.integer):

This is a broader check against multiple integer types that should all theoretically work within the code base. If you want to prevent negative integers you can always use np.unsignedinteger instead or whatever best fits the ultimate use and possible error field.

@eddiebergman
Copy link
Contributor

Heyo @Shakesbeery,

This has been addressed and will be out whenever the next release is made.

Best,
Eddie

@eddiebergman eddiebergman self-assigned this Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants