'How to resolve the error encountered while using GPyTorch with SpectralMixture Kernel?

I am using GPyTorch for fitting a gaussian process regression model (primarily for the learning process). While following their tutorial, I am trying to use SpectralMixtureKernel. However, I am getting the following error. But first here is the code (which is basically the same as their tutorial, but for convenience, replicated here):

class ExactGPModel(gpytorch.models.ExactGP):
    def __init__(self,train_x,train_y,likelihood):
        super(ExactGPModel, self).__init__(train_x,train_y,likelihood)
        self.mean_module = gpytorch.means.ConstantMean()

        self.covar_module = gpytorch.kernels.SpectralMixtureKernel(num_mixtures=4)

        self.covar_module.initialize_from_data(train_x, train_y)



    def forward(self,x):
        mean_x = self.mean_module(x)
        covar_x = self.covar_module(x)
        return gpytorch.distributions.MultivariateNormal(mean_x,covar_x)

pandas dataframe converted to torch.tensor below

train_x = torch.tensor(train_x.values.astype(np.float32))
train_y = torch.tensor(train_y.values.astype(np.float32))

test_x = torch.tensor(test_x.values.astype(np.float32))
test_y = torch.tensor(test_y.values.astype(np.float32))

Then

likelihood = gpytorch.likelihoods.GaussianLikelihood()

model = ExactGPModel(train_x,train_y, likelihood)

Once the last line is run, I am getting the following error:

Traceback (most recent call last):

  File "<ipython-input-195-e3bc37af324c>", line 1, in <module>
    model = ExactGPModel(train_x,train_y, likelihood)

  File "<ipython-input-186-323eff9c5819>", line 7, in __init__
    self.covar_module.initialize_from_data(train_x, train_y)

  File "/anaconda3/envs/py36/lib/python3.6/site-packages/gpytorch/kernels/spectral_mixture_kernel.py", line 163, in initialize_from_data
    self.raw_mixture_scales.data.normal_().mul_(max_dist).abs_().pow_(-1)

RuntimeError: output with shape [4, 1, 1] doesn't match the broadcast shape [4, 1, 33]

Any help to resolve this issue would be appreciated.

Thanks.



Solution 1:[1]

I was having the same problem. In my case I was using a train_x vector with a dimension larger than 1. In these cases you should set the ard_num_dims parameter

self.covar_module = gpytorch.kernels.SpectralMixtureKernel(num_mixtures=4, ard_num_dims=33)

More info in https://docs.gpytorch.ai/en/latest/kernels.html#spectralmixturekernel

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 innicoder