Pyro.ai tutorials out of sync with github

The code in An Introduction to Inference in Pyro — Pyro Tutorials 1.6.0 documentation leads to an exception when you run cell 10

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)
~/miniconda3/envs/pyro/lib/python3.7/site-packages/pyro/poutine/trace_struct.py in compute_log_prob(self, site_filter)
215 try:
→ 216 log_p = site[“fn”].log_prob(site[“value”], *site[“args”], **site[“kwargs”])
217 except ValueError as e:

~/miniconda3/envs/pyro/lib/python3.7/site-packages/torch/distributions/normal.py in log_prob(self, value)
72 if self._validate_args:
—> 73 self._validate_sample(value)
74 # compute the variance

~/miniconda3/envs/pyro/lib/python3.7/site-packages/torch/distributions/distribution.py in _validate_sample(self, value)
254 if not isinstance(value, torch.Tensor):
→ 255 raise ValueError(‘The value argument to log_prob must be a Tensor’)
256

ValueError: The value argument to log_prob must be a Tensor

The above exception was the direct cause of the following exception:

ValueError Traceback (most recent call last)
in
12 num_steps = 2500
13 for t in range(num_steps):
—> 14 losses.append(svi.step(guess))
15 a.append(pyro.param(“a”).item())
16 b.append(pyro.param(“b”).item())

~/miniconda3/envs/pyro/lib/python3.7/site-packages/pyro/infer/svi.py in step(self, *args, **kwargs)
126 # get loss and compute gradients
127 with poutine.trace(param_only=True) as param_capture:
→ 128 loss = self.loss_and_grads(self.model, self.guide, *args, **kwargs)
129
130 params = set(site[“value”].unconstrained()

~/miniconda3/envs/pyro/lib/python3.7/site-packages/pyro/infer/trace_elbo.py in loss_and_grads(self, model, guide, *args, **kwargs)
129 loss = 0.0
130 # grab a trace from the generator
→ 131 for model_trace, guide_trace in self._get_traces(model, guide, args, kwargs):
132 loss_particle, surrogate_loss_particle = self._differentiable_loss_particle(model_trace, guide_trace)
133 loss += loss_particle / self.num_particles

~/miniconda3/envs/pyro/lib/python3.7/site-packages/pyro/infer/elbo.py in _get_traces(self, model, guide, args, kwargs)
168 else:
169 for i in range(self.num_particles):
→ 170 yield self._get_trace(model, guide, args, kwargs)

~/miniconda3/envs/pyro/lib/python3.7/site-packages/pyro/infer/trace_elbo.py in _get_trace(self, model, guide, args, kwargs)
56 “”"
57 model_trace, guide_trace = get_importance_trace(
—> 58 “flat”, self.max_plate_nesting, model, guide, args, kwargs)
59 if is_validation_enabled():
60 check_if_enumerated(guide_trace)

~/miniconda3/envs/pyro/lib/python3.7/site-packages/pyro/infer/enum.py in get_importance_trace(graph_type, max_plate_nesting, model, guide, args, kwargs, detach)
53 model_trace = prune_subsample_sites(model_trace)
54
—> 55 model_trace.compute_log_prob()
56 guide_trace.compute_score_parts()
57 if is_validation_enabled():

~/miniconda3/envs/pyro/lib/python3.7/site-packages/pyro/poutine/trace_struct.py in compute_log_prob(self, site_filter)
219 shapes = self.format_shapes(last_site=site[“name”])
220 raise ValueError(“Error while computing log_prob at site ‘{}’:\n{}\n{}”
→ 221 .format(name, exc_value, shapes)).with_traceback(traceback) from e
222 site[“unscaled_log_prob”] = log_p
223 log_p = scale_and_mask(log_p, site[“scale”], site[“mask”])

~/miniconda3/envs/pyro/lib/python3.7/site-packages/pyro/poutine/trace_struct.py in compute_log_prob(self, site_filter)
214 if “log_prob” not in site:
215 try:
→ 216 log_p = site[“fn”].log_prob(site[“value”], *site[“args”], **site[“kwargs”])
217 except ValueError as e:
218 _, exc_value, traceback = sys.exc_info()

~/miniconda3/envs/pyro/lib/python3.7/site-packages/torch/distributions/normal.py in log_prob(self, value)
71 def log_prob(self, value):
72 if self._validate_args:
—> 73 self._validate_sample(value)
74 # compute the variance
75 var = (self.scale ** 2)

~/miniconda3/envs/pyro/lib/python3.7/site-packages/torch/distributions/distribution.py in _validate_sample(self, value)
253 “”"
254 if not isinstance(value, torch.Tensor):
→ 255 raise ValueError(‘The value argument to log_prob must be a Tensor’)
256
257 event_dim_start = len(value.size()) - len(self._event_shape)

ValueError: Error while computing log_prob at site ‘measurement’:
The value argument to log_prob must be a Tensor
Trace Shapes:
Param Sites:
Sample Sites:
weight dist |
value |
log_prob |
measurement dist |
value |

this is due to the measurement in conditioned_scale not being a tensor

conditioned_scale = pyro.condition(scale, data={"measurement": 9.5})

After looking to make an issue I found that this is fixed in Condition on tensor instead of scalar in Intro part ii tutorial (#2818) back on April 24. This fix has not been deployed to the website.

Thanks @jtf621! I’ve updated tutorials, hopefully they should sync shortly. In the future feel free to file tutorial bugs on https://github.com/pyro-ppl/pyro/issues/new where we can track them more easily.

1 Like