Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify PenalizedMCObjective to support non-batch eval #2073

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions botorch/acquisition/penalized.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ def forward(self, samples: Tensor, X: Optional[Tensor] = None) -> Tensor:
if self.expand_dim is not None:
# reshape penalty_obj to match the dim
penalty_obj = penalty_obj.unsqueeze(self.expand_dim)
# this happens when samples is a `q x m`-dim tensor and X is a `q x d`-dim
# tensor; obj returned from GenericMCObjective is a `q`-dim tensor and
# penalty_obj is a `1 x q`-dim tensor.
if obj.ndim == 1:
assert penalty_obj.shape == torch.Size([1, samples.shape[-2]])
penalty_obj = penalty_obj.squeeze(dim=0)
return obj - self.regularization_parameter * penalty_obj


Expand Down
2 changes: 1 addition & 1 deletion test/acquisition/test_penalized.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def test_penalized_mc_objective(self):
samples = torch.randn(4, 3, device=self.device, dtype=dtype)
X = torch.randn(4, 5, device=self.device, dtype=dtype)
penalized_obj = generic_obj(samples) - 0.1 * l1_penalty_obj(X)
self.assertTrue(torch.equal(obj(samples, X), penalized_obj))
self.assertTrue(torch.equal(obj(samples, X), penalized_obj.squeeze(0)))
# test 'q x d' Tensor X
samples = torch.randn(4, 2, 3, device=self.device, dtype=dtype)
X = torch.randn(2, 5, device=self.device, dtype=dtype)
Expand Down