Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzo committed Jan 21, 2021
1 parent ee5bf10 commit bdf6d77
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion numpyro/distributions/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import math
import warnings
import weakref

import numpy as np

Expand Down Expand Up @@ -52,6 +53,7 @@ def _clipped_expit(x):
class Transform(object):
domain = constraints.real
codomain = constraints.real
_inv = None

@property
def event_dim(self):
Expand All @@ -62,7 +64,13 @@ def event_dim(self):

@property
def inv(self):
return _InverseTransform(self)
inv = None
if self._inv is not None:
inv = self._inv()
if inv is None:
inv = _InverseTransform(self)
self._inv = weakref.ref(inv)
return inv

def __call__(self, x):
return NotImplementedError
Expand Down
1 change: 1 addition & 0 deletions test/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ def test_bijective_transforms(transform, event_shape, batch_shape):
z = transform.inv(y)
assert_allclose(x, z, atol=1e-6, rtol=1e-6)
assert transform.inv.inv is transform
assert transform.inv is transform.inv
assert transform.domain is transform.inv.codomain
assert transform.codomain is transform.inv.domain

Expand Down

0 comments on commit bdf6d77

Please sign in to comment.