Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
)
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:
model1 = SingleTaskGP(train_X=train_x1, train_Y=train_y1)
model2 = SingleTaskGP(train_X=train_x2, train_Y=train_y2)
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)
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):
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))
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)
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)
import torch
from botorch import settings
from botorch.exceptions import InputDataError, InputDataWarning
from botorch.models.utils import (
add_output_dim,
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))
#! /usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# 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])
class TestAckley(SyntheticTestFunctionBaseTestCase, BotorchTestCase):
functions = [Ackley(), Ackley(negate=True), Ackley(noise_std=0.1), Ackley(dim=3)]
class TestBeale(SyntheticTestFunctionBaseTestCase, BotorchTestCase):
functions = [Beale(), Beale(negate=True), Beale(noise_std=0.1)]
class TestBranin(SyntheticTestFunctionBaseTestCase, BotorchTestCase):
functions = [Branin(), Branin(negate=True), Branin(noise_std=0.1)]
class TestBukin(SyntheticTestFunctionBaseTestCase, BotorchTestCase):
functions = [Bukin(), Bukin(negate=True), Bukin(noise_std=0.1)]
class TestCosine8(SyntheticTestFunctionBaseTestCase, BotorchTestCase):
functions = [Cosine8(), Cosine8(negate=True), Cosine8(noise_std=0.1)]
class TestDropWave(SyntheticTestFunctionBaseTestCase, BotorchTestCase):
functions = [DropWave(), DropWave(negate=True), DropWave(noise_std=0.1)]
class TestDixonPrice(SyntheticTestFunctionBaseTestCase, BotorchTestCase):