Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to index a list with a TensorFlow tensor?

Assume a list with non concatenable objects which needs to be accessed via a look up table. So the list index will be a tensor object but this is not possible.

 tf_look_up = tf.constant(np.array([3, 2, 1, 0, 4]))
 index = tf.constant(2)
 list = [0,1,2,3,4]

 target = list[tf_look_up[index]]

This will bring out the following error message.

 TypeError: list indices must be integers or slices, not Tensor

Is the a way/workaround to index lists with tensors?

like image 744
spreisel Avatar asked Dec 16 '16 15:12

spreisel


People also ask

How do you access the element in a tensor?

To access elements from a 3-D tensor Slicing can be used. Slicing means selecting the elements present in the tensor by using “:” slice operator. We can slice the elements by using the index of that particular element.

What is index in tensor?

In mathematics and mathematical physics, raising and lowering indices are operations on tensors which change their type. Raising and lowering indices are a form of index manipulation in tensor expressions.

Can you slice tensors?

You can use tf. slice on higher dimensional tensors as well. You can also use tf. strided_slice to extract slices of tensors by 'striding' over the tensor dimensions.

Is a tensor a NumPy array?

The difference between a NumPy array and a tensor is that the tensors are backed by the accelerator memory like GPU and they are immutable, unlike NumPy arrays.


2 Answers

tf.gather is designed for this purpose.

Simply run tf.gather(list, tf_look_up[index]), you'll get what you want.

like image 58
soloice Avatar answered Oct 14 '22 17:10

soloice


Tensorflow actually has support for a HashTable. See the documentation for more details.

Here, what you could do is the following:

table = tf.contrib.lookup.HashTable(
    tf.contrib.lookup.KeyValueTensorInitializer(tf_look_up, list), -1)

Then just get the desired input by running

target = table.lookup(index)

Note that -1 is the default value if the key is not found. You may have to add key_dtype and value_dtype to the constructor depending on the configuration of your tensors.

like image 43
jbird Avatar answered Oct 14 '22 17:10

jbird