-
-
Notifications
You must be signed in to change notification settings - Fork 986
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve speed and numerical stability of scale_tril to precision (#2264)
- Loading branch information
Showing
5 changed files
with
109 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright (c) 2017-2019 Uber Technologies, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
import torch | ||
|
||
from pyro.distributions import MultivariateNormal | ||
from tests.common import assert_equal | ||
|
||
|
||
def random_mvn(loc_shape, cov_shape, dim): | ||
""" | ||
Generate a random MultivariateNormal distribution for testing. | ||
""" | ||
rank = dim + dim | ||
loc = torch.randn(loc_shape + (dim,), requires_grad=True) | ||
cov = torch.randn(cov_shape + (dim, rank), requires_grad=True) | ||
cov = cov.matmul(cov.transpose(-1, -2)) | ||
return MultivariateNormal(loc, cov) | ||
|
||
|
||
@pytest.mark.parametrize('loc_shape', [ | ||
(), (2,), (3, 2), | ||
]) | ||
@pytest.mark.parametrize('cov_shape', [ | ||
(), (2,), (3, 2), | ||
]) | ||
@pytest.mark.parametrize('dim', [ | ||
1, 3, 5, | ||
]) | ||
def test_shape(loc_shape, cov_shape, dim): | ||
mvn = random_mvn(loc_shape, cov_shape, dim) | ||
assert mvn.loc.shape == mvn.batch_shape + mvn.event_shape | ||
assert mvn.covariance_matrix.shape == mvn.batch_shape + mvn.event_shape * 2 | ||
assert mvn.scale_tril.shape == mvn.covariance_matrix.shape | ||
assert mvn.precision_matrix.shape == mvn.covariance_matrix.shape | ||
|
||
assert_equal(mvn.precision_matrix, mvn.covariance_matrix.inverse()) | ||
|
||
# smoke test for precision/log_prob backward | ||
(mvn.precision_matrix.sum() + mvn.log_prob(torch.zeros(dim)).sum()).backward() |
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