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

Incorrect event dimension for ComposeTransform if transform reduces event dimensions #1893

Closed
tillahoffmann opened this issue Oct 23, 2024 · 1 comment · Fixed by #1894
Closed
Labels
bug Something isn't working

Comments

@tillahoffmann
Copy link
Contributor

An example is probably the clearest illustration. If the transform increases the number of event dimensions, everything works as expected.

>>> from numpyro.distributions.transforms import CorrCholeskyTransform, ComposeTransform
>>> part = CorrCholeskyTransform()
>>> part.domain.event_dim, part.codomain.event_dim
(1, 2)
>>> composed = ComposeTransform([part])
>>> composed.domain.event_dim, composed.codomain.event_dim
(1, 2)

If the transform reduces the number of event dimensions, wrapping the transform in a ComposeTransform leads to unexpected results.

>>> part = CorrCholeskyTransform().inv
>>> part.domain.event_dim, part.codomain.event_dim
(2, 1)
>>> composed = ComposeTransform([part])
>>> composed.domain.event_dim, composed.codomain.event_dim
(3, 1)

I had a brief look at the code below, but I couldn't quite get my head around it.

def _get_compose_transform_output_event_dim(parts):
output_event_dim = parts[0].codomain.event_dim
for part in parts[1:]:
output_event_dim = part.codomain.event_dim + max(
output_event_dim - part.domain.event_dim, 0
)
return output_event_dim

Here's a minimal test that can be added to test/test_transforms.py to reproduce.

@pytest.mark.parametrize("transform", [
    CorrCholeskyTransform(),  # passes
    CorrCholeskyTransform().inv,  # fails
])
def test_compose_domain_codomain(transform):
    composed = ComposeTransform([transform])
    assert transform.domain.event_dim == composed.domain.event_dim
    assert transform.codomain.event_dim == composed.codomain.event_dim
@fehiepsi fehiepsi added the bug Something isn't working label Oct 23, 2024
@fehiepsi
Copy link
Member

It seems that the loop at this line is incorrect

for part in parts[len(parts) - 1 :: -1]:

I think it should be

for part in parts[len(parts) - 2 :: -1]:

Could you double check?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants