StaticHashTable
- as of TF 2.3 - cannot return multi-dimensional values, let alone ragged ones. So despite padding the values, creating a hash table like this:
keys = ["Fritz", "Franz", "Fred"]
values = [[1, 2, 3, -1], [4, 5, -1, -1], [6, 7, 8, 9]]
table_init = tf.lookup.KeyValueTensorInitializer(keys, values)
table = tf.lookup.StaticHashTable(table_init, -1)
will throw the following error:
InvalidArgumentError: Expected shape [3] for value, got [3,4] [Op:LookupTableImportV2]
To circumvent this, you can use Dense hash tables although it is in experimental mode. Neither dense nor static hash tables provide support for ragged keys or values. So your best bet is to pad your values, and create a dense hash table. During lookup, you can rag them back. The overall code looks like this:
keys = ["Fritz", "Franz", "Fred"]
values = [[1, 2, 3, -1], [4, 5, -1, -1], [6, 7, 8, 9]]
table = tf.lookup.experimental.DenseHashTable(key_dtype=tf.string, value_dtype=tf.int64, empty_key="<EMPTY_SENTINEL>", deleted_key="<DELETE_SENTINEL>", default_value=[-1, -1, -1, -1])
table.insert(keys, values)
And during lookup:
>>> tf.RaggedTensor.from_tensor(table.lookup(['Franz', 'Emil']), padding=-1)
<tf.RaggedTensor [[4, 5], []]>