2

I have a jitted function where one of the inputs is an array whose number of unique elements I would like to calculate. I cannot use jax.numpy.unique, it throws a ConcretizationTypeError due to the output of unique being of variable size. Any algorithmic workarounds_

1
  • Try using the size arg of jax.numpy.unique. Commented Apr 3, 2024 at 1:28

1 Answer 1

2

One way to do this is to sort the array and count the number of adjacent entries which are not equal. For example:

import jax
import jax.numpy as jnp

x = jnp.array([3, 2, 1, 2, 3])

@jax.jit
def count_unique(x):
  x = jnp.sort(x)
  return 1 + (x[1:] != x[:-1]).sum()

print(count_unique(x))
# 3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.