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.
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.
tf.gather
is designed for this purpose.
Simply run tf.gather(list, tf_look_up[index])
, you'll get what you want.