1

There is a index tensor like this: [[1,2,3],[1,2,3]] (shape is batch * length)

and there is a value tensor like this: (shape is batch * length * deep)

[[[0.9,0.9,0.1,0.1],[0.9,0.1,0.8,0.1],[0.9,0.1,0.1,0.6]],
[[0.1,0.9,0.8,1],[1,2,0.8,0.1],[0.1,0.1,2,0.6]]]. 

how can I get [[0.9,0.8,0.6],[0.9,0.8,0.6]] with tensorflow?

2
  • tf.math.reduce_max(tensor, axis=-1)? Commented Sep 25, 2020 at 13:53
  • 2
    The problem I had was taking a row of values and using it as an index in the last dimension of another matrix, corresponding values. It's not about the maximum Commented Sep 26, 2020 at 1:07

1 Answer 1

2

I'm not sure that this is the best solution, but it works: tf.gather_nd(values, tf.expand_dims(index, -1), batch_dims=2)

e.g.:

>>> index = tf.constant([[1,2,3],[1,2,3]])
>>> values = tf.constant([[[0.9,0.9,0.1,0.1],[0.9,0.1,0.8,0.1],[0.9,0.1,0.1,0.6]],[[0.1,0.9,0.8,1],[1,2,0.8,0.1],[0.1,0.1,2,0.6]]])
>>> result = tf.gather_nd(values, tf.expand_dims(index, -1), batch_dims=2)
>>> result.eval()
array([[0.9, 0.8, 0.6],
       [0.9, 0.8, 0.6]], dtype=float32)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.