SheffieldML / GPyOpt

Gaussian Process Optimization using GPy
BSD 3-Clause "New" or "Revised" License
927 stars 261 forks source link

Mixed 4-D Domain prediction #296

Open chengymo opened 4 years ago

chengymo commented 4 years ago

Hi,

I am currently trying to run optimization on Gpyopt in a 4-D mixed domain with bound definition:

mixed_domain = [{'name': 'Defect type', 'type': 'categorical', 'domain': range(len(unique_plane)),'dimensionality': 1},
                    {'name': 'Defect Row', 'type': 'discrete', 'domain': (1,2,3),'dimensionality': 1},
                    {'name': 'thickness', 'type': 'continuous', 'domain': (0.4,0.6),'dimensionality': 2}]

I would like to make prediction. Is there some special way I have to treat the x for making prediction? I have tried passing into model.model.predict(np.array([0,1,0.4,0.4])). However I obtain an error message:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "<ipython-input-73-374d44b3f491>", line 1, in <module>
    yh = myBopt.model.model.predict(np.array([0,1,0.4,0.4]).reshape(1,4))

  File "D:\Pythonwd\lib\site-packages\GPy\core\gp.py", line 335, in predict
    mean, var = self._raw_predict(Xnew, full_cov=full_cov, kern=kern)

  File "D:\Pythonwd\lib\site-packages\GPy\core\gp.py", line 292, in _raw_predict
    mu, var = self.posterior._raw_predict(kern=self.kern if kern is None else kern, Xnew=Xnew, pred_var=self._predictive_variable, full_cov=full_cov)

  File "D:\Pythonwd\lib\site-packages\GPy\inference\latent_function_inference\posterior.py", line 276, in _raw_predict
    Kx = kern.K(pred_var, Xnew)

  File "D:\Pythonwd\lib\site-packages\GPy\kern\src\kernel_slice_operations.py", line 109, in wrap
    with _Slice_wrap(self, X, X2) as s:

  File "D:\Pythonwd\lib\site-packages\GPy\kern\src\kernel_slice_operations.py", line 65, in __init__
    self.X2 = self.k._slice_X(X2) if X2 is not None else X2

  File "<D:\Pythonwd\lib\site-packages\decorator.py:decorator-gen-140>", line 2, in _slice_X

  File "D:\Pythonwd\lib\site-packages\paramz\caching.py", line 283, in g
    return cacher(*args, **kw)

  File "D:\Pythonwd\lib\site-packages\paramz\caching.py", line 172, in __call__
    return self.operation(*args, **kw)

  File "D:\Pythonwd\lib\site-packages\GPy\kern\src\kern.py", line 117, in _slice_X
    return X[:, self._all_dims_active]

IndexError: index 4 is out of bounds for axis 1 with size 4

Thank you for your help.

chengymo commented 4 years ago

I just realize in prediction of categorical domain, I would have to extend the dimension for the categorical data. i.e. my categorical dimension has 21 values. Hence the actual X required for input has dimension of 21+3. So the prediction worked if I input: yh = myBopt.model.model.predict(np.array([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0.4,0.4]).reshape(1,24)) Is there a better way to make prediction with some tranformation?

ekalosak commented 4 years ago

If I'm not mistaken, usually, you'll want to use myBopt.model.predict() because it does the munging for you via the design_space.

See e.g.

Screen Shot 2020-02-07 at 11 45 05 AM

where the second element of the bo.space.space is categorical and I can use 2 as a representation of the specific category when using bo.model.predict.