biop
biop

Reputation: 65

Look Up Dictionary Tensorflow for a List inside array

i have a Tensor where i want to lookup and output a int for every word.

How can i scroll over this and put out a value for every word?

table =tf.lookup.StaticHashTable(tf.lookup.TextFileInitializer(vocab_filename, tf.string, 0, tf.int64, 1, delimiter="!"),7) 
out=table.lookup(data[0])
print(out)

<tf.Tensor: shape=(231637,), dtype=string, numpy=
array([b"['honey', 'apple', 'tree', 'butter', 'olive oil', 'salt']",

Upvotes: 1

Views: 477

Answers (1)

user11530462
user11530462

Reputation:

You can use the StaticVocabularyTable to achieve the same. Below is an example.

vocab = ["<1H OCEAN", "INLAND", "NEAR OCEAN", "NEAR BAY", "ISLAND"]
indices = tf.range(len(vocab), dtype=tf.int64)
table_init = tf.lookup.KeyValueTensorInitializer(vocab, indices)
num_oov_buckets = 20
table = tf.lookup.StaticVocabularyTable(table_init, num_oov_buckets) 

Result:

out = table.lookup(tf.constant(["NEAR OCEAN"]))
print(out)
tf.Tensor([2], shape=(1,), dtype=int64)

If you want to create Vocabulary List and Index values from the text file, you can follow the below 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 the document for detailed instruction.

Upvotes: 1

Related Questions