Considering,...
- Matrix
A form [m, n] - tensor
I form [m]
I want to get a list of J elements from A , where J[i] = A[i, I[i]] .
That is, I contains the index of the item to select from each row in A
Context: I already have argmax(A, 1) , and now I also want max . I know that I can just use reduce_max . And having tried a little, I also came up with this:
J = tf.gather_nd(A, tf.transpose(tf.pack([tf.to_int64(tf.range(A.get_shape()[0])), I])))
Where to_int64 is required, because the range creates only int32 , and argmax creates only int64 .
None of the two seemed particularly elegant to me. One of them has overhead (probably around factor n ), and the other has an unknown cognitive overhead factor. Did I miss something?
source share