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] GP Output Standardization Without Specified output_transform #2547

Closed
stefanpricopie opened this issue Sep 21, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@stefanpricopie
Copy link

🐛 Bug

When using BoTorch's SingleTaskGP model without specifying an output_transform, the GP model in Google Colab seems to standardize the training targets automatically. In Colab, gp.train_targets is different from Y while on MacOS, the GP model behaves as expected.

This issue might be related to Issue #2533, although the issue there seems to be with 'input_transform' rather than 'output_transform'.

To reproduce

import torch
from botorch.models import SingleTaskGP
from gpytorch.mlls import ExactMarginalLogLikelihood
from botorch.fit import fit_gpytorch_mll
import matplotlib.pyplot as plt

torch.manual_seed(124)

dim = 1
train_X = torch.rand(100, dim, dtype=torch.double) * 2
Y = 1 - torch.linalg.norm(train_X - 0.5, dim=-1, keepdim=True)

train_Yvar = torch.full_like(Y, 1e-4)  # Adding noise variance

# No output transformation is provided
gp = SingleTaskGP(train_X=train_X, train_Y=Y, train_Yvar=train_Yvar)

mll = ExactMarginalLogLikelihood(gp.likelihood, gp)
fit_gpytorch_mll(mll)

print(gp.train_inputs[0][:5])
print(train_X[:5])

print(gp.train_targets[:5])
print(Y[:5])

# Generate test points for evaluation
test_x = torch.linspace(0, 2, 100, dtype=torch.double).unsqueeze(-1)

# Plot the GP predictions
with torch.no_grad():
    observed_pred = gp.likelihood(gp(test_x))
    lower, upper = observed_pred.confidence_region()

# Create two subplots: one for GP and one for EI
fig, ax = plt.subplots(1, 1, figsize=(8, 5))

# Plot the GP mean and confidence interval on the first subplot
with torch.no_grad():
    observed_pred = gp.likelihood(gp(test_x))
    lower, upper = observed_pred.confidence_region()

ax.plot(test_x.numpy(), observed_pred.mean.numpy(), 'b', label='GP mean')
ax.fill_between(test_x.numpy().flatten(), lower.numpy().flatten(), upper.numpy().flatten(), alpha=0.2, color='blue')
ax.plot(train_X.numpy(), Y.numpy(), 'ro', label='Training points')

# Enhancements for clarity on the first subplot (Surrogate model)
ax.set_ylabel('y')
ax.set_title('Gaussian Process Surrogate Model')
ax.legend()
plt.show()

** Stack trace/error message **

tensor([[0.4418],
        [1.4512],
        [0.1962],
        [0.9559],
        [1.7554]], dtype=torch.float64)

tensor([[0.4418],
        [1.4512],
        [0.1962],
        [0.9559],
        [1.7554]], dtype=torch.float64)

tensor([ 1.2871, -0.5817,  0.7731,  0.4548, -1.2182], dtype=torch.float64)

tensor([[ 0.9418],
        [ 0.0484],
        [ 0.6962],
        [ 0.5441],
        [-0.2554]], dtype=torch.float64)

colab

Expected Behavior

tensor([[0.4418],
        [1.4512],
        [0.1962],
        [0.9559],
        [1.7554]], dtype=torch.float64)

tensor([[0.4418],
        [1.4512],
        [0.1962],
        [0.9559],
        [1.7554]], dtype=torch.float64)

tensor([ 0.9418,  0.0488,  0.6962,  0.5441, -0.2554], dtype=torch.float64)

tensor([[ 0.9418],
        [ 0.0484],
        [ 0.6962],
        [ 0.5441],
        [-0.2554]], dtype=torch.float64)

macos

System information

  • BoTorch Version (run print(botorch.__version__)):
    • Colab: 0.12.0
    • MacOS: 0.12.0 (though botorch.__version__ returns Unknown)
  • GPyTorch Version (run print(gpytorch.__version__)):
    • Colab: 1.13
    • MacOS: 1.13
  • PyTorch Version (run print(torch.__version__)):
    • Colab: 2.4.1+cu121
    • MacOS: 2.4.1
  • Python Version:
    • Colab: 3.10.12
    • MacOS: 3.11.9
  • Computer OS:
    • Colab: Linux 6.1.85+
    • MacOS: Darwin 24.0.0

Thanks for looking into this!

@stefanpricopie stefanpricopie added the bug Something isn't working label Sep 21, 2024
@Balandat
Copy link
Contributor

As indicated in the "major changes" of the 0.12 release https://github.com/pytorch/botorch/releases/tag/v0.12.0, we

Use Standarize by default in all the models using the upgraded priors. In addition to reducing the amount of boilerplate needed to initialize a model, this change was motivated by the change to default priors, because the new
priors will work less well when data is not standardized. Users who do not want to use transforms should explicitly pass in None (#2458, #2532).

So you probably are using different botorch versions. If you use outcome_transform=None your code will behave consistently across versions.

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

No branches or pull requests

2 participants