How to convert `tf.contrib.lookup.index_table_from_file` into Tensorflow v2

Viewed 815

I'm new to Tensorflow and working on code from v1 Tensorflow, but tf.contrib module is no longer supported in Tensorflow and I'm facing trouble finding their substitute in V2.
I disabled V2 with this code :

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

But still it is not working for tf.contrib.lookup.index_table_from_file and I'm getting an error AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'. I tried looking in their official documentation Here but couldn't find one.

What is the substitute for tf.contrib.lookup.index_table_from_file function in V2?

1 Answers

You can try tf.lookup.StaticVocabularyTable to achieve the same.
If you want to bring index and value from TextFile you can use tf.lookup.TextFileInitializer within tf.lookup.StaticVocabularyTable.

Below is an example.

num_oov_buckets = 3
input_tensor = tf.constant(["emerson", "lake", "palmer", "king", "crimnson"])
table = tf.lookup.StaticVocabularyTable(
    tf.lookup.TextFileInitializer(
        filename,
        key_dtype=tf.string, key_index=tf.lookup.TextFileIndex.WHOLE_LINE,
        value_dtype=tf.int64, value_index=tf.lookup.TextFileIndex.LINE_NUMBER,
        delimiter="\t"),
    num_oov_buckets)
out = table.lookup(input_tensor)    

You can follow this Official Documentation from Tensorflow for more details.

Related