Skip to content

Commit

Permalink
Disable GPy for newer numpy
Browse files Browse the repository at this point in the history
GPy can not be used for newer numpy
  • Loading branch information
Alaya-in-Matrix committed Sep 12, 2023
1 parent 2bccb4e commit 726c9ec
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 37 deletions.
6 changes: 6 additions & 0 deletions HEBO/hebo/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the MIT License for more details.

has_gpy = True
try:
from numpy.testing import Tester
except ImportError:
has_gpy = False

from . import util, scalers, model_factory, layers, base_model
8 changes: 7 additions & 1 deletion HEBO/hebo/models/gp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the MIT License for more details.

from . import gp, gpy_wgp, gpy_mlp

from hebo.models import has_gpy

if has_gpy:
from . import gp, gpy_wgp, gpy_mlp
else:
from . import gp
4 changes: 4 additions & 0 deletions HEBO/hebo/models/gp/gpy_mlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
from ..scalers import TorchMinMaxScaler, TorchStandardScaler
from ..util import filter_nan

from hebo.models import has_gpy
if not has_gpy:
raise ImportError('GPy can not be installed with current numpy version, use GP instead')


class GPyMLPGP(BaseModel):
"""
Expand Down
3 changes: 3 additions & 0 deletions HEBO/hebo/models/gp/gpy_wgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from ..layers import EmbTransform, OneHotTransform
from ..scalers import TorchMinMaxScaler, TorchStandardScaler
from ..util import filter_nan
from hebo.models import has_gpy
if not has_gpy:
raise ImportError('GPy can not be installed with current numpy version, use GP instead')

import GPy
import torch
Expand Down
50 changes: 33 additions & 17 deletions HEBO/hebo/models/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
# PARTICULAR PURPOSE. See the MIT License for more details.

import torch
from hebo.models import has_gpy
from .base_model import BaseModel
from .gp.svgp import SVGP
from .gp.svidkl import SVIDKL
from .gp.gp import GP
from .gp.gpy_wgp import GPyGP
from .gp.gpy_mlp import GPyMLPGP
if has_gpy:
from .gp.gpy_wgp import GPyGP
from .gp.gpy_mlp import GPyMLPGP
from .rf.rf import RF

from .nn.mcbn import MCBNEnsemble
Expand All @@ -29,20 +31,34 @@
except ImportError:
has_catboost = False

model_dict = {
'svidkl' : SVIDKL,
'svgp' : SVGP,
'gp' : GP,
'gpy' : GPyGP,
'gpy_mlp' : GPyMLPGP,
'rf' : RF,
'deep_ensemble' : DeepEnsemble,
'psgld' : pSGLDEnsemble,
'mcbn' : MCBNEnsemble,
'masked_deep_ensemble' : MaskedDeepEnsemble,
'fe_deep_ensemble': FeDeepEnsemble,
'gumbel': GumbelDeepEnsemble,
}
if has_gpy:
model_dict = {
'svidkl' : SVIDKL,
'svgp' : SVGP,
'gp' : GP,
'gpy' : GPyGP,
'gpy_mlp' : GPyMLPGP,
'rf' : RF,
'deep_ensemble' : DeepEnsemble,
'psgld' : pSGLDEnsemble,
'mcbn' : MCBNEnsemble,
'masked_deep_ensemble' : MaskedDeepEnsemble,
'fe_deep_ensemble': FeDeepEnsemble,
'gumbel': GumbelDeepEnsemble,
}
else:
model_dict = {
'svidkl' : SVIDKL,
'svgp' : SVGP,
'gp' : GP,
'rf' : RF,
'deep_ensemble' : DeepEnsemble,
'psgld' : pSGLDEnsemble,
'mcbn' : MCBNEnsemble,
'masked_deep_ensemble' : MaskedDeepEnsemble,
'fe_deep_ensemble': FeDeepEnsemble,
'gumbel': GumbelDeepEnsemble,
}
if has_catboost:
model_dict['catboost'] = CatBoost

Expand Down Expand Up @@ -75,7 +91,7 @@ def __init__(self,
self.model_name = self.conf.get('base_model_name', 'gp')
self.model_conf = {k : v for k, v in self.conf.items() if k != 'model_name'}
self.models = [model_dict[self.model_name](num_cont, num_enum, 1, **self.model_conf) for _ in range(num_out)]


def fit(self, Xc, Xe, y):
for i in range(self.num_out):
Expand Down
5 changes: 4 additions & 1 deletion HEBO/hebo/optimizers/bo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@
import pandas as pd
import torch

from hebo.models import has_gpy
from hebo.design_space.design_space import DesignSpace
from hebo.models.model_factory import get_model
from hebo.acquisitions.acq import LCB
from hebo.acq_optimizers.evolution_optimizer import EvolutionOpt

from .abstract_optimizer import AbstractOptimizer

default_gp = 'gpy' if has_gpy else 'gp'

class BO(AbstractOptimizer):
support_combinatorial = True
support_contextual = True
def __init__(
self,
space : DesignSpace,
model_name = 'gpy',
model_name = default_gp,
rand_sample = None,
acq_cls = None,
acq_conf = None):
Expand Down
5 changes: 4 additions & 1 deletion HEBO/hebo/optimizers/hebo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from torch.quasirandom import SobolEngine
from sklearn.preprocessing import power_transform

from hebo.models import has_gpy
from hebo.design_space.design_space import DesignSpace
from hebo.models.model_factory import get_model
from hebo.acquisitions.acq import MACE, Mean, Sigma
Expand All @@ -27,11 +28,13 @@

torch.set_num_threads(min(1, torch.get_num_threads()))

default_gp = 'gpy' if has_gpy else 'gp'

class HEBO(AbstractOptimizer):
support_parallel_opt = True
support_combinatorial = True
support_contextual = True
def __init__(self, space, model_name = 'gpy', rand_sample = None, acq_cls = MACE, es = 'nsga2', model_config = None,
def __init__(self, space, model_name = default_gp, rand_sample = None, acq_cls = MACE, es = 'nsga2', model_config = None,
scramble_seed: Optional[int] = None ):
"""
model_name : surrogate model to be used
Expand Down
5 changes: 4 additions & 1 deletion HEBO/hebo/optimizers/hebo_contextual.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
from .hebo import HEBO
from hebo.acquisitions.acq import Acquisition, MACE

from hebo.models import has_gpy
default_gp = 'gpy' if has_gpy else 'gp'

class HEBO_VectorContextual(AbstractOptimizer):
support_parallel_opt = True
support_combinatorial = True
support_contextual = True
def __init__(self,
space,
context_dict : dict,
model_name : str = 'gpy',
model_name : str = default_gp,
rand_sample : int = None
):
self.hebo = HEBO(space, model_name, rand_sample)
Expand Down
5 changes: 4 additions & 1 deletion HEBO/hebo/optimizers/hebo_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
from .abstract_optimizer import AbstractOptimizer
from .hebo import HEBO

from hebo.models import has_gpy
default_gp = 'gpy' if has_gpy else 'gp'

torch.set_num_threads(min(1, torch.get_num_threads()))

def gen_emb_space(eff_dim : int, scale : float) -> DesignSpace:
Expand Down Expand Up @@ -86,7 +89,7 @@ class HEBO_Embedding(AbstractOptimizer):
support_contextual = False
def __init__(self,
space : DesignSpace,
model_name = 'gpy',
model_name = default_gp,
eff_dim : int = 1,
scale : float = 1,
strategy : str = 'alebo',
Expand Down
5 changes: 4 additions & 1 deletion HEBO/hebo/optimizers/noisy_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from copy import deepcopy
from torch.quasirandom import SobolEngine

from hebo.models import has_gpy
from hebo.design_space.design_space import DesignSpace
from hebo.models.model_factory import get_model
from hebo.acquisitions.acq import NoisyAcq, Mean, Sigma
Expand All @@ -23,11 +24,13 @@

torch.set_num_threads(min(1, torch.get_num_threads()))

default_gp = 'gpy' if has_gpy else 'gp'

class NoisyOpt(HEBO):
support_parallel_opt = True
support_combinatorial = True
support_contextual = True
def __init__(self, space, model_name = 'gpy', rand_sample = None, es = 'nsga2', model_config = None):
def __init__(self, space, model_name = default_gp, rand_sample = None, es = 'nsga2', model_config = None):
"""
model_name : surrogate model to be used
rand_iter : iterations to perform random sampling
Expand Down
15 changes: 1 addition & 14 deletions HEBO/test/test_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from hebo.optimizers.noisy_opt import NoisyOpt
from hebo.optimizers.hebo import HEBO
from hebo.optimizers.bo import BO
from hebo.optimizers.vcbo import VCBO
from hebo.optimizers.general import GeneralBO
from hebo.optimizers.evolution import Evolution
from hebo.optimizers.hebo_contextual import HEBO_VectorContextual
Expand All @@ -35,7 +34,7 @@
def obj(x : pd.DataFrame) -> np.ndarray:
return x['x0'].values.astype(float).reshape(-1, 1) ** 2

@pytest.mark.parametrize('model_name', ['gp', 'gpy', 'rf', 'deep_ensemble', 'gpy_mlp'])
@pytest.mark.parametrize('model_name', ['gp', 'rf', 'deep_ensemble'])
@pytest.mark.parametrize('opt_cls', [BO, HEBO, GeneralBO, NoisyOpt, Evolution], ids = ['bo', 'hebo', 'general', 'noisy', 'evolution'])
def test_opt(model_name, opt_cls):
space = DesignSpace().parse([
Expand All @@ -55,18 +54,6 @@ def test_opt(model_name, opt_cls):
if num_suggest > 11:
break

def test_vcbo():
space = DesignSpace().parse([
{'name' : 'x0', 'type' : 'num', 'lb' : -3, 'ub' : 7},
])
opt = VCBO(space, rand_sample = 8)
num_suggest = 0
for i in range(11):
num_suggest = 8 if opt.support_parallel_opt else 1
rec = opt.suggest(n_suggestions = num_suggest)
num_suggest += rec.shape[0]
if num_suggest > 11:
break

@pytest.mark.parametrize('start_from_mu', [True, False])
def test_cmaes(start_from_mu):
Expand Down

0 comments on commit 726c9ec

Please sign in to comment.