|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and 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 | +from itertools import product |
| 8 | +from typing import Dict |
| 9 | +from warnings import catch_warnings, simplefilter |
| 10 | + |
| 11 | +import torch |
| 12 | +from botorch.acquisition.input_constructors import get_acqf_input_constructor |
| 13 | +from botorch.acquisition.logei import ( |
| 14 | + qLogExpectedImprovement, |
| 15 | + qLogNoisyExpectedImprovement, |
| 16 | +) |
| 17 | +from botorch.acquisition.monte_carlo import ( |
| 18 | + qExpectedImprovement, |
| 19 | + qNoisyExpectedImprovement, |
| 20 | + qProbabilityOfImprovement, |
| 21 | +) |
| 22 | +from botorch.acquisition.objective import LearnedObjective |
| 23 | +from botorch.exceptions.warnings import InputDataWarning |
| 24 | +from botorch.fit import fit_gpytorch_mll |
| 25 | +from botorch.models import SingleTaskGP |
| 26 | +from botorch.sampling.normal import SobolQMCNormalSampler |
| 27 | +from botorch.utils.datasets import SupervisedDataset |
| 28 | +from botorch.utils.testing import BotorchTestCase |
| 29 | +from gpytorch.mlls.exact_marginal_log_likelihood import ExactMarginalLogLikelihood |
| 30 | + |
| 31 | + |
| 32 | +class TestObjectiveAndConstraintIntegration(BotorchTestCase): |
| 33 | + def setUp(self) -> None: |
| 34 | + self.q = 3 |
| 35 | + self.d = 2 |
| 36 | + self.tkwargs = {"device": self.device, "dtype": torch.double} |
| 37 | + |
| 38 | + def _get_acqf_inputs(self, train_batch_shape: torch.Size, m: int) -> Dict: |
| 39 | + |
| 40 | + train_x = torch.rand((*train_batch_shape, 5, self.d), **self.tkwargs) |
| 41 | + y = torch.rand((*train_batch_shape, 5, m), **self.tkwargs) |
| 42 | + |
| 43 | + training_data = SupervisedDataset( |
| 44 | + X=train_x, |
| 45 | + Y=y, |
| 46 | + feature_names=[f"x{i}" for i in range(self.d)], |
| 47 | + outcome_names=[f"y{i}" for i in range(m)], |
| 48 | + ) |
| 49 | + utility = y.sum(-1).unsqueeze(-1) |
| 50 | + |
| 51 | + with catch_warnings(): |
| 52 | + simplefilter("ignore", category=InputDataWarning) |
| 53 | + model = SingleTaskGP(train_x, y) |
| 54 | + mll = ExactMarginalLogLikelihood(model.likelihood, model) |
| 55 | + fit_gpytorch_mll(mll=mll) |
| 56 | + |
| 57 | + with catch_warnings(): |
| 58 | + simplefilter("ignore", category=InputDataWarning) |
| 59 | + pref_model = SingleTaskGP(y, utility) |
| 60 | + pref_mll = ExactMarginalLogLikelihood(pref_model.likelihood, pref_model) |
| 61 | + fit_gpytorch_mll(mll=pref_mll) |
| 62 | + return { |
| 63 | + "training_data": training_data, |
| 64 | + "model": model, |
| 65 | + "pref_model": pref_model, |
| 66 | + "train_x": train_x, |
| 67 | + } |
| 68 | + |
| 69 | + def _base_test_with_learned_objective( |
| 70 | + self, |
| 71 | + train_batch_shape: torch.Size, |
| 72 | + prune_baseline: bool, |
| 73 | + test_batch_shape: torch.Size, |
| 74 | + ) -> None: |
| 75 | + acq_inputs = self._get_acqf_inputs(train_batch_shape=train_batch_shape, m=4) |
| 76 | + |
| 77 | + pref_sample_shapes = [1, 8] |
| 78 | + test_acqf_classes_and_kws = [ |
| 79 | + # Not yet working |
| 80 | + # (qExpectedImprovement, {}), |
| 81 | + # (qProbabilityOfImprovement, {}), |
| 82 | + # (qLogExpectedImprovement, {}), |
| 83 | + (qNoisyExpectedImprovement, {"prune_baseline": prune_baseline}), |
| 84 | + (qLogNoisyExpectedImprovement, {"prune_baseline": prune_baseline}), |
| 85 | + ] |
| 86 | + |
| 87 | + for (acqf_cls, kws), pref_sample_shape in product( |
| 88 | + test_acqf_classes_and_kws, pref_sample_shapes |
| 89 | + ): |
| 90 | + with self.subTest( |
| 91 | + train_batch_shape=train_batch_shape, |
| 92 | + test_batch_shape=test_batch_shape, |
| 93 | + prune_baseline=prune_baseline, |
| 94 | + acqf_cls=acqf_cls, |
| 95 | + pref_sample_shape=pref_sample_shape, |
| 96 | + ): |
| 97 | + objective = LearnedObjective( |
| 98 | + pref_model=acq_inputs["pref_model"], |
| 99 | + sample_shape=torch.Size([pref_sample_shape]), |
| 100 | + ) |
| 101 | + test_x = torch.rand( |
| 102 | + (*test_batch_shape, *train_batch_shape, self.q, self.d), |
| 103 | + **self.tkwargs, |
| 104 | + ) |
| 105 | + input_constructor = get_acqf_input_constructor(acqf_cls=acqf_cls) |
| 106 | + |
| 107 | + inputs = input_constructor( |
| 108 | + objective=objective, |
| 109 | + model=acq_inputs["model"], |
| 110 | + training_data=acq_inputs["training_data"], |
| 111 | + X_baseline=acq_inputs["train_x"], |
| 112 | + sampler=SobolQMCNormalSampler(torch.Size([4])), |
| 113 | + **kws, |
| 114 | + ) |
| 115 | + acqf = acqf_cls(**inputs) |
| 116 | + acq_val = acqf(test_x) |
| 117 | + self.assertEqual(acq_val.shape.numel(), test_x.shape[:-2].numel()) |
| 118 | + |
| 119 | + def test_with_learned_objective_train_data_not_batched(self) -> None: |
| 120 | + train_batch_shape = [] |
| 121 | + test_batch_shapes = [[], [1], [2]] |
| 122 | + for test_batch_shape in test_batch_shapes: |
| 123 | + self._base_test_with_learned_objective( |
| 124 | + train_batch_shape=torch.Size(train_batch_shape), |
| 125 | + prune_baseline=True, |
| 126 | + test_batch_shape=torch.Size(test_batch_shape), |
| 127 | + ) |
| 128 | + |
| 129 | + def test_with_learned_objective_train_data_1d_batch(self) -> None: |
| 130 | + train_batch_shape = [1] |
| 131 | + test_batch_shapes = [[], [1], [2]] |
| 132 | + for test_batch_shape in test_batch_shapes: |
| 133 | + self._base_test_with_learned_objective( |
| 134 | + train_batch_shape=torch.Size(train_batch_shape), |
| 135 | + # Batched inputs `X_baseline` are currently unsupported by |
| 136 | + # prune_inferior_points |
| 137 | + prune_baseline=False, |
| 138 | + test_batch_shape=torch.Size(test_batch_shape), |
| 139 | + ) |
| 140 | + |
| 141 | + def test_with_learned_objective_train_data_batched(self) -> None: |
| 142 | + train_batch_shape = [3] |
| 143 | + test_batch_shapes = [[], [1], [2]] |
| 144 | + for test_batch_shape in test_batch_shapes: |
| 145 | + self._base_test_with_learned_objective( |
| 146 | + train_batch_shape=torch.Size(train_batch_shape), |
| 147 | + # Batched inputs `X_baseline` are currently unsupported by |
| 148 | + # prune_inferior_points |
| 149 | + prune_baseline=False, |
| 150 | + test_batch_shape=torch.Size(test_batch_shape), |
| 151 | + ) |
| 152 | + |
| 153 | + def _base_test_without_learned_objective( |
| 154 | + self, |
| 155 | + train_batch_shape: torch.Size, |
| 156 | + prune_baseline: bool, |
| 157 | + test_batch_shape: torch.Size, |
| 158 | + ) -> None: |
| 159 | + inputs = self._get_acqf_inputs(train_batch_shape=train_batch_shape, m=1) |
| 160 | + constraints = [lambda y: y[..., 0]] |
| 161 | + test_x = torch.rand( |
| 162 | + (*test_batch_shape, *train_batch_shape, self.q, self.d), **self.tkwargs |
| 163 | + ) |
| 164 | + |
| 165 | + input_constructor_kwargs = { |
| 166 | + "model": inputs["model"], |
| 167 | + "training_data": inputs["training_data"], |
| 168 | + "X_baseline": inputs["train_x"], |
| 169 | + "sampler": SobolQMCNormalSampler(torch.Size([4])), |
| 170 | + } |
| 171 | + |
| 172 | + for acqf_cls, kws in [ |
| 173 | + (qNoisyExpectedImprovement, {"prune_baseline": prune_baseline}), |
| 174 | + (qLogNoisyExpectedImprovement, {"prune_baseline": prune_baseline}), |
| 175 | + (qExpectedImprovement, {}), |
| 176 | + (qProbabilityOfImprovement, {}), |
| 177 | + (qLogExpectedImprovement, {}), |
| 178 | + ]: |
| 179 | + # Not working. |
| 180 | + if train_batch_shape.numel() > 1 and acqf_cls == qLogExpectedImprovement: |
| 181 | + continue |
| 182 | + input_constructor = get_acqf_input_constructor(acqf_cls=acqf_cls) |
| 183 | + |
| 184 | + with self.subTest( |
| 185 | + "no objective or constraints", |
| 186 | + train_batch_shape=train_batch_shape, |
| 187 | + prune_baseline=prune_baseline, |
| 188 | + test_batch_shape=test_batch_shape, |
| 189 | + acqf_cls=acqf_cls, |
| 190 | + ): |
| 191 | + acqf = acqf_cls(**input_constructor(**input_constructor_kwargs, **kws)) |
| 192 | + acq_val = acqf(test_x) |
| 193 | + self.assertEqual(acq_val.shape.numel(), test_x.shape[:-2].numel()) |
| 194 | + |
| 195 | + with self.subTest( |
| 196 | + "constrained", |
| 197 | + train_batch_shape=train_batch_shape, |
| 198 | + prune_baseline=prune_baseline, |
| 199 | + test_batch_shape=test_batch_shape, |
| 200 | + acqf_cls=acqf_cls, |
| 201 | + ): |
| 202 | + acqf = acqf_cls( |
| 203 | + **input_constructor( |
| 204 | + constraints=constraints, **input_constructor_kwargs, **kws |
| 205 | + ) |
| 206 | + ) |
| 207 | + self.assertEqual(acq_val.shape.numel(), test_x.shape[:-2].numel()) |
| 208 | + acq_val = acqf(test_x) |
| 209 | + |
| 210 | + def test_without_learned_objective(self) -> None: |
| 211 | + train_batch_shapes = [[], [1], [2]] |
| 212 | + test_batch_shapes = [[], [1], [3]] |
| 213 | + for train_batch_shape, test_batch_shape in product( |
| 214 | + train_batch_shapes, test_batch_shapes |
| 215 | + ): |
| 216 | + # Batched inputs `X_baseline` are currently unsupported by |
| 217 | + # prune_inferior_points |
| 218 | + prune_baseline_ = [False] if len(train_batch_shape) > 0 else [False, True] |
| 219 | + for prune_baseline in prune_baseline_: |
| 220 | + self._base_test_without_learned_objective( |
| 221 | + train_batch_shape=torch.Size(train_batch_shape), |
| 222 | + prune_baseline=prune_baseline, |
| 223 | + test_batch_shape=torch.Size(test_batch_shape), |
| 224 | + ) |
0 commit comments