英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论