-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from Joshuaalbert/implement-jaxify
* implement #59
- Loading branch information
Showing
7 changed files
with
88 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from jaxns.framework.model import * | ||
from jaxns.framework.prior import * | ||
from jaxns.framework.special_priors import * | ||
from jaxns.framework.jaxify import * | ||
from jaxns.framework.bases import PriorModelGen, PriorModelType |
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,44 @@ | ||
import warnings | ||
from typing import Callable | ||
|
||
import jax | ||
import numpy as np | ||
|
||
from jaxns.internals.types import float_type, LikelihoodType | ||
|
||
__all__ = [ | ||
'jaxify_likelihood' | ||
] | ||
|
||
def jaxify_likelihood(log_likelihood: Callable[..., np.ndarray], vectorised: bool = False) -> LikelihoodType: | ||
""" | ||
Wraps a non-JAX log likelihood function. | ||
Args: | ||
log_likelihood: a non-JAX log-likelihood function, which accepts a number of arguments and returns a scalar | ||
log-likelihood. | ||
vectorised: if True then the `log_likelihood` performs a vectorised computation for leading batch dimensions, | ||
i.e. if a leading batch dimension is added to all input arguments, then it returns a vector of | ||
log-likelihoods with the same leading batch dimension. | ||
Returns: | ||
A JAX-compatible log-likelihood function. | ||
""" | ||
warnings.warn( | ||
"You're using a non-JAX log-likelihood function. This may be slower than a JAX log-likelihood function. " | ||
"Also, you are responsible for ensuring that the function is deterministic. " | ||
"Also, you cannot use learnable parameters in the likelihood call." | ||
) | ||
|
||
def _casted_log_likelihood(*args) -> np.ndarray: | ||
return np.asarray(log_likelihood(*args), dtype=float_type) | ||
|
||
def _log_likelihood(*args) -> jax.Array: | ||
# Define the expected shape & dtype of output. | ||
result_shape_dtype = jax.ShapeDtypeStruct( | ||
shape=(), | ||
dtype=float_type | ||
) | ||
return jax.pure_callback(_casted_log_likelihood, result_shape_dtype, *args, vectorized=vectorised) | ||
|
||
return _log_likelihood |
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,35 @@ | ||
import jax | ||
import jax.random | ||
import numpy as np | ||
|
||
from jaxns import Prior, Model | ||
from jaxns.framework.jaxify import jaxify_likelihood | ||
from jaxns.framework.tests.test_model import tfpd | ||
|
||
|
||
def test_jaxify_likelihood(): | ||
def log_likelihood(x, y): | ||
return np.sum(x, axis=-1) + np.sum(y, axis=-1) | ||
|
||
wrapped_ll = jaxify_likelihood(log_likelihood) | ||
np.testing.assert_allclose(wrapped_ll(np.array([1, 2]), np.array([3, 4])), 10) | ||
|
||
vmaped_wrapped_ll = jax.vmap(jaxify_likelihood(log_likelihood, vectorised=True)) | ||
|
||
np.testing.assert_allclose(vmaped_wrapped_ll(np.array([[1, 2], [2, 2]]), np.array([[3, 4], [4, 4]])), | ||
np.array([10, 12])) | ||
|
||
|
||
def test_jaxify(): | ||
def prior_model(): | ||
x = yield Prior(tfpd.Uniform(), name='x').parametrised() | ||
return x | ||
|
||
@jaxify_likelihood | ||
def log_likelihood(x): | ||
return x | ||
|
||
model = Model(prior_model=prior_model, log_likelihood=log_likelihood) | ||
model.sanity_check(key=jax.random.PRNGKey(0), S=10) | ||
assert model.U_ndims == 0 | ||
assert model.num_params == 1 |
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