将任务协方差矩阵设置为GPyTorch中的相关矩阵

huangapple go评论65阅读模式
英文:

Setting the task covariance matrix to the correlation matrix in GPyTorch

问题

I am trying to set the calculated correlation matrix as the task covariance in GPyTorch for a numerical experiment. In case you are wondering, the data is normalized already.

corr_matrix = ...
corr_matrix = torch.from_numpy(corr_matrix)

I define a very simplistic model the following way:

class MultitaskGPModel(gpytorch.models.ExactGP):
    def __init__(self, train_x, train_y, likelihood):
        super(MultitaskGPModel, self).__init__(train_x, train_y, likelihood)
        self.mean_module = gpytorch.means.MultitaskMean(
            gpytorch.means.ConstantMean(), num_tasks=23
        )
        self.covar_module = gpytorch.kernels.MultitaskKernel(
            gpytorch.kernels.RBFKernel(), num_tasks=23, rank=23
        )

    def forward(self, x):
        mean_x = self.mean_module(x)
        covar_x = self.covar_module(x)
        print(covar_x)
        return gpytorch.distributions.MultitaskMultivariateNormal(mean_x, covar_x)
likelihood = gpytorch.likelihoods.MultitaskGaussianLikelihood(num_tasks=23)
model = MultitaskGPModel(train_x.float(), train_y.float(), likelihood)

From what I understood a way to fix the parameter would be something like this:

model.covar_module.task_covar_module._parameters['covar_factor'] = torch.from_numpy(corr_matrix)

I don't touch anything on the optimizer, given that the tensor I use to give the covariance matrix has the argument requires_grad = False

Nonetheless I got this error:

RuntimeError: KroneckerProductLinearOperator expects lazy tensors with the same batch shapes. Got [torch.Size([]), torch.Size([23])].

I don't really understand how to insert this parameter so it respects the Kronecker product evaluation. So I would appreciate any help.

英文:

I am trying to set the calculated correlation matrix as the task covariance in GPyTorch for a numerical experiment. In case you are wondering, the data is normalized already.

corr_matrix = ...
corr_matrix = torch.from_numpy(corr_matrix)

I define a very simplistic model the following way:

class MultitaskGPModel(gpytorch.models.ExactGP):
    def __init__(self, train_x, train_y, likelihood):
        super(MultitaskGPModel, self).__init__(train_x, train_y, likelihood)
        self.mean_module = gpytorch.means.MultitaskMean(
            gpytorch.means.ConstantMean(), num_tasks=23
        )
        self.covar_module = gpytorch.kernels.MultitaskKernel(
            gpytorch.kernels.RBFKernel(), num_tasks=23, rank=23)
        )

    def forward(self, x):
        mean_x = self.mean_module(x)
        covar_x = self.covar_module(x)
        print(covar_x)
        return gpytorch.distributions.MultitaskMultivariateNormal(mean_x, covar_x)
likelihood = gpytorch.likelihoods.MultitaskGaussianLikelihood(num_tasks=23)
model = MultitaskGPModel(train_x.float(), train_y.float(), likelihood)

From what I understood a way to fix the parameter would be something like this:

model.covar_module.task_covar_module._parameters['covar_factor'] = torch.from_numpy(corr_matrix)

I don't touch anything on the optimizer, given that the tensor I use to give the covariance matrix has the argument requires_grad = False

Nonetheless I got this error:

RuntimeError: KroneckerProductLinearOperator expects lazy tensors with the same batch shapes. Got [torch.Size([]), torch.Size([23])].

I don't really understand how to insert this parameter so it respects the Kronecker product evaluation. So I would appreciate any help.

答案1

得分: 0

我找到问题所在了。我遇到了一个不良的维度设置。

model.covar_module.task_covar_module._parameters['raw_var'] = torch.zeros_like(model.covar_module.task_covar_module._parameters['raw_var'])
model.covar_module.task_covar_module._parameters['covar_factor'] = torch.from_numpy(corr_matrix).float()

这样问题就解决了。

英文:

So, I found out what the problem was. I was having a bad dimensionality set up.

model.covar_module.task_covar_module._parameters['raw_var'] = torch.zeros_like(model.covar_module.task_covar_module._parameters['raw_var'])
model.covar_module.task_covar_module._parameters['covar_factor'] = torch.from_numpy(corr_matrix).float()

That way it got solved

huangapple
  • 本文由 发表于 2023年5月15日 14:56:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定