-
Notifications
You must be signed in to change notification settings - Fork 223
/
test_model_rendering.py
131 lines (115 loc) · 4.18 KB
/
test_model_rendering.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright Contributors to the Pyro project.
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import pytest
import jax.numpy as jnp
import numpyro
import numpyro.distributions as dist
from numpyro.infer.inspect import generate_graph_specification, get_model_relations
def simple(data):
x = numpyro.sample("x", dist.Normal(0, 1))
sd = numpyro.sample("sd", dist.LogNormal(x, 1))
with numpyro.plate("N", len(data)):
numpyro.sample("obs", dist.Normal(x, sd), obs=data)
def plate_improper_subsets():
with numpyro.plate("N", 10):
with numpyro.plate("M", 10):
numpyro.sample("x", dist.Normal(0, 1))
def nested_plates():
N_plate = numpyro.plate("N", 10, dim=-2)
M_plate = numpyro.plate("M", 5, dim=-1)
with N_plate:
numpyro.sample("x", dist.Normal(0, 1))
with M_plate:
numpyro.sample("y", dist.Normal(0, 1))
with M_plate:
numpyro.sample("z", dist.Normal(0, 1))
def discrete_to_continuous(probs, locs):
c = numpyro.sample("c", dist.Categorical(probs))
# We need to make sure that locs is a jax ndarray
# because indexing a numpy ndarray with an abstract
# index does not work in JAX.
numpyro.sample("x", dist.Normal(jnp.asarray(locs)[c], 0.5))
def discrete(prob):
numpyro.sample("x", dist.Bernoulli(prob))
@pytest.mark.parametrize(
"test_model,model_kwargs,expected_graph_spec",
[
(
simple,
dict(data=np.ones(10)),
{
"plate_groups": {"N": ["obs"], None: ["x", "sd"]},
"plate_data": {"N": {"parent": None}},
"node_data": {
"x": {"is_observed": False, "distribution": "Normal"},
"sd": {"is_observed": False, "distribution": "LogNormal"},
"obs": {"is_observed": True, "distribution": "Normal"},
},
"edge_list": [("x", "sd"), ("x", "obs"), ("sd", "obs")],
},
),
(
plate_improper_subsets,
dict(),
{
"plate_groups": {"N": ["x"], "M": ["x"], None: []},
"plate_data": {"N": {"parent": None}, "M": {"parent": "N"}},
"node_data": {"x": {"is_observed": False, "distribution": "Normal"}},
"edge_list": [],
},
),
(
nested_plates,
dict(),
{
"plate_groups": {
"N": ["x", "y"],
"M": ["y"],
"M__CLONE": ["z"],
None: [],
},
"plate_data": {
"N": {"parent": None},
"M": {"parent": "N"},
"M__CLONE": {"parent": None},
},
"node_data": {
"x": {"is_observed": False, "distribution": "Normal"},
"y": {"is_observed": False, "distribution": "Normal"},
"z": {"is_observed": False, "distribution": "Normal"},
},
"edge_list": [],
},
),
(
discrete_to_continuous,
dict(probs=np.array([0.15, 0.3, 0.3, 0.25]), locs=np.array([-2, 0, 2, 4])),
{
"plate_groups": {None: ["c", "x"]},
"plate_data": {},
"node_data": {
"c": {"is_observed": False, "distribution": "CategoricalProbs"},
"x": {"is_observed": False, "distribution": "Normal"},
},
"edge_list": [("c", "x")],
},
),
(
discrete,
dict(prob=0.5),
{
"plate_groups": {None: ["x"]},
"plate_data": {},
"node_data": {
"x": {"is_observed": False, "distribution": "BernoulliProbs"}
},
"edge_list": [],
},
),
],
)
def test_model_transformation(test_model, model_kwargs, expected_graph_spec):
relations = get_model_relations(test_model, model_kwargs=model_kwargs)
graph_spec = generate_graph_specification(relations)
assert graph_spec == expected_graph_spec