|
1 | 1 | r"""Tests for the zuko.distributions module.""" |
2 | 2 |
|
| 3 | +import pytest |
3 | 4 | import torch |
4 | 5 |
|
5 | 6 | from torch.distributions import * |
6 | 7 |
|
7 | 8 | from zuko.distributions import * |
8 | 9 |
|
9 | 10 |
|
10 | | -def test_distributions() -> None: |
| 11 | +@pytest.mark.parametrize("batch_shape", [(), (3,), (2, 3)]) |
| 12 | +def test_distributions(batch_shape: tuple[int, ...]) -> None: |
11 | 13 | 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), |
24 | 28 | ] |
25 | 29 |
|
26 | | - shape = (2**18,) |
| 30 | + shape = (2**16,) |
27 | 31 |
|
28 | 32 | for d in ds: |
29 | | - assert d.batch_shape == (), d |
| 33 | + assert d.batch_shape == batch_shape, d |
30 | 34 |
|
31 | 35 | # Shapes |
32 | 36 | x = d.sample(shape) |
33 | 37 |
|
34 | | - assert x.shape == shape + d.event_shape, d |
| 38 | + assert x.shape == shape + d.batch_shape + d.event_shape, d |
35 | 39 |
|
36 | 40 | log_p = d.log_prob(x) |
37 | 41 |
|
38 | | - assert log_p.shape == shape, d |
| 42 | + assert log_p.shape == shape + d.batch_shape, d |
39 | 43 |
|
40 | 44 | # Expectation |
41 | 45 | lower, upper = x.min(dim=0).values, x.max(dim=0).values |
42 | 46 | width = upper - lower |
43 | 47 |
|
44 | 48 | x = Uniform(lower - width / 2, upper + width / 2).sample(shape) |
45 | 49 |
|
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) |
47 | 52 |
|
48 | | - assert (0.9 <= p) and (p <= 1.1), d |
| 53 | + assert torch.all((0.9 <= p) & (p <= 1.1)), d |
49 | 54 |
|
50 | 55 | # Expand |
51 | | - d = d.expand((32,)) |
| 56 | + d = d.expand((32, *batch_shape)) |
52 | 57 |
|
53 | | - assert d.batch_shape == (32,), d |
| 58 | + assert d.batch_shape == (32, *batch_shape), d |
54 | 59 |
|
55 | 60 | x = d.sample() |
56 | 61 |
|
|
0 commit comments