Skip to content

Commit

Permalink
Expand composite observations to full size
Browse files Browse the repository at this point in the history
  • Loading branch information
ruicoelhopedro committed Mar 29, 2024
1 parent 80d82fd commit ee80d5e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion piglot/optimisers/botorch/bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ def _build_acquisition_composite(
# Build composite MC objective
_, y_avg, y_std = dataset.get_obervation_stats()
mc_objective = GenericMCObjective(
lambda vals, X: -self.objective.composition.composition_torch(vals * y_std + y_avg)
lambda vals, X: -self.objective.composition.composition_torch(
dataset.expand_observations(vals * y_std + y_avg)
)
)
# Delegate acquisition building
best = torch.max(dataset.values).item()
Expand Down
25 changes: 25 additions & 0 deletions piglot/optimisers/botorch/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ def get_obervation_stats(self) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor
y_std = y_std[mask]
return mask, y_avg, y_std

def expand_observations(self, reduced: torch.Tensor) -> torch.Tensor:
"""Expand reduced observations to full size.
Parameters
----------
reduced : torch.Tensor
Reduced observations.
Returns
-------
torch.Tensor
Expanded observations.
"""
# Infer the shape of the expanded tensor: only modify the last dimension
new_shape = list(reduced.shape)
new_shape[-1] = self.n_outputs
expanded = torch.empty(new_shape, dtype=self.dtype, device=self.device)
# Collapse the tensor to a 2D array for indexing the last dimension
expanded_flat = expanded.view(-1, self.n_outputs)
mask, _, _ = self.get_obervation_stats()
expanded_flat[:, mask] = reduced.view(-1, reduced.shape[-1])
# Fill the missing values with the average of the observed ones
y_avg = torch.mean(self.values, dim=-2)
expanded_flat[:, ~mask] = y_avg[~mask]
# Note: we are using a view, so the expanded tensor is already modified
return expanded

def standardised(self) -> BayesDataset:
"""Return a dataset with unit-cube parameters and standardised outputs.
Expand Down

0 comments on commit ee80d5e

Please sign in to comment.