How to use tensor flow look up to get Array values based on String key

Viewed 169

I'm looking to use tensor flow lookup to get array values based on a string key.

Ex: Given a value 'Normal_DEF' -> corresponding array [0.1,0.2,0.3] must be returned.
Similarly 'Normal_AGING,DRY,DULL' -> should return [0.25,0.36,0.45].

So far I've used KeyValueTensorInitializer, along with Static Vocabulary and HashTable based on suggestions from here:

  1. Tensorflow Dictionary lookup with String Tensor
  2. Tensorflow Table lookup from int -> float
  3. How to use Tensorflow look up tables

But they don't seem to work for value pairs which are arrays.

Code snippets I tried:

Static Vocabulary table:

keys = tf.constant([ ['NORMAL_DEF'], ['NORMAL_AGING,DARKSPOTS,BRIGHTSPOTS'], ["DRY_SKINTONE"]  ])

# Tried defining arrays in multiple ways, but look up isn't considering array values
# as array entires
values = tf.constant( [ [ (1.0, 2.0) ], [ (3.0, 4.0) ], [ (3.0, 4.0 )] ] )

kvin = tf.lookup.StaticVocabularyTable( tf.lookup.KeyValueTensorInitializer(keys = keys, values = values), num_oov_buckets = 3)

Errors: TypeError: Invalid value dtype, expected <dtype: 'int64'> but got <dtype: 'float32'>.

Static Hash Table:

keys = tf.constant([ ['NORMAL_DEF'], ['NORMAL_AGING,DARKSPOTS,BRIGHTSPOTS'], ["DRY_SKINTONE"]  ])
    
    # Tried defining arrays in multiple ways, but look up isn't considering array values
    # as array entires
    values = tf.constant( [ [ (1.0, 2.0) ], [ (3.0, 4.0) ], [ (3.0, 4.0 )] ] )

sht = tf.lookup.StaticHashTable(tf.lookup.KeyValueTensorInitializer(keys = keys,
                                                                      values = values), default_value = tf.constant([0.0,0.0,0.0])
                         )

Errors: ValueError: Shapes (3,) and () must have the same rank

TF Version: 2.3.1

Any help is appreciated. Thanks.

0 Answers
Related