Use jnp.interp#129
Open
ASKabalan wants to merge 15 commits intoDifferentiableUniverseInitiative:masterfrom
Open
Use jnp.interp#129ASKabalan wants to merge 15 commits intoDifferentiableUniverseInitiative:masterfrom
ASKabalan wants to merge 15 commits intoDifferentiableUniverseInitiative:masterfrom
Conversation
Collaborator
|
Although if I agree to use Here is my 1-cent (in the context of jax-cosmo one would use the decorator and switch #@functools.partial(jax.vmap, in_axes=(0, None, None))
def interp_modif(x, xp, fp):
"""
Simple equivalent of np.interp that compute a linear interpolation.
We are not doing any checks, so make sure your query points are lying
inside the array.
x, xp, fp need to be 1d arrays
"""
x = jnp.atleast_1d(x)
# First we find the nearest neighbour
ind = jnp.argmin((x - xp) ** 2)
# Perform linear interpolation
ind = jnp.clip(ind, 0, len(xp) - 2)
xi = xp[ind]
# Figure out if we are on the right or the left of nearest
s = jnp.sign(jnp.clip(x, xp[0], xp[-2]) - xi)
s =jax.lax.convert_element_type(s,jnp.int32)
one = jnp.copysign(1, s)
one = jax.lax.convert_element_type(one,jnp.int32)
a = (fp[ind + one] - fp[ind]) / (
xp[ind + one] - xp[ind]
)
b = fp[ind] - a * xp[ind]
return jnp.squeeze(a * x + b)The failure comes essentialy from the two clipping lower bounds. I have also remove the casting to int64. |
EiffL
reviewed
Dec 20, 2024
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Using jax numpy interpolate instead of the custom code that was made before jnp.interp was implemented
Notebook proving that jnp.interp is more accurate and much faster