Skip to content

[Bug] model_list_to_batched ignores the covar module of the input models #1411

@saitcakmak

Description

@saitcakmak

🐛 Bug

model_list_to_batched ignores the covariance module of the input model-list, and produces a batched model with the default covariance module. If the input model-list covar module does not have priors compatible with the default covar module, then it just errors out.

Originally reported in #1409.

To reproduce

** Code snippet to reproduce **

import gpytorch
import torch
from botorch.models.converter import model_list_to_batched
from botorch.models.gp_regression import SingleTaskGP
from botorch.models.model_list_gp_regression import ModelListGP
from gpytorch.kernels.rbf_kernel import RBFKernel
from gpytorch.kernels.scale_kernel import ScaleKernel

train_X = torch.rand(5, 2)
train_Y = torch.randn(5, 2)
model_list = ModelListGP(
    *[
        SingleTaskGP(
            train_X=train_X,
            train_Y=train_Y[:, i : i + 1],
            covar_module=ScaleKernel(
                base_kernel=RBFKernel(  # Note the RBF kernel here.
                    ard_num_dims=2,
                    lengthscale_prior=gpytorch.priors.GammaPrior(3.0, 6.0),
                ),
                outputscale_prior=gpytorch.priors.GammaPrior(2.0, 0.15),
            ),
        )
        for i in range(2)
    ]
)

batched_model = model_list_to_batched(model_list)
# Has Matern kernel with the default priors.
print(batched_model)

** Stack trace/error message **

SingleTaskGP(
  (likelihood): GaussianLikelihood(
    (noise_covar): HomoskedasticNoise(
      (noise_prior): GammaPrior()
      (raw_noise_constraint): GreaterThan(1.000E-04)
    )
  )
  (mean_module): ConstantMean()
  (covar_module): ScaleKernel(
    (base_kernel): MaternKernel(
      (lengthscale_prior): GammaPrior()
      (raw_lengthscale_constraint): Positive()
    )
    (outputscale_prior): GammaPrior()
    (raw_outputscale_constraint): Positive()
  )
)

If the priors are commented out:

'ScaleKernel' object has no attribute 'outputscale_prior'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/mnt/xarfuse/uid-352651/c1da8b8f-seed-nspid4026531836_cgpid13510401-ns-4026531840/gpytorch/module.py in __getattr__(self, name)
    432             try:
--> 433                 return super().__getattribute__(name)
    434             except AttributeError:
AttributeError: 'ScaleKernel' object has no attribute 'outputscale_prior'

During handling of the above exception, another exception occurred:
AttributeError                            Traceback (most recent call last)
<ipython-input-130-a5251b88a562> in <module>
     26 )
     27 
---> 28 batched_model = model_list_to_batched(model_list)
     29 # Has Matern kernel with the default priors.
     30 print(batched_model)
/mnt/xarfuse/uid-352651/c1da8b8f-seed-nspid4026531836_cgpid13510401-ns-4026531840/botorch/models/converter.py in model_list_to_batched(model_list)
    163     # ensure scalars agree (TODO: Allow different priors for different outputs)
    164     for n in non_adjusted_batch_keys:
--> 165         v0 = _get_module(models[0], n)
    166         if not all(torch.equal(_get_module(m, n), v0) for m in models[1:]):
    167             raise UnsupportedError("All scalars must have the same value.")
/mnt/xarfuse/uid-352651/c1da8b8f-seed-nspid4026531836_cgpid13510401-ns-4026531840/botorch/models/converter.py in _get_module(module, name)
     45     if name != "":
     46         for a in name.split("."):
---> 47             current = getattr(current, a)
     48     return current
     49 
/mnt/xarfuse/uid-352651/c1da8b8f-seed-nspid4026531836_cgpid13510401-ns-4026531840/gpytorch/module.py in __getattr__(self, name)
    433                 return super().__getattribute__(name)
    434             except AttributeError:
--> 435                 raise e
    436 
    437 
/mnt/xarfuse/uid-352651/c1da8b8f-seed-nspid4026531836_cgpid13510401-ns-4026531840/gpytorch/module.py in __getattr__(self, name)
    428     def __getattr__(self, name):
    429         try:
--> 430             return super().__getattr__(name)
    431         except AttributeError as e:
    432             try:
/mnt/xarfuse/uid-352651/c1da8b8f-seed-nspid4026531836_cgpid13510401-ns-4026531840/torch/nn/modules/module.py in __getattr__(self, name)
   1263             if name in modules:
   1264                 return modules[name]
-> 1265         raise AttributeError("'{}' object has no attribute '{}'".format(
   1266             type(self).__name__, name))
   1267 
AttributeError: 'ScaleKernel' object has no attribute 'outputscale_prior'

Expected Behavior

The returned model should have the same type of covar module as the input model.

System information

BoTorch latest.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions