Summary
I am looking for a function is_unique that for each element in a tensorflow tf.Tensor returns a boolean value whether its value is unique within the tensor.
Example input / output:
In [7]: idx = tf.constant([162, 223, 276, 162, 261, 215, 0, 0, 0], dtype=tf.int64)
In [8]: is_unique(idx)
Out[8]: <tf.*Tensor*: shape=(9,), dtype=bool, numpy=array([False, True, True, False, True, True, False, False, False])>
Adding another example on request for a tensor with higher rank: For a rank-2 tensor
tf.constant([[1, 2], [1, 3]], dtype=tf.int32)
the function would yield
<tf.Tensor: shape=(2, 2), dtype=bool, numpy=
array([[False, True],
[False, True]])>
This seems to be rather basic, but I believe there is no such function in tensorflow, or is there?
My code:
I haven't found a simple way to achieve this with tf.unique et al., so my current implementation looks like this:
def is_unique(t):
return tf.equal(
tf.reduce_sum(
tf.cast(
t[:,tf.newaxis] == t,
tf.int64,
),
axis = 1
),
1
)
(Basically, this creates a matrix of comparisons of every element with every other, counts the number of positive outcomes per row and compares this with 1.)
This won't work with tensors of rank > 1 and needs quadratic memory in the tensor size, so feel free to propose better solutions.