-
-
Notifications
You must be signed in to change notification settings - Fork 984
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Numerically stabilize ProjectedNormal.log_prob() via erfc (#3071)
* Numerically stabilize ProjectedNormal.log_prob() via logaddexp * Fix conceptual error, now this NANs * Switch to erfc * Strengthen tests * lint * Strengthen test, clamp harder
- Loading branch information
Showing
6 changed files
with
63 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright Contributors to the Pyro project. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
import torch | ||
|
||
import pyro.distributions as dist | ||
from tests.common import default_dtype | ||
|
||
|
||
@pytest.mark.parametrize("strength", [0, 1, 10, 100, 1000]) | ||
@pytest.mark.parametrize("dim", [2, 3, 4]) | ||
@pytest.mark.parametrize("dtype", [torch.float, torch.double], ids=str) | ||
def test_log_prob(dtype, dim, strength): | ||
with default_dtype(dtype): | ||
concentration = torch.full((dim,), float(strength), requires_grad=True) | ||
value = dist.ProjectedNormal(torch.zeros_like(concentration)).sample([10000]) | ||
d = dist.ProjectedNormal(concentration) | ||
|
||
logp = d.log_prob(value) | ||
assert logp.max().lt(1 + dim * strength).all() | ||
|
||
logp.sum().backward() | ||
assert not torch.isnan(concentration.grad).any() |