Skip to content
Advertisement

How can I select top-n elements from tensor without repeating elements?

I want to select top-n elements of 3 dimension tensor given the picked elements are all unique. All the elements are sorted by the 2nd column, and I’m selecting top-2 in the example below but I don’t want duplicates in there.

  • Condition: No for loops or tf.map_fn()

  • Here is the input and desired_output that I want:

JavaScript
  • This is what I’m getting right now; which I don’t want!
JavaScript
  • Here is what I actually want
JavaScript

Advertisement

Answer

This is one possible way to do that, although it requires more work since it sorts the array first.

JavaScript

Note there is a kind of corner case which is when there are not k different elements in a group. In that case, this solution would put the repeated elements at the end, which would break the score order.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement