Skip to content

Commit

Permalink
Revert "custom hamming kernel enabling single task gp on categorical …
Browse files Browse the repository at this point in the history
…features"

This reverts commit 17d8350.
  • Loading branch information
e-dorigatti committed Dec 19, 2024
1 parent 8400fdb commit 2e29852
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 243 deletions.
3 changes: 1 addition & 2 deletions bofire/data_models/kernels/categorical.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, Optional
from typing import Literal

from bofire.data_models.kernels.kernel import ConcreteKernel

Expand All @@ -10,4 +10,3 @@ class CategoricalKernel(ConcreteKernel):
class HammingDistanceKernel(CategoricalKernel):
type: Literal["HammingDistanceKernel"] = "HammingDistanceKernel"
ard: bool = True
with_one_hots: Optional[bool] = None
25 changes: 0 additions & 25 deletions bofire/kernels/categorical.py

This file was deleted.

35 changes: 6 additions & 29 deletions bofire/kernels/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import bofire.data_models.kernels.api as data_models
import bofire.priors.api as priors
from bofire.kernels.categorical import HammingKernelWithOneHots
from bofire.kernels.fingerprint_kernels.tanimoto_kernel import TanimotoKernel
from bofire.kernels.shape import WassersteinKernel

Expand Down Expand Up @@ -216,35 +215,13 @@ def map_HammingDistanceKernel(
ard_num_dims: int,
active_dims: List[int],
features_to_idx_mapper: Optional[Callable[[List[str]], List[int]]],
) -> GpytorchKernel:
) -> CategoricalKernel:
active_dims = _compute_active_dims(data_model, active_dims, features_to_idx_mapper)

if data_model.with_one_hots is None:
with_one_hots = data_model.features is not None and len(active_dims) > 1
else:
with_one_hots = data_model.with_one_hots

if with_one_hots and len(active_dims) == 1:
raise RuntimeError(
"only one feature for categorical kernel operating on one-hot features"
)
elif not with_one_hots and len(active_dims) > 1:
# this is not necessarily an issue since botorch's CategoricalKernel
# can work on multiple features at the same time
pass

if with_one_hots:
return HammingKernelWithOneHots(
batch_shape=batch_shape,
ard_num_dims=len(active_dims) if data_model.ard else None,
active_dims=active_dims, # type: ignore
)
else:
return CategoricalKernel(
batch_shape=batch_shape,
ard_num_dims=len(active_dims) if data_model.ard else None,
active_dims=active_dims, # type: ignore
)
return CategoricalKernel(
batch_shape=batch_shape,
ard_num_dims=len(active_dims) if data_model.ard else None,
active_dims=active_dims, # type: ignore
)


def map_WassersteinKernel(
Expand Down
132 changes: 0 additions & 132 deletions scratch.py

This file was deleted.

55 changes: 0 additions & 55 deletions tests/bofire/surrogates/test_gps.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,61 +335,6 @@ def test_SingleTaskGPModel_feature_subsets():
assert len(gp_mapped.model.covar_module.kernels[1].active_dims) == 4


def test_SingleTaskGPModel_mixed_features():
"""test that we can use a single task gp with mixed features"""
inputs = Inputs(
features=[
ContinuousInput(
key=f"x_{i+1}",
bounds=(-4, 4),
)
for i in range(2)
]
+ [
CategoricalInput(key="x_cat_1", categories=["mama", "papa"]),
CategoricalInput(key="x_cat_2", categories=["cat", "dog"]),
],
)
outputs = Outputs(features=[ContinuousOutput(key="y")])
experiments = inputs.sample(n=10)
experiments.eval("y=((x_1**2 + x_2 - 11)**2+(x_1 + x_2**2 -7)**2)", inplace=True)
experiments.loc[experiments.x_cat_1 == "mama", "y"] *= 5.0
experiments.loc[experiments.x_cat_1 == "papa", "y"] /= 2.0
experiments.loc[experiments.x_cat_2 == "cat", "y"] *= -2.0
experiments.loc[experiments.x_cat_2 == "dog", "y"] /= -5.0
experiments["valid_y"] = 1

gp_data = SingleTaskGPSurrogate(
inputs=inputs,
outputs=outputs,
kernel=AdditiveKernel(
kernels=[
HammingDistanceKernel(
ard=True,
features=["x_cat_1", "x_cat_2"],
),
RBFKernel(
ard=True,
lengthscale_prior=HVARFNER_LENGTHSCALE_PRIOR(),
features=[f"x_{i+1}" for i in range(2)],
),
]
),
)

gp_mapped = surrogates.map(gp_data)
assert hasattr(gp_mapped, "fit")
assert len(gp_mapped.kernel.kernels) == 2
assert gp_mapped.kernel.kernels[0].features == ["x_cat_1", "x_cat_2"]
assert gp_mapped.kernel.kernels[1].features == ["x_1", "x_2"]
gp_mapped.fit(experiments)
pred = gp_mapped.predict(experiments)
assert pred.shape == (10, 2)
assert ((pred['y_pred'] - experiments['y'])**2).mean() < 0.5
assert gp_mapped.model.covar_module.kernels[0].active_dims.tolist() == [2, 3, 4, 5]
assert gp_mapped.model.covar_module.kernels[1].active_dims.tolist() == [0, 1]


def test_MixedSingleTaskGPHyperconfig():
inputs = Inputs(
features=[
Expand Down

0 comments on commit 2e29852

Please sign in to comment.