Skip to content

Commit c000b19

Browse files
🩹 Include broadcast bounds in Truncated batch shape (#83)
Co-authored-by: ktz03 <[email protected]>
1 parent 1063ae4 commit c000b19

2 files changed

Lines changed: 27 additions & 22 deletions

File tree

‎tests/test_distributions.py‎

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,61 @@
11
r"""Tests for the zuko.distributions module."""
22

3+
import pytest
34
import torch
45

56
from torch.distributions import *
67

78
from zuko.distributions import *
89

910

10-
def test_distributions() -> None:
11+
@pytest.mark.parametrize("batch_shape", [(), (3,), (2, 3)])
12+
def test_distributions(batch_shape: tuple[int, ...]) -> None:
1113
ds = [
12-
NormalizingFlow(ExpTransform(), Gamma(2.0, 1.0)),
13-
Joint(Uniform(0.0, 1.0), Normal(0.0, 1.0)),
14-
Mixture(Normal(torch.randn(2), torch.ones(2)), torch.randn(2)),
15-
GeneralizedNormal(2.0),
16-
DiagNormal(torch.zeros(2), torch.ones(2)),
17-
BoxUniform(-torch.ones(2), torch.ones(2)),
18-
TransformedUniform(ExpTransform(), -1.0, 1.0),
19-
Truncated(Normal(0.0, 1.0), 1.0, 2.0),
20-
Sort(Normal(0.0, 1.0), 2),
21-
TopK(Normal(0.0, 1.0), 2, 3),
22-
Minimum(Normal(0.0, 1.0), 3),
23-
Maximum(Normal(0.0, 1.0), 3),
14+
NormalizingFlow(ExpTransform(), Gamma(2.0 * torch.ones(batch_shape), 1.0)),
15+
Joint(Uniform(torch.zeros(batch_shape), 1.0), Normal(0.0, 1.0)),
16+
Mixture(Normal(torch.randn(*batch_shape, 2), 1.0), torch.randn(*batch_shape, 2)),
17+
GeneralizedNormal(2.0 * torch.ones(batch_shape)),
18+
DiagNormal(torch.zeros(*batch_shape, 2), torch.ones(*batch_shape, 2)),
19+
BoxUniform(-torch.ones(*batch_shape, 2), torch.ones(*batch_shape, 2)),
20+
TransformedUniform(ExpTransform(), -torch.ones(batch_shape), 1.0),
21+
Truncated(Normal(torch.zeros(batch_shape), 1.0), 1.0, 2.0),
22+
Truncated(Normal(0.0, 1.0), -torch.ones(batch_shape), torch.ones(batch_shape)),
23+
Joint(Truncated(Normal(0.0, 1.0), -torch.ones(batch_shape), 1.0), Normal(0.0, 1.0)),
24+
Sort(Normal(torch.zeros(batch_shape), 1.0), 2),
25+
TopK(Normal(torch.zeros(batch_shape), 1.0), 2, 3),
26+
Minimum(Normal(torch.zeros(batch_shape), 1.0), 3),
27+
Maximum(Normal(torch.zeros(batch_shape), 1.0), 3),
2428
]
2529

26-
shape = (2**18,)
30+
shape = (2**16,)
2731

2832
for d in ds:
29-
assert d.batch_shape == (), d
33+
assert d.batch_shape == batch_shape, d
3034

3135
# Shapes
3236
x = d.sample(shape)
3337

34-
assert x.shape == shape + d.event_shape, d
38+
assert x.shape == shape + d.batch_shape + d.event_shape, d
3539

3640
log_p = d.log_prob(x)
3741

38-
assert log_p.shape == shape, d
42+
assert log_p.shape == shape + d.batch_shape, d
3943

4044
# Expectation
4145
lower, upper = x.min(dim=0).values, x.max(dim=0).values
4246
width = upper - lower
4347

4448
x = Uniform(lower - width / 2, upper + width / 2).sample(shape)
4549

46-
p = d.log_prob(x).exp().mean() * (2 * width).prod()
50+
p = d.log_prob(x).exp().mean(dim=0)
51+
p = p * (2 * width).reshape(*batch_shape, -1).prod(dim=-1)
4752

48-
assert (0.9 <= p) and (p <= 1.1), d
53+
assert torch.all((0.9 <= p) & (p <= 1.1)), d
4954

5055
# Expand
51-
d = d.expand((32,))
56+
d = d.expand((32, *batch_shape))
5257

53-
assert d.batch_shape == (32,), d
58+
assert d.batch_shape == (32, *batch_shape), d
5459

5560
x = d.sample()
5661

‎zuko/distributions.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def __repr__(self) -> str:
467467

468468
@property
469469
def batch_shape(self) -> Size:
470-
return self.base.batch_shape
470+
return self.uniform.batch_shape
471471

472472
def expand(self, batch_shape: Size, new: Distribution | None = None) -> Distribution:
473473
new = self._get_checked_instance(Truncated, new)

0 commit comments

Comments
 (0)