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 Dirichlet.log_prob() when x=0 and alpha=1 #103605

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/distributions/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2773,6 +2773,20 @@ def test_dirichlet_log_prob(self):
expected_log_prob = scipy.stats.dirichlet.logpdf(x[i].numpy(), alpha.numpy())
self.assertEqual(actual_log_prob[i], expected_log_prob, atol=1e-3, rtol=0)

@unittest.skipIf(not TEST_NUMPY, "NumPy not found")
def test_dirichlet_log_prob_zero(self):
# Specifically test the special case where x=0 and α=1. The PDF is
# proportional to x**(α-1), which in this case works out to 0**0=1.
# The log PDF of this term should therefore be 0. However, it's easy
# to accidentally introduce NaNs by calculating log(x) without regard
# for the value of α-1.
alpha = torch.tensor([1, 2])
dist = Dirichlet(alpha)
x = torch.tensor([0, 1])
actual_log_prob = dist.log_prob(x)
expected_log_prob = scipy.stats.dirichlet.logpdf(x.numpy(), alpha.numpy())
self.assertEqual(actual_log_prob, expected_log_prob, atol=1e-3, rtol=0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need to override the tolerances here? I would expect the default one to work fine here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied the whole assertion line from the existing Dirichlet.log_prob() test. I don't know if there's a good reason why these tolerances were chosen in the first place, though. Maybe there's a test environment that uses low-precision floats or something? In any case, my goal was to be consistent. Also, this test only really needs to distinguish between NaN and not NaN, so for that the tolerance doesn't matter.


@unittest.skipIf(not TEST_NUMPY, "NumPy not found")
def test_dirichlet_sample(self):
set_rng_seed(0) # see Note [Randomized statistical tests]
Expand Down
2 changes: 1 addition & 1 deletion torch/distributions/dirichlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def rsample(self, sample_shape=()):
def log_prob(self, value):
if self._validate_args:
self._validate_sample(value)
return ((torch.log(value) * (self.concentration - 1.0)).sum(-1) +
return (torch.xlogy(self.concentration - 1.0, value).sum(-1) +
torch.lgamma(self.concentration.sum(-1)) -
torch.lgamma(self.concentration).sum(-1))

Expand Down