2

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.

Amith Adiraju
  • 306
  • 4
  • 18
  • Did you read the error messages? They are extremely explicit. Your first example has the wrong type and the second one has the wrong shape. – o-90 Jun 04 '21 at 02:21
  • I knw that what I'm passing in not acceptable, but I don't know how to fix them to accept arrays. That's the whole point of this qstn, how to change my values so that I can pass arrays through them. – Amith Adiraju Jun 04 '21 at 03:52
  • Literally going by the error statements and making appropriate changes doesn't solve the issue, just an FYI. Default behaviour isn't for supporting arrays: Spark.apache.org/docs/latest/mllib-collaborative-filtering.html. Which is why I was looking for creative ways to solve the issue with communities' support. – Amith Adiraju Jun 04 '21 at 04:05

0 Answers0