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 loopsortf.map_fn()Here is the input and desired_output that I want:
input_tensor = tf.constant([
[[2.0, 1.0],
[2.0, 1.0],
[3.0, 0.4],
[1.0, 0.1]],
[[44.0, 0.8],
[22.0, 0.7],
[11.0, 0.5],
[11.0, 0.5]],
[[5555.0, 0.8],
[3333.0, 0.7],
[4444.0, 0.4],
[1111.0, 0.1]],
[[444.0, 0.8],
[333.0, 1.1],
[333.0, 1.1],
[111.0, 0.1]]
])
- This is what I'm getting right now; which I don't want!
>> TOPK = 2
>> topk_resutls = tf.gather(
input_tensor,
tf.math.top_k(input_tensor[:, :, 1], k=TOPK, sorted=True).indices,
batch_dims=1
)
>> topk_resutls.numpy().tolist()
[[[2.0, 1.0], [2.0, 1.0]],
[[44.0, 0.8], [22.0, 0.7]],
[[5555.0, 0.8], [3333.0, 0.7]],
[[333.0, 1.1], [333.0, 1.1]]]
- Here is what I actually want
[[[2.0, 1.0], [3.0, 0.4]], # [3.0, 0.4] is the 2nd highest element based on 2nd column
[[44.0, 0.8], [22.0, 0.7]],
[[5555.0, 0.8], [3333.0, 0.7]],
[[333.0, 1.1], [444.0, 0.8]]] # [444.0, 0.8] is the 2nd highest element based on 2nd column