question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Sampling from priors doesn't match shape of hyperparameters

See original GitHub issue

🐛 Bug

I found some unexpected interactions between ard_num_dims and the shapes of priors for kernels – a few settings where if I sample from a hyperparameter prior I don’t get a tensor the same shape as the hyperparameter. I’m not sure if all of these are intended or not, but looks like a bug to me.

To reproduce

import torch
from gpytorch.priors import GammaPrior, NormalPrior
from gpytorch.kernels import RBFKernel

# make a kernel
scales = torch.Tensor([1,1])
kernel = RBFKernel(
    ard_num_dims=2,
    lengthscale_prior=GammaPrior(3.0, 6.0 / scales),
)
new_lengthscale = kernel.lengthscale_prior.sample(kernel.lengthscale.shape)
print(kernel.lengthscale.shape) # size 1,2
print(new_lengthscale.shape) # size 1,2,2, if I try to assign it back I get an error

# same with another prior
kernel2 = RBFKernel(
    ard_num_dims=2,
    lengthscale_prior=NormalPrior(loc=10, scale=scales)
)

new_lengthscale = kernel2.lengthscale_prior.sample(kernel2.lengthscale.shape)
print(kernel2.lengthscale.shape) # size 1,2
print(new_lengthscale.shape) # size 1, 2, 2

# ard_num_dims is only 1 but we have a higher-dim prior. Is this behavior defined?
kernel3 = RBFKernel(
    ard_num_dims=1,
    lengthscale_prior=NormalPrior(loc=10, scale=scales)
)

new_lengthscale = kernel3.lengthscale_prior.sample(kernel3.lengthscale.shape)
print(kernel3.lengthscale.shape) # size 1, 1 -- but shouldn't we expect 1,2? 
print(new_lengthscale.shape) # size 1, 1, 2

# ok, ard_num_dims is 2 but my prior is 1d, now it works correctly
kernel4 = RBFKernel(
    ard_num_dims=2,
    lengthscale_prior=NormalPrior(loc=10, scale=1)
)

new_lengthscale = kernel4.lengthscale_prior.sample(kernel4.lengthscale.shape)
print(kernel4.lengthscale.shape) # size 1,2
print(new_lengthscale.shape) # size 1, 2

Expected Behavior

It would be nice if we got a warning/error earlier for undefined/unsupported behavior, and otherwise shapes matched correctly.

System information

Please complete the following information:

  • GPyTorch Version: 1.2.0
  • PyTorch Version: 1.6.0.
  • Computer OS: verified on OSX and CentOS.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Balandatcommented, Oct 20, 2020

Yeah @dme65 ran into some issues that are likely related, it seems like there are some issues with batch sizes and the priors.

1reaction
mshvartsmancommented, Mar 2, 2021

I think as long as we expand based on closure() in register_prior we should be fine, right? Though I’m not sure whether batch_shape will play well with this. I’ll make some changes locally and give it a shot. Relatedly, if this and #1318 is the same bug (as seems to be the case), should we close one of them to keep discussion consolidated?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prior-selection - Taming the BEAST
Specifying proper priors and starting values is crucial and can be a difficult exercise in the beginning. It is not always easy to...
Read more >
Chapter 5 Priors in R-INLA | Bayesian inference with INLA
This book introduces the integrated nested Laplace approximation (INLA) for Bayesian inference and its associated R package R-INLA.
Read more >
Hyper-parameter optimization algorithms: a short review
A comprehensive reviews of the state of the art in algorithms for hyper-parameter optimization in machine learning.
Read more >
Posterior very different to prior and likelihood - Cross Validated
Yes this situation can arise and is a feature of your modeling assumptions specifically normality in the prior and sampling model ...
Read more >
Hyperparameter Importance Across Datasets - arXiv
However, this progress is not yet matched by equal progress ... and trial & error. ... data, we also infer prior distributions over...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found