Reputation: 276
I have a tensor tf.shape(X) == [M, N, N]
and a set of indices tf.shape(IDX) == [N, N]
. How can I form a tensor tf.shape(Y) = [N, N]
, which equals to the slice of X
using indices IDX
in the first dimension? I.e.
Y[i, j] = X[IDX[i, j], i, j]
for all i,j = 1..N
.
I have tried to play with tf.gather_nd
but with no result :(
Upvotes: 1
Views: 1375
Reputation: 5162
Update 10-12-2016:
As of tensorflow version 0.11 and up one can index into tensors in the same way as numpy.
a = tf.Variable([9,10,11])
b = tf.constant([[1,2,3,4],[5,6,7,8]])
a = b[0,1:]
Gradients are also supported on the indexing.
What did you try already?
It seems like there's a bug with tf.gather_nd that I reported. Here's the response
Support for partial indices in gather_nd (fewer indices than dimensions) was added quite recently. You are you using a version of TensorFlow where each index tensor must have exactly the number of tensor dimensions. The code should work at HEAD.
so by version 0.10 or above gather_nd should work like you want.
However this below works
import tensorflow as tf
x = tf.constant([[1,1,1,1],[1,2,3,4]],shape=(2,4))
indices = [[0,0],[0,1]]
y = tf.gather_nd(x,indices)
so it seems like you need the full index description at the moment, not just slice 0. You also try tf.pack.
You can also track the progress of indexing tensors in tensorflow here: https://github.com/tensorflow/tensorflow/issues/206
Upvotes: 3