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

[Bug]: ModelListGP can't fantisize samples with observation_noise unless masked #2734

Closed
1 task done
swierh opened this issue Feb 6, 2025 · 0 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@swierh
Copy link
Contributor

swierh commented Feb 6, 2025

What happened?

When using the ModelListGP.fantasize method, using observation_noise doesn't work, unless the evaluation_mask is also specified.


While looping over the outputs, the observation_noise_i is only set when there's a mask: https://github.com/pytorch/botorch/blob/main/botorch/models/model.py#L659

It should also be set when there's no mask.

Suggested fix, within the corresponding else statement, add:

if observation_noise is not None:
    observation_noise_i = observation_noise[..., i : i + 1]

Please provide a minimal, reproducible example of the unexpected behavior.

import torch
from botorch.fit import fit_gpytorch_mll
from botorch.models.gp_regression import SingleTaskGP
from botorch.models.model_list_gp_regression import ModelListGP
from botorch.sampling.list_sampler import ListSampler
from botorch.sampling.normal import SobolQMCNormalSampler
from gpytorch.mlls.sum_marginal_log_likelihood import SumMarginalLogLikelihood

model = ModelListGP(
    *[SingleTaskGP(train_X=torch.tensor([[0.0], [1.0]]), train_Y=torch.tensor([[0.0], [1.0]])) for _ in range(2)]
)
mll = SumMarginalLogLikelihood(model.likelihood, model)
mll = fit_gpytorch_mll(mll)
sampler = ListSampler(*[SobolQMCNormalSampler(sample_shape=torch.Size([1])) for _ in range(2)])

# Works:
model.fantasize(
    torch.tensor([[0.5]]),
    sampler=sampler,
    observation_noise=torch.tensor([[0.2, 0.2]]),
    evaluation_mask=torch.tensor([[1.0, 1.0]], dtype=torch.bool),
)

# Doesn't work:
model.fantasize(
    torch.tensor([[0.5]]),
    sampler=sampler,
    observation_noise=torch.tensor([[0.2, 0.2]]),
)

Please paste any relevant traceback/logs produced by the example provided.

File [...]/botorch/models/model.py:668, in ModelList.fantasize(self, X, sampler, observation_noise, evaluation_mask, **kwargs)
    660     else:
    661         sampler_i = (
    662             sampler.samplers[i] if isinstance(sampler, ListSampler) else sampler
    663         )
    665     fant_model = self.models[i].fantasize(
    666         X=X_i,
    667         sampler=sampler_i,
--> 668         observation_noise=observation_noise_i,
    669         **kwargs,
    670     )
    671     fant_models.append(fant_model)
    672 return self.__class__(*fant_models)

UnboundLocalError: cannot access local variable 'observation_noise_i' where it is not associated with a value

BoTorch Version

0.13.0

Python Version

3.11

Operating System

macOS 15.3

Code of Conduct

  • I agree to follow BoTorch's Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant