-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add polynomial kernel * add polynomial kernel * add polynomial kernel * add polynomial kernel * add quadratic surrogate * adding linear kernel not necessary * adding linear kernel not necessary * adding linear kernel not necessary * fix test * change kernel tzpe hint
- Loading branch information
1 parent
669fa44
commit 191b382
Showing
8 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import Literal | ||
|
||
from pydantic import Field | ||
|
||
from bofire.data_models.kernels.api import ( | ||
PolynomialKernel, | ||
) | ||
from bofire.data_models.priors.api import BOTORCH_NOISE_PRIOR, AnyPrior | ||
|
||
# from bofire.data_models.strategies.api import FactorialStrategy | ||
from bofire.data_models.surrogates.botorch import BotorchSurrogate | ||
from bofire.data_models.surrogates.scaler import ScalerEnum | ||
from bofire.data_models.surrogates.trainable import TrainableSurrogate | ||
|
||
|
||
class QuadraticSurrogate(BotorchSurrogate, TrainableSurrogate): | ||
type: Literal["QuadraticSurrogate"] = "QuadraticSurrogate" | ||
|
||
kernel: PolynomialKernel = Field(default_factory=lambda: PolynomialKernel(power=2)) | ||
noise_prior: AnyPrior = Field(default_factory=lambda: BOTORCH_NOISE_PRIOR()) | ||
scaler: ScalerEnum = ScalerEnum.NORMALIZE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import numpy as np | ||
from pandas.testing import assert_frame_equal | ||
|
||
import bofire.surrogates.api as surrogates | ||
from bofire.data_models.domain.api import Inputs, Outputs | ||
from bofire.data_models.features.api import ContinuousInput, ContinuousOutput | ||
from bofire.data_models.kernels.api import PolynomialKernel | ||
from bofire.data_models.surrogates.api import QuadraticSurrogate | ||
|
||
|
||
def test_QuadraticSurrogate(): | ||
N_EXPERIMENTS = 10 | ||
|
||
inputs = Inputs( | ||
features=[ | ||
ContinuousInput(key="a", bounds=(0, 40)), | ||
ContinuousInput(key="b", bounds=(20, 60)), | ||
] | ||
) | ||
outputs = Outputs(features=[ContinuousOutput(key="c")]) | ||
|
||
experiments = inputs.sample(N_EXPERIMENTS) | ||
experiments["c"] = ( | ||
experiments["a"] * 2.2 | ||
+ experiments["b"] * -0.05 | ||
+ experiments["b"] | ||
+ np.random.normal(loc=0, scale=5, size=N_EXPERIMENTS) | ||
) | ||
experiments["valid_c"] = 1 | ||
|
||
surrogate_data = QuadraticSurrogate(inputs=inputs, outputs=outputs) | ||
surrogate = surrogates.map(surrogate_data) | ||
|
||
assert isinstance(surrogate, surrogates.SingleTaskGPSurrogate) | ||
assert isinstance(surrogate.kernel, PolynomialKernel) | ||
|
||
# check dump | ||
surrogate.fit(experiments=experiments) | ||
preds = surrogate.predict(experiments) | ||
dump = surrogate.dumps() | ||
surrogate.loads(dump) | ||
preds2 = surrogate.predict(experiments) | ||
assert_frame_equal(preds, preds2) |