Skip to content

Fix handling of prior terms in ExactMarginalLogLikelihood #2039

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

Merged
merged 9 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion gpytorch/mlls/exact_marginal_log_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ def _add_other_terms(self, res, params):
res = res.add(added_loss_term.loss(*params))

# Add log probs of priors on the (functions of) parameters
res_ndim = res.ndim
for name, module, prior, closure, _ in self.named_priors():
res.add_(prior.log_prob(closure(module)).sum())
prior_term = prior.log_prob(closure(module))
while prior_term.ndim > res_ndim:
prior_term = prior_term.sum(dim=-1)
res.add_(prior_term)

return res

Expand Down
67 changes: 67 additions & 0 deletions test/mlls/test_exact_marginal_log_likelihood.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3

import unittest

import gpytorch
import torch
from gpytorch.constraints.constraints import GreaterThan
from gpytorch.kernels.matern_kernel import MaternKernel
from gpytorch.kernels.scale_kernel import ScaleKernel
from gpytorch.likelihoods.gaussian_likelihood import GaussianLikelihood
from gpytorch.means.constant_mean import ConstantMean
from gpytorch.mlls.exact_marginal_log_likelihood import ExactMarginalLogLikelihood
from gpytorch.priors.torch_priors import GammaPrior


class ExactGPModel(gpytorch.models.ExactGP):
def __init__(self, train_x, train_y):
batch_shape = train_x.shape[:-2]
noise_prior = GammaPrior(1.1, 0.05)
noise_prior_mode = (noise_prior.concentration - 1) / noise_prior.rate
likelihood = GaussianLikelihood(
noise_prior=noise_prior,
batch_shape=batch_shape,
noise_constraint=GreaterThan(
1e-4,
transform=None,
initial_value=noise_prior_mode,
),
)
super().__init__(train_x, train_y, likelihood)
self.mean_module = gpytorch.means.ConstantMean(batch_shape=batch_shape)
self.covar_module = ScaleKernel(
MaternKernel(
nu=2.5,
ard_num_dims=train_x.shape[-1],
batch_shape=batch_shape,
lengthscale_prior=GammaPrior(3.0, 6.0),
),
batch_shape=batch_shape,
outputscale_prior=GammaPrior(2.0, 0.15),
)

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


class TestExactMarginalLogLikelihood(unittest.TestCase):
def test_batched_eval(self):
train_x = torch.rand(10, 2)
train_y = torch.randn(10)
non_batch_model = ExactGPModel(train_x, train_y)
mll = ExactMarginalLogLikelihood(non_batch_model.likelihood, non_batch_model)
output = non_batch_model(train_x)
non_batch_mll_eval = mll(output, train_y)

train_x = train_x.expand(10, -1, -1)
train_y = train_y.expand(10, -1)
batch_model = ExactGPModel(train_x, train_y)
mll = ExactMarginalLogLikelihood(batch_model.likelihood, batch_model)
output = batch_model(train_x)
batch_mll_eval = mll(output, train_y)

self.assertEqual(non_batch_mll_eval.shape, torch.Size())
self.assertEqual(batch_mll_eval.shape, torch.Size([10]))
self.assertTrue(torch.allclose(non_batch_mll_eval.expand(10), batch_mll_eval))