From a5f13b04ea12e977aecb0a2836136a00b40a6183 Mon Sep 17 00:00:00 2001 From: Stefan Webb Date: Mon, 25 May 2020 11:51:40 -0700 Subject: [PATCH] Bug fix to AffineAutoregressive --- .gitattributes | 2 -- .../distributions/transforms/affine_autoregressive.py | 2 +- tests/distributions/test_transforms.py | 11 +++++++---- 3 files changed, 8 insertions(+), 7 deletions(-) delete mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index b6115e6e49..0000000000 --- a/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -*.py linguist-language=python -*.ipynb linguist-documentation diff --git a/pyro/distributions/transforms/affine_autoregressive.py b/pyro/distributions/transforms/affine_autoregressive.py index 74ac7cada9..ceb85d8a39 100644 --- a/pyro/distributions/transforms/affine_autoregressive.py +++ b/pyro/distributions/transforms/affine_autoregressive.py @@ -217,8 +217,8 @@ def _inverse_stable(self, y): mean, logit_scale = self.arn(torch.stack(x, dim=-1)) inverse_scale = 1 + torch.exp(-logit_scale[..., idx] - self.sigmoid_bias) x[idx] = inverse_scale * y[..., idx] + (1 - inverse_scale) * mean[..., idx] - self._cached_log_scale = inverse_scale + self._cached_log_scale = self.logsigmoid(logit_scale + self.sigmoid_bias) x = torch.stack(x, dim=-1) return x diff --git a/tests/distributions/test_transforms.py b/tests/distributions/test_transforms.py index ed7dd031d6..384156ee6b 100644 --- a/tests/distributions/test_transforms.py +++ b/tests/distributions/test_transforms.py @@ -95,16 +95,19 @@ def nonzero(x): assert lower_sum == float(0.0) def _test_inverse(self, shape, transform): + # Test g^{-1}(g(x)) = x + # NOTE: Calling _call and _inverse directly bypasses caching base_dist = dist.Normal(torch.zeros(shape), torch.ones(shape)) - x_true = base_dist.sample(torch.Size([10])) y = transform._call(x_true) - - # Cache is empty, hence must be calculating inverse afresh + J_1 = transform.log_abs_det_jacobian(x_true, y) x_calculated = transform._inverse(y) - + J_2 = transform.log_abs_det_jacobian(x_true, y) assert (x_true - x_calculated).abs().max().item() < self.delta + # Test that Jacobian after inverse op is same as after forward + assert (J_1 - J_2).abs().max().item() < self.delta + def _test_shape(self, base_shape, transform): base_dist = dist.Normal(torch.zeros(base_shape), torch.ones(base_shape)) sample = dist.TransformedDistribution(base_dist, [transform]).sample()