CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers

Bug in Chapter 1 Pyro Version: Exponential Distribution Input

JAEarly opened this issue · 2 comments

In the Pyro Version of Chapter 1, the following code block has an issue:

lambda_ = [0.5, 1]
d = dist.Exponential(torch.tensor(lambda_))

x = torch.linspace(0, 4, 100).view(-1, 1)
y = torch.exp(d.log_prob(x))

It raises the error:

ValueError: The value argument must be within the support

Problem:
The underlying PyTorch code for the exponential distribution only allows inputs greater than zero, so as x includes zero, this block fails. I think the underlying issue is with PyTorch, so I've opened a PR to change the constraints for the exponential function from positive (>0) to non-negative (>=0). If that change goes through, this issue is moot, but if they don't change it, then the block will need to be updated.

A quick fix is just to add a small value to x[0], e.g.:

x[0] += 1e-8

Then this block runs and the subsequent plot looks correct.

My PR to PyTorch to fix this has been accepted, so this bug will no longer happen if you have the latest version of PyTorch. Closing as fixed.