From a20b39a29629d9f2ecdbf1f152eccee7de825ca5 Mon Sep 17 00:00:00 2001 From: SaiAakash Date: Thu, 12 Sep 2024 00:30:22 +0100 Subject: [PATCH 1/8] added posterior_transform to posterior method --- botorch/models/approximate_gp.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/botorch/models/approximate_gp.py b/botorch/models/approximate_gp.py index 5bd4beecb6..d71c11019b 100644 --- a/botorch/models/approximate_gp.py +++ b/botorch/models/approximate_gp.py @@ -137,7 +137,13 @@ def train(self: TApproxModel, mode: bool = True) -> TApproxModel: return Module.train(self, mode=mode) def posterior( - self, X, output_indices=None, observation_noise=False, *args, **kwargs + self, + X, + output_indices=None, + observation_noise=False, + posterior_transform=None, + *args, + **kwargs, ) -> GPyTorchPosterior: self.eval() # make sure model is in eval mode @@ -157,6 +163,8 @@ def posterior( posterior = GPyTorchPosterior(distribution=dist) if hasattr(self, "outcome_transform"): posterior = self.outcome_transform.untransform_posterior(posterior) + if posterior_transform is not None: + posterior = posterior_transform(posterior) return posterior def forward(self, X) -> MultivariateNormal: From 0aedb371524c965bf1100a5949fa47088ab11d57 Mon Sep 17 00:00:00 2001 From: SaiAakash Date: Thu, 12 Sep 2024 09:33:36 +0100 Subject: [PATCH 2/8] removed catch-alls; added unit test --- botorch/models/approximate_gp.py | 4 +--- test/models/test_approximate_gp.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/botorch/models/approximate_gp.py b/botorch/models/approximate_gp.py index d71c11019b..2b12b346ba 100644 --- a/botorch/models/approximate_gp.py +++ b/botorch/models/approximate_gp.py @@ -142,8 +142,6 @@ def posterior( output_indices=None, observation_noise=False, posterior_transform=None, - *args, - **kwargs, ) -> GPyTorchPosterior: self.eval() # make sure model is in eval mode @@ -158,7 +156,7 @@ def posterior( X = X.unsqueeze(-3).repeat(*[1] * (X_ndim - 2), self.num_outputs, 1, 1) dist = self.model(X) if observation_noise: - dist = self.likelihood(dist, *args, **kwargs) + dist = self.likelihood(dist) posterior = GPyTorchPosterior(distribution=dist) if hasattr(self, "outcome_transform"): diff --git a/test/models/test_approximate_gp.py b/test/models/test_approximate_gp.py index ed7cba18cf..f49b07cf57 100644 --- a/test/models/test_approximate_gp.py +++ b/test/models/test_approximate_gp.py @@ -20,6 +20,7 @@ GreedyImprovementReduction, GreedyVarianceReduction, ) +from botorch.acquisition.objective import ScalarizedPosteriorTransform from botorch.posteriors import GPyTorchPosterior, TransformedPosterior from botorch.utils.testing import BotorchTestCase from gpytorch.likelihoods import GaussianLikelihood, MultitaskGaussianLikelihood @@ -102,6 +103,16 @@ def test_posterior(self): # test batch_shape property self.assertEqual(model.batch_shape, tx.shape[:-2]) + # Test that checks if posterior_transfomr is correctly applied + [tx1, ty1, test1] = all_tests["non_batched_mo"] + model1 = SingleTaskVariationalGP(tx1, ty1, inducing_points=tx) + posterior_transform = ScalarizedPosteriorTransform( + weights=torch.tensor([1.0, 1.0]) + ) + posterior1 = model1.posterior(test1, posterior_transform=posterior_transform) + self.assertIsInstance(posterior1, GPyTorchPosterior) + self.assertEqual(posterior1.mean.shape[1], 1) + def test_variational_setUp(self): for dtype in [torch.float, torch.double]: train_X = torch.rand(10, 1, device=self.device, dtype=dtype) From c85f0e36d23e054a27af9abbddba055ae161fd10 Mon Sep 17 00:00:00 2001 From: SaiAakash Date: Thu, 12 Sep 2024 09:35:19 +0100 Subject: [PATCH 3/8] fixed test --- test/models/test_approximate_gp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/models/test_approximate_gp.py b/test/models/test_approximate_gp.py index f49b07cf57..183f8f3c76 100644 --- a/test/models/test_approximate_gp.py +++ b/test/models/test_approximate_gp.py @@ -105,7 +105,7 @@ def test_posterior(self): # Test that checks if posterior_transfomr is correctly applied [tx1, ty1, test1] = all_tests["non_batched_mo"] - model1 = SingleTaskVariationalGP(tx1, ty1, inducing_points=tx) + model1 = SingleTaskVariationalGP(tx1, ty1, inducing_points=tx1) posterior_transform = ScalarizedPosteriorTransform( weights=torch.tensor([1.0, 1.0]) ) From e4db43070dfc130fd986fe339ed35b7b23bb19c6 Mon Sep 17 00:00:00 2001 From: SaiAakash Date: Thu, 12 Sep 2024 09:38:06 +0100 Subject: [PATCH 4/8] fixed typo in comment --- test/models/test_approximate_gp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/models/test_approximate_gp.py b/test/models/test_approximate_gp.py index 183f8f3c76..9df427904f 100644 --- a/test/models/test_approximate_gp.py +++ b/test/models/test_approximate_gp.py @@ -103,7 +103,7 @@ def test_posterior(self): # test batch_shape property self.assertEqual(model.batch_shape, tx.shape[:-2]) - # Test that checks if posterior_transfomr is correctly applied + # Test that checks if posterior_transform is correctly applied [tx1, ty1, test1] = all_tests["non_batched_mo"] model1 = SingleTaskVariationalGP(tx1, ty1, inducing_points=tx1) posterior_transform = ScalarizedPosteriorTransform( From 774c227c28a598a0113f1a5b3b08bdc29b652bf3 Mon Sep 17 00:00:00 2001 From: Sait Cakmak Date: Fri, 13 Sep 2024 20:22:54 +0200 Subject: [PATCH 5/8] fix import ordering, add type hints --- botorch/models/approximate_gp.py | 11 +++++++---- test/models/test_approximate_gp.py | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/botorch/models/approximate_gp.py b/botorch/models/approximate_gp.py index 2b12b346ba..f1380b5e2a 100644 --- a/botorch/models/approximate_gp.py +++ b/botorch/models/approximate_gp.py @@ -31,7 +31,6 @@ import copy import warnings - from typing import Optional, TypeVar, Union import torch @@ -139,10 +138,14 @@ def train(self: TApproxModel, mode: bool = True) -> TApproxModel: def posterior( self, X, - output_indices=None, - observation_noise=False, - posterior_transform=None, + output_indices: Optional[list[int]] = None, + observation_noise: bool = False, + posterior_transform: Optional[PosteriorTransform] = None, ) -> GPyTorchPosterior: + if output_indices is not None: + raise NotImplementedError( # pragma: no cover + f"{self.__class__.__name__}.posterior does not support output indices." + ) self.eval() # make sure model is in eval mode # input transforms are applied at `posterior` in `eval` mode, and at diff --git a/test/models/test_approximate_gp.py b/test/models/test_approximate_gp.py index 9df427904f..a075484f4a 100644 --- a/test/models/test_approximate_gp.py +++ b/test/models/test_approximate_gp.py @@ -8,6 +8,7 @@ import warnings import torch +from botorch.acquisition.objective import ScalarizedPosteriorTransform from botorch.fit import fit_gpytorch_mll from botorch.models.approximate_gp import ( _SingleTaskVariationalGP, @@ -20,7 +21,6 @@ GreedyImprovementReduction, GreedyVarianceReduction, ) -from botorch.acquisition.objective import ScalarizedPosteriorTransform from botorch.posteriors import GPyTorchPosterior, TransformedPosterior from botorch.utils.testing import BotorchTestCase from gpytorch.likelihoods import GaussianLikelihood, MultitaskGaussianLikelihood From 0e0afe101986d80127b14a35647d4b081a119905 Mon Sep 17 00:00:00 2001 From: Sait Cakmak Date: Fri, 13 Sep 2024 20:24:37 +0200 Subject: [PATCH 6/8] fix missing import --- botorch/models/approximate_gp.py | 1 + 1 file changed, 1 insertion(+) diff --git a/botorch/models/approximate_gp.py b/botorch/models/approximate_gp.py index f1380b5e2a..64e19ed79b 100644 --- a/botorch/models/approximate_gp.py +++ b/botorch/models/approximate_gp.py @@ -34,6 +34,7 @@ from typing import Optional, TypeVar, Union import torch +from botorch.acquisition.objective import PosteriorTransform from botorch.models.gpytorch import GPyTorchModel from botorch.models.transforms.input import InputTransform from botorch.models.transforms.outcome import OutcomeTransform From 9710853bed9c4410f9364ed60a245db57fff5ae0 Mon Sep 17 00:00:00 2001 From: Max Balandat Date: Mon, 16 Sep 2024 07:17:52 -0700 Subject: [PATCH 7/8] remove stray TypeVar --- botorch/models/approximate_gp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/botorch/models/approximate_gp.py b/botorch/models/approximate_gp.py index a2e7586546..1cf32d5ccf 100644 --- a/botorch/models/approximate_gp.py +++ b/botorch/models/approximate_gp.py @@ -31,7 +31,7 @@ import copy import warnings -from typing import Optional, TypeVar, Union +from typing import Optional, Union import torch from botorch.acquisition.objective import PosteriorTransform From 15966b4b969d2c7e132c2e6de81cf2a9f68696a6 Mon Sep 17 00:00:00 2001 From: Max Balandat Date: Mon, 16 Sep 2024 09:12:51 -0700 Subject: [PATCH 8/8] Fix cuda test error --- test/models/test_approximate_gp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/models/test_approximate_gp.py b/test/models/test_approximate_gp.py index a31d471b50..e660845bed 100644 --- a/test/models/test_approximate_gp.py +++ b/test/models/test_approximate_gp.py @@ -108,7 +108,7 @@ def test_posterior(self): [tx1, ty1, test1] = all_tests["non_batched_mo"] model1 = SingleTaskVariationalGP(tx1, ty1, inducing_points=tx1) posterior_transform = ScalarizedPosteriorTransform( - weights=torch.tensor([1.0, 1.0]) + weights=torch.tensor([1.0, 1.0], device=self.device) ) posterior1 = model1.posterior(test1, posterior_transform=posterior_transform) self.assertIsInstance(posterior1, GPyTorchPosterior)