5

my question is how to map tensor with a dictionary? for example like this:

dict = {1:3, 2:4}
origin_tensor = tf.Variable([1,2,1], tf.int32)

The dictionary is large. Now, How can I make a map options to map the tensor to tf.Variable([3,4,3], tf.int32) according to the dict ?

What's more, it is no way to use .eval() when mapping, you can think the origin_tensor is a label tensor from batch reader.

zhangqiulu
  • 51
  • 4

2 Answers2

3

In Tensorflow 2.0 (compatibility with earlier versions not tested) use tf.lookup:

dictionary = {1:3, 2:4}
origin_tensor = tf.Variable([1,2,1], dtype=tf.int64)

note: dict is reserved in python so it is replaced with dictionary and dtype=tf.int32 is replaced with dtype=tf.int64 for compatibility with tf.lookup.KeyValueTensorInitializer

This is the original tensor:

origin_tensor
>> <tf.Variable 'Variable:0' shape=(3,) dtype=int64, numpy=array([1, 2, 1])>

This is the Tensorflow lookup table made from a key-value tensor initialized from a python dictionary:

table = tf.lookup.StaticVocabularyTable(
        tf.lookup.KeyValueTensorInitializer(
            list(dictionary.keys()),
            list(dictionary.values()),
            key_dtype=tf.int64,
            value_dtype=tf.int64,
        ),
        num_oov_buckets=1,
    )

This is the actual lookup that returns the result_tensor with desired elements based on the lookup table:

result_tensor = table.lookup(origin_tensor)

Here is the result:

result_tensor
>> <tf.Tensor: id=400475, shape=(3,), dtype=int64, numpy=array([3, 4, 3])>

Cheers!

1

You can use the tf.map_fn() function. Since the situation you described shows a connection of x=x+1, which can be interpreted in Tensorflow as:

elems = np.array([1, 2])
plus_one = tf.map_fn(lambda x: x + 1, elems)
# plus_one == [3, 4]
  • 8
    How about more general mapping? E.g., `m={0:0, 1:2, 2:2, 3:2, 4:2, 5:3, 6:1}`. Can we use `dict` for that like `tf.map_fn(lambda x: m[x], elems)`? – YW P Kwon Jun 06 '17 at 21:22
  • 1
    @user3928118 OP's elems would be a tensor, not a np array of values as shown in the sample code. I am dealing with a similar issue right now and I cannot figure out how to use map_fn, if it even applies. – Todd Gillette Sep 11 '19 at 20:00