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 pyro.poutine.do to avoid duplicate entries in cond_indep_stack #2846

Merged
merged 1 commit into from
May 16, 2021
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
1 change: 1 addition & 0 deletions pyro/poutine/do_messenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def _pyro_sample(self, msg):

# split node, avoid reapplying self recursively to new node
new_msg = msg.copy()
new_msg["cond_indep_stack"] = () # avoid entering plates twice
apply_stack(new_msg)

# apply intervention
Expand Down
24 changes: 24 additions & 0 deletions tests/poutine/test_counterfactual.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,27 @@ def model():
assert_equal(interventions[name], actual_values[name])
if interventions[name] != observations[name]:
assert_not_equal(interventions[name], tr.nodes[name]['value'])


def test_plate_duplication_smoke():

def model(N):

with pyro.plate("x_plate", N):
z1 = pyro.sample("z1", dist.MultivariateNormal(torch.zeros(2), torch.eye(2)))
z2 = pyro.sample("z2", dist.MultivariateNormal(torch.zeros(2), torch.eye(2)))
return pyro.sample("x", dist.MultivariateNormal(z1+z2, torch.eye(2)))

fix_z1 = torch.tensor([[-6.1258, -6.1524],
[-4.1513, -4.3080]])

obs_x = torch.tensor([[-6.1258, -6.1524],
[-4.1513, -4.3080]])

do_model = poutine.do(model, data={"z1": fix_z1})
do_model = poutine.condition(do_model, data={"x": obs_x})
do_auto = pyro.infer.autoguide.AutoMultivariateNormal(do_model)
optim = pyro.optim.Adam({"lr": 0.05})

svi = pyro.infer.SVI(do_model, do_auto, optim, pyro.infer.Trace_ELBO())
svi.step(len(obs_x))