Skip to content
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

Fix LKJ sample shape bug #2617

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion pyro/distributions/lkj.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def __init__(self, d, eta, validate_args=None):
super().__init__(torch.Size(), torch.Size((d, d)), validate_args=validate_args)

def sample(self, sample_shape=torch.Size()):
y = self._gen.sample(sample_shape=self.batch_shape + sample_shape).detach()
with torch.no_grad():
y = self._gen.sample(sample_shape=sample_shape + self.batch_shape)
z = y.mul(2).add(-1.0)
return _vector_to_l_cholesky(z)

Expand Down
14 changes: 14 additions & 0 deletions tests/distributions/test_lkj.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,17 @@ def test_log_prob_d2(eta):
tst = test_dist.log_prob(x)

assert_tensors_equal(lp, tst, prec=1e-6)


def test_sample_batch():
# Regression test for https://github.com/pyro-ppl/pyro/issues/2615
dist = LKJCorrCholesky(d=3, eta=torch.ones(())).expand([12])
# batch shape and event shape are as you'd expect
assert dist.batch_shape == torch.Size([12])
assert dist.event_shape == torch.Size([3, 3])
# samples have correct shape when sample_shape=()
assert dist.shape(()) == torch.Size([12, 3, 3])
assert dist.sample().shape == torch.Size([12, 3, 3])
# samples had the wrong shape when sample_shape is non-unit
assert dist.shape((4,)) == torch.Size([4, 12, 3, 3])
assert dist.sample((4,)).shape == torch.Size([4, 12, 3, 3])