gpytorch icon indicating copy to clipboard operation
gpytorch copied to clipboard

[Bug] BetaLikelihood behaviour is inconsistent with the other _OneDimensionalLikelihood subclasses.

Open wbeardall opened this issue 2 years ago • 1 comments

🐛 Bug

gpytorch.likelihoods.BetaLikelihood returns incorrect distributions when called. Instead of returning a Beta distribution of the same shape as the input distribution, it returns a Beta with size [gpytorch.settings.num_likelihood_samples.value(), input_shape]

To reproduce

** Code snippet to reproduce **

import torch
import gpytorch

data_size=50

f = gpytorch.distributions.MultivariateNormal(torch.rand(data_size),
                                              torch.eye(data_size))

print(f.shape())

likelihood = gpytorch.likelihoods.BetaLikelihood()

y = likelihood(f)

print(y.shape())

** Stack trace/error message **

torch.Size([50])
torch.Size([10, 50])

Expected Behavior

We should expect the return shape to match the input shape, as it is for the other subclasses of _OneDimensionalLikelihood.

System information

Please complete the following information: GPyTorch v.1.6.0 Torch v.1.10.0 OS Ubuntu 20.04

wbeardall avatar Mar 23 '22 14:03 wbeardall

This doesn't seem to necessarily be the case, although I agree the number of forwards samples driving the shapes here could be documented better:

likelihood = gpytorch.likelihoods.StudentTLikelihood()
likelihood(f).rsample().shape # also 10 x 50

likelihood = gpytorch.likelihoods.LaplaceLikelihood()
likelihood(f).rsample().shape # also 10 x 50

# although it doesn't hold for BernoulliLikelihood
likelihood = gpytorch.likelihoods.BernoulliLikelihood()
y = likelihood(f)
y.sample().shape # is 50

In the bernoulli setting, this is because it has an overwritten marginal function, while the other three do not. I believe that this design choice is because of how E_{q(f)}(\log p(y| f)) is calculated during training (closed form? for the bernoulli but not for the others?).

@gpleiss Is the above sentence actually the case?

wjmaddox avatar Mar 28 '22 14:03 wjmaddox

I encountered this issue while trying to do beta regression. I was extremely confused why likelihood(model(train_x)).sample(sample_shape=torch.Size([N])).shape == [N, 10, len(x)] and not just [N, len(x)]. How are you supposed to interpret that extra dimension? What is the correct way to get a single distribution of values for each x?

crasanders avatar Jan 25 '23 22:01 crasanders

The included 10 here should be the number of quadrature samples to evaluate E_{q(f)}(\log p(y| f)). I believe these samples should automatically go way when you evaluate the likelhood.

wjmaddox avatar Jan 27 '23 15:01 wjmaddox

Unless I'm misunderstanding something, the samples don't just go away. Using the example above, if I call samples = y.sample(torch.Size([100])), I would expect to get a [100, 50] tensor back, but I actually get a [100, 10, 50] tensor.

crasanders avatar Jan 30 '23 14:01 crasanders

This is the correct behavior. See the new proposed documentation

gpleiss avatar Mar 07 '23 02:03 gpleiss