|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | +# |
| 4 | +# This source code is licensed under the MIT license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import torch |
| 8 | +from botorch.acquisition.analytic import ExpectedImprovement |
| 9 | +from botorch.acquisition.monte_carlo import qExpectedImprovement |
| 10 | +from botorch.acquisition.penalized import ( |
| 11 | + GaussianPenalty, |
| 12 | + GroupLassoPenalty, |
| 13 | + L2Penalty, |
| 14 | + PenalizedAcquisitionFunction, |
| 15 | + group_lasso_regularizer, |
| 16 | +) |
| 17 | +from botorch.exceptions import UnsupportedError |
| 18 | +from botorch.sampling.samplers import IIDNormalSampler |
| 19 | +from botorch.utils.testing import BotorchTestCase, MockModel, MockPosterior |
| 20 | + |
| 21 | + |
| 22 | +class TestL2Penalty(BotorchTestCase): |
| 23 | + def test_gaussian_penalty(self): |
| 24 | + for dtype in (torch.float, torch.double): |
| 25 | + init_point = torch.tensor([1.0, 1.0, 1.0], device=self.device, dtype=dtype) |
| 26 | + l2_module = L2Penalty(init_point=init_point) |
| 27 | + |
| 28 | + # testing a batch of two points |
| 29 | + sample_point = torch.tensor( |
| 30 | + [[1.0, 2.0, 3.0], [2.0, 3.0, 4.0]], device=self.device, dtype=dtype |
| 31 | + ) |
| 32 | + |
| 33 | + diff_norm_squared = ( |
| 34 | + torch.norm((sample_point - init_point), p=2, dim=-1) ** 2 |
| 35 | + ) |
| 36 | + real_value = diff_norm_squared.max(dim=-1).values |
| 37 | + computed_value = l2_module(sample_point) |
| 38 | + self.assertEqual(computed_value.item(), real_value.item()) |
| 39 | + |
| 40 | + |
| 41 | +class TestGaussianPenalty(BotorchTestCase): |
| 42 | + def test_gaussian_penalty(self): |
| 43 | + for dtype in (torch.float, torch.double): |
| 44 | + init_point = torch.tensor([1.0, 1.0, 1.0], device=self.device, dtype=dtype) |
| 45 | + sigma = 0.1 |
| 46 | + gaussian_module = GaussianPenalty(init_point=init_point, sigma=sigma) |
| 47 | + |
| 48 | + # testing a batch of two points |
| 49 | + sample_point = torch.tensor( |
| 50 | + [[1.0, 2.0, 3.0], [2.0, 3.0, 4.0]], device=self.device, dtype=dtype |
| 51 | + ) |
| 52 | + |
| 53 | + diff_norm_squared = ( |
| 54 | + torch.norm((sample_point - init_point), p=2, dim=-1) ** 2 |
| 55 | + ) |
| 56 | + max_l2_distance = diff_norm_squared.max(dim=-1).values |
| 57 | + real_value = torch.exp(max_l2_distance / 2 / sigma ** 2) |
| 58 | + computed_value = gaussian_module(sample_point) |
| 59 | + self.assertEqual(computed_value.item(), real_value.item()) |
| 60 | + |
| 61 | + |
| 62 | +class TestGroupLassoPenalty(BotorchTestCase): |
| 63 | + def test_group_lasso_penalty(self): |
| 64 | + for dtype in (torch.float, torch.double): |
| 65 | + init_point = torch.tensor([0.5, 0.5, 0.5], device=self.device, dtype=dtype) |
| 66 | + groups = [[0, 2], [1]] |
| 67 | + group_lasso_module = GroupLassoPenalty(init_point=init_point, groups=groups) |
| 68 | + |
| 69 | + # testing a single point |
| 70 | + sample_point = torch.tensor( |
| 71 | + [[1.0, 2.0, 3.0]], device=self.device, dtype=dtype |
| 72 | + ) |
| 73 | + real_value = group_lasso_regularizer( |
| 74 | + sample_point - init_point, groups |
| 75 | + ) # torch.tensor([5.105551242828369], device=self.device, dtype=dtype) |
| 76 | + computed_value = group_lasso_module(sample_point) |
| 77 | + self.assertEqual(computed_value.item(), real_value.item()) |
| 78 | + |
| 79 | + # testing unsupported input dim: X.shape[-2] > 1 |
| 80 | + sample_point_2 = torch.tensor( |
| 81 | + [[1.0, 2.0, 3.0], [2.0, 3.0, 4.0]], device=self.device, dtype=dtype |
| 82 | + ) |
| 83 | + with self.assertRaises(NotImplementedError): |
| 84 | + group_lasso_module(sample_point_2) |
| 85 | + |
| 86 | + |
| 87 | +class TestPenalizedAcquisitionFunction(BotorchTestCase): |
| 88 | + def test_penalized_acquisition_function(self): |
| 89 | + for dtype in (torch.float, torch.double): |
| 90 | + mock_model = MockModel( |
| 91 | + MockPosterior(mean=torch.tensor([1.0]), variance=torch.tensor([1.0])) |
| 92 | + ) |
| 93 | + init_point = torch.tensor([0.5, 0.5, 0.5], device=self.device, dtype=dtype) |
| 94 | + groups = [[0, 2], [1]] |
| 95 | + raw_acqf = ExpectedImprovement(model=mock_model, best_f=1.0) |
| 96 | + penalty = GroupLassoPenalty(init_point=init_point, groups=groups) |
| 97 | + lmbda = 0.1 |
| 98 | + acqf = PenalizedAcquisitionFunction( |
| 99 | + raw_acqf=raw_acqf, penalty_func=penalty, regularization_parameter=lmbda |
| 100 | + ) |
| 101 | + |
| 102 | + sample_point = torch.tensor( |
| 103 | + [[1.0, 2.0, 3.0]], device=self.device, dtype=dtype |
| 104 | + ) |
| 105 | + raw_value = raw_acqf(sample_point) |
| 106 | + penalty_value = penalty(sample_point) |
| 107 | + real_value = raw_value - lmbda * penalty_value |
| 108 | + computed_value = acqf(sample_point) |
| 109 | + self.assertTrue(torch.equal(real_value, computed_value)) |
| 110 | + |
| 111 | + # testing X_pending for analytic raw_acqfn (EI) |
| 112 | + X_pending = torch.tensor([0.1, 0.2, 0.3], device=self.device, dtype=dtype) |
| 113 | + with self.assertRaises(UnsupportedError): |
| 114 | + acqf.set_X_pending(X_pending) |
| 115 | + |
| 116 | + # testing X_pending for non-analytic raw_acqfn (EI) |
| 117 | + sampler = IIDNormalSampler(num_samples=2) |
| 118 | + raw_acqf_2 = qExpectedImprovement( |
| 119 | + model=mock_model, best_f=0, sampler=sampler |
| 120 | + ) |
| 121 | + init_point = torch.tensor([1.0, 1.0, 1.0], device=self.device, dtype=dtype) |
| 122 | + l2_module = L2Penalty(init_point=init_point) |
| 123 | + acqf_2 = PenalizedAcquisitionFunction( |
| 124 | + raw_acqf=raw_acqf_2, |
| 125 | + penalty_func=l2_module, |
| 126 | + regularization_parameter=lmbda, |
| 127 | + ) |
| 128 | + |
| 129 | + X_pending = torch.tensor([0.1, 0.2, 0.3], device=self.device, dtype=dtype) |
| 130 | + acqf_2.set_X_pending(X_pending) |
| 131 | + self.assertTrue(torch.equal(acqf_2.X_pending, X_pending)) |
0 commit comments