# How to use botorch - 10 common examples

## To help you get started, weâ€™ve selected a few botorch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

pytorch / botorch / botorch / test_functions / synthetic.py View on Github
``````self._optimizers = [tuple(0.0 for _ in range(self.dim))]
super().__init__(noise_std=noise_std, negate=negate)

def evaluate_true(self, X: Tensor) -> Tensor:
result = torch.zeros_like(X[..., 0])
for i in range(self.dim // 4):
i_ = i + 1
part1 = (X[..., 4 * i_ - 4] + 10.0 * X[..., 4 * i_ - 3]) ** 2
part2 = 5.0 * (X[..., 4 * i_ - 2] - X[..., 4 * i_ - 1]) ** 2
part3 = (X[..., 4 * i_ - 3] - 2.0 * X[..., 4 * i_ - 2]) ** 4
part4 = 10.0 * (X[..., 4 * i_ - 4] - X[..., 4 * i_ - 1]) ** 4
result += part1 + part2 + part3 + part4
return result

class Rastrigin(SyntheticTestFunction):

_optimal_value = 0.0

def __init__(
self, dim=2, noise_std: Optional[float] = None, negate: bool = False
) -> None:
self.dim = dim
self._bounds = [(-5.12, 5.12) for _ in range(self.dim)]
self._optimizers = [tuple(0.0 for _ in range(self.dim))]
super().__init__(noise_std=noise_std, negate=negate)

def evaluate_true(self, X: Tensor) -> Tensor:
return 10.0 * self.dim + torch.sum(
X ** 2 - 10.0 * torch.cos(2.0 * math.pi * X), dim=-1
)``````
pytorch / botorch / test / acquisition / test_monte_carlo.py View on Github
``````bs = acqf.sampler.base_samples.clone()
acqf(X)
self.assertFalse(torch.equal(acqf.sampler.base_samples, bs))
res = acqf(X.expand(2, 1, 1))  # 2-dim batch
self.assertEqual(res[0].item(), 1.0)
self.assertEqual(res[1].item(), 0.0)
# the base samples should have the batch dim collapsed
self.assertEqual(acqf.sampler.base_samples.shape, torch.Size([2, 1, 2, 1]))
bs = acqf.sampler.base_samples.clone()
acqf(X.expand(2, 1, 1))
self.assertFalse(torch.equal(acqf.sampler.base_samples, bs))

# TODO: Test different objectives (incl. constraints)

class TestQNoisyExpectedImprovement(BotorchTestCase):
def test_q_noisy_expected_improvement(self):
for dtype in (torch.float, torch.double):
# the event shape is `b x q x t` = 1 x 2 x 1
samples_noisy = torch.tensor([1.0, 0.0], device=self.device, dtype=dtype)
samples_noisy = samples_noisy.view(1, 2, 1)
# X_baseline is `q' x d` = 1 x 1
X_baseline = torch.zeros(1, 1, device=self.device, dtype=dtype)
mm_noisy = MockModel(MockPosterior(samples=samples_noisy))
# X is `q x d` = 1 x 1
X = torch.zeros(1, 1, device=self.device, dtype=dtype)

# basic test
sampler = IIDNormalSampler(num_samples=2)
acqf = qNoisyExpectedImprovement(
model=mm_noisy, X_baseline=X_baseline, sampler=sampler
)``````
pytorch / botorch / test / models / test_model_list_gp_regression.py View on Github
``````train_y1_var = 0.1 + 0.1 * torch.rand_like(train_y1, **tkwargs)
train_y2_var = 0.1 + 0.1 * torch.rand_like(train_y2, **tkwargs)
model1 = FixedNoiseGP(
train_X=train_x1, train_Y=train_y1, train_Yvar=train_y1_var
)
model2 = FixedNoiseGP(
train_X=train_x2, train_Y=train_y2, train_Yvar=train_y2_var
)
else:
model = ModelListGP(model1, model2)
return model.to(**tkwargs)

class TestModelListGP(BotorchTestCase):
def test_ModelListGP(self):
for double in (False, True):
tkwargs = {
"device": self.device,
"dtype": torch.double if double else torch.float,
}
model = _get_model(n=10, **tkwargs)
self.assertIsInstance(model, ModelListGP)
self.assertIsInstance(model.likelihood, LikelihoodList)
for m in model.models:
self.assertIsInstance(m.mean_module, ConstantMean)
self.assertIsInstance(m.covar_module, ScaleKernel)
matern_kernel = m.covar_module.base_kernel
self.assertIsInstance(matern_kernel, MaternKernel)
self.assertIsInstance(matern_kernel.lengthscale_prior, GammaPrior)``````
pytorch / botorch / test / test_functions / test_synthetic.py View on Github
``````class TestDropWave(SyntheticTestFunctionBaseTestCase, BotorchTestCase):

functions = [DropWave(), DropWave(negate=True), DropWave(noise_std=0.1)]

class TestDixonPrice(SyntheticTestFunctionBaseTestCase, BotorchTestCase):

functions = [
DixonPrice(),
DixonPrice(negate=True),
DixonPrice(noise_std=0.1),
DixonPrice(dim=3),
]

class TestEggHolder(SyntheticTestFunctionBaseTestCase, BotorchTestCase):

functions = [EggHolder(), EggHolder(negate=True), EggHolder(noise_std=0.1)]

class TestGriewank(SyntheticTestFunctionBaseTestCase, BotorchTestCase):

functions = [
Griewank(),
Griewank(negate=True),
Griewank(noise_std=0.1),
Griewank(dim=4),
]

class TestHartmann(SyntheticTestFunctionBaseTestCase, BotorchTestCase):``````
pytorch / botorch / test / posteriors / test_posterior.py View on Github
``````def device(self):
pass

@property
def dtype(self):
pass

@property
def event_shape(self):
pass

def rsample(self, *args):
pass

class TestPosterior(BotorchTestCase):
def test_abstract_base_posterior(self):
with self.assertRaises(TypeError):
Posterior()

def test_mean_var_notimplemented_error(self):
posterior = NotSoAbstractPosterior()
with self.assertRaises(NotImplementedError) as e:
posterior.mean
self.assertIn("NotSoAbstractPosterior", str(e.exception))
with self.assertRaises(NotImplementedError) as e:
posterior.variance
self.assertIn("NotSoAbstractPosterior", str(e.exception))``````
pytorch / botorch / test / acquisition / test_knowledge_gradient.py View on Github
``````model=mm,
num_fantasies=n_f,
objective=objective,
current_value=current_value,
cost_aware_utility=cau,
)
val = qMFKG(X)
patch_f.assert_called_once()
cargs, ckwargs = patch_f.call_args
self.assertEqual(ckwargs["X"].shape, torch.Size([1, 1, 1]))
val_exp = mock_util(X, objective(samples) - current_value).mean(dim=0)
self.assertTrue(torch.allclose(val, val_exp, atol=1e-4))
self.assertTrue(torch.equal(qMFKG.extract_candidates(X), X[..., :-n_f, :]))

class TestKGUtils(BotorchTestCase):
def test_get_value_function(self):
with mock.patch(NO, new_callable=mock.PropertyMock) as mock_num_outputs:
mock_num_outputs.return_value = 1
mm = MockModel(None)
# test PosteriorMean
vf = _get_value_function(mm)
self.assertIsInstance(vf, PosteriorMean)
self.assertIsNone(vf.objective)
# test SimpleRegret
obj = GenericMCObjective(lambda Y: Y.sum(dim=-1))
sampler = IIDNormalSampler(num_samples=2)
vf = _get_value_function(model=mm, objective=obj, sampler=sampler)
self.assertIsInstance(vf, qSimpleRegret)
self.assertEqual(vf.objective, obj)
self.assertEqual(vf.sampler, sampler)``````
pytorch / botorch / test / utils / test_transforms.py View on Github
``````Y = torch.rand(5, 4, 3, 2)
X_tf = match_batch_shape(X, Y)
self.assertTrue(torch.equal(X_tf, X.repeat(5, 1, 1, 1)))

X = torch.rand(2, 1, 3, 2)
Y = torch.rand(2, 4, 3, 2)
X_tf = match_batch_shape(X, Y)
self.assertTrue(torch.equal(X_tf, X.repeat(1, 4, 1, 1)))

X = torch.rand(4, 2, 3, 2)
Y = torch.rand(4, 3, 3, 2)
with self.assertRaises(RuntimeError):
match_batch_shape(X, Y)

class TorchNormalizeIndices(BotorchTestCase):
def test_normalize_indices(self):
self.assertIsNone(normalize_indices(None, 3))
indices = [0, 2]
nlzd_indices = normalize_indices(indices, 3)
self.assertEqual(nlzd_indices, indices)
nlzd_indices = normalize_indices(indices, 4)
self.assertEqual(nlzd_indices, indices)
indices = [0, -1]
nlzd_indices = normalize_indices(indices, 3)
self.assertEqual(nlzd_indices, [0, 2])
with self.assertRaises(ValueError):
nlzd_indices = normalize_indices([3], 3)
with self.assertRaises(ValueError):
nlzd_indices = normalize_indices([-4], 3)``````
pytorch / botorch / test / models / test_utils.py View on Github
``````import torch
from botorch import settings
from botorch.exceptions import InputDataError, InputDataWarning
from botorch.models.utils import (
check_min_max_scaling,
check_no_nans,
check_standardization,
multioutput_to_batch_mode_transform,
validate_input_scaling,
)
from botorch.utils.testing import BotorchTestCase

class TestMultiOutputToBatchModeTransform(BotorchTestCase):
def test_multioutput_to_batch_mode_transform(self):
for dtype in (torch.float, torch.double):
tkwargs = {"device": self.device, "dtype": dtype}
n = 3
num_outputs = 2
train_X = torch.rand(n, 1, **tkwargs)
train_Y = torch.rand(n, num_outputs, **tkwargs)
train_Yvar = torch.rand(n, num_outputs, **tkwargs)
X_out, Y_out, Yvar_out = multioutput_to_batch_mode_transform(
train_X=train_X,
train_Y=train_Y,
num_outputs=num_outputs,
train_Yvar=train_Yvar,
)
expected_X_out = train_X.unsqueeze(0).expand(num_outputs, -1, 1)
self.assertTrue(torch.equal(X_out, expected_X_out))``````
pytorch / botorch / test / test_functions / test_synthetic.py View on Github
``````functions = [HolderTable(), HolderTable(negate=True), HolderTable(noise_std=0.1)]

class TestLevy(SyntheticTestFunctionBaseTestCase, BotorchTestCase):

functions = [
Levy(),
Levy(negate=True),
Levy(noise_std=0.1),
Levy(dim=3),
Levy(dim=3, negate=True),
Levy(dim=3, noise_std=0.1),
]

class TestMichalewicz(SyntheticTestFunctionBaseTestCase, BotorchTestCase):

functions = [
Michalewicz(),
Michalewicz(negate=True),
Michalewicz(noise_std=0.1),
Michalewicz(dim=5),
Michalewicz(dim=5, negate=True),
Michalewicz(dim=5, noise_std=0.1),
Michalewicz(dim=10),
Michalewicz(dim=10, negate=True),
Michalewicz(dim=10, noise_std=0.1),
]

class TestPowell(SyntheticTestFunctionBaseTestCase, BotorchTestCase):``````
pytorch / botorch / test / models / test_model.py View on Github
``````#! /usr/bin/env python3
#
# LICENSE file in the root directory of this source tree.

from botorch.models.model import Model
from botorch.utils.testing import BotorchTestCase

class NotSoAbstractBaseModel(Model):
def posterior(self, X, output_indices, observation_noise, **kwargs):
pass

class TestBaseModel(BotorchTestCase):
def test_abstract_base_model(self):
with self.assertRaises(TypeError):
Model()

def test_not_so_abstract_base_model(self):
model = NotSoAbstractBaseModel()
with self.assertRaises(NotImplementedError):
model.condition_on_observations(None, None)
with self.assertRaises(NotImplementedError):
model.num_outputs
with self.assertRaises(NotImplementedError):
model.subset_output([0])``````

## botorch

Bayesian Optimization in PyTorch

MIT