Explanation of `tf.gather_nd` in Tensorflow

Can you intuitively explain or give more examples of tf.gather_nd for indexing and cutting into high-dimensional tensors in Tensorflow? I read the API , but it is concise enough that it is difficult for me to follow the concept of a function.

Thanks.

+7
tensorflow
source share
1 answer

Ok, think of it this way:

You provide a list of indexes to index the provided tensor to obtain these values. The first dimension of indices is for each index that you will perform. Suppose a tensor is just a list of lists.

[[0]] means that you want to return one given value to index 0 in the provided tensor. Similar:

 [tensor[0]] 

[[0], [1]] means that you want to return two values ​​at indices 0 and 1 as follows:

 [tensor[0], tensor[1]] 

Now, what if the tensor is more than one dimension? We do the same:

[[0, 0]] means that you want to return one value to index 0 of dimension 0, and then index 0 of dimension 1. Also:

 [tensor[0][0]] 

[[0, 1], [2, 3]] means that you want to return two values ​​for the provided indices and sizes. For example:

 [tensor[0][1], tensor[2][3]] 

Hope this makes sense. I tried using Python indexing to explain how it would look in Python to do this in a list of lists.

+5
source share

All Articles