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?
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.
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.
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.
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.
tf.gather
is designed for this purpose.
Simply run tf.gather(list, tf_look_up[index])
, you'll get what you want.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With