Skip to content

[Bug] Error in sampling from LKJCovariancePrior #2685

@lpapenme

Description

@lpapenme

🐛 Bug

Error in sampling from LKJCovariancePrior

To reproduce

** Code snippet to reproduce **

n = 3
sd_prior = gpytorch.priors.GammaPrior(1.0, 1.0)
prior = gpytorch.priors.LKJCovariancePrior(n=n, eta=1.0, sd_prior=sd_prior)

samples = prior.sample(torch.Size([5]))

** Stack trace/error message **

Traceback (most recent call last):
  File "test_script.py, line 12, in <module>
    samples = prior.sample(torch.Size([5]))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.11/site-packages/gpytorch/priors/lkj_prior.py", line 115, in sample
    return marginal_sds.matmul(base_correlation).matmul(marginal_sds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Expected size for first two dimensions of batch2 tensor to be: [5, 15] but got: [5, 3].

Expected Behavior

I'd expect this to sample a (5,3,3) tensor of five 3x3 covariance matrices.

System information

Please complete the following information:

  • GPyTorch version 1.14
  • torch version 2.4.1
  • MacOS Tahoe 26.1

Additional context

Something like

if marginal_sds.shape == sample_shape:
    marginal_sds = marginal_sds.unsqueeze(-1) # ensure marginal_sds has a feature dimension for broadcasting
marginal_sds = marginal_sds.expand(*sample_shape, self.correlation_prior.n)

seems to work, but causes downstream tests to fail.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions