Suppose I have a tensor, x = [1, 2, 6, 6, 4, 2, 3, 2]
I want to find the index of the first occurrence of every unique element in x.
The output should be [0, 1, 6, 4, 2].
I basically want the second output of numpy.unique(x,return_index=True). This functionality doesn't seem to be supported in tf.unique.
Is there a workaround to this in tensorflow, without using any loops?
答案 0 :(得分:0)
One possibilty would be to use x.eval() which gives you a numpy array back, then use numpy.unique(...)
答案 1 :(得分:0)
x = [1, 2, 6, 6, 4, 2, 3, 2]
x_count = tf.cumsum(tf.ones_like(x))-1
unique, unique_id = tf.unique(x)
unique_first = tf.unsorted_segment_min(x_count, unique_id, tf.shape(unique)[0])
with tf.Session() as sess:
print(sess.run(tf.stack([unique, unique_first],0)))
礼物:
[[1 2 6 4 3]
[0 1 2 4 6]]