I have a 3D Eigen tensor (Eigen::Tensor<float, 3, Eigen::RowMajor, EIGEN_DEFAULT_DENSE_INDEX_TYPE>) of shape [B, D, C] containing floating point numbers that represent the confidence score of some network output.
I want to perform top k and obtain the values and indices of the highest-scoring outputs
along the second dimension and reduce D to a smaller dimension k. The resulting vector should be of shape [B, k, C].
In python (tensorflow) it would simply be (in this case integers for simplicity):
data = np.array([[[3, 9, 8, 5],
[3, 3, 0, 1],
[5, 5, 1, 9]],
[[1, 3, 0, 9],
[8, 3, 7, 7],
[8, 0, 9, 5]]])
values, indices = tf.math.top_k(tensor, k=2, sorted=False)
And the result:
Values
[[[8 9]
[3 3]
[5 9]]
[[3 9]
[7 8]
[8 9]]]
Indices
[[[2 1]
[1 0]
[0 3]]
[[1 3]
[2 0]
[0 2]]]
Thanks in advance!
The following code works, but it is not efficient: