In [2]:
import tensorflow as tf
import numpy as np
sess = tf.InteractiveSession()
In [9]:
vocab = tf.contrib.lookup.string_to_index_table_from_file(
vocabulary_file="vocab.txt", num_oov_buckets=0, default_value=1)
vocab_inv = tf.contrib.lookup.index_to_string_table_from_file(
vocabulary_file="vocab.txt", default_value="<UNK>")
s = vocab.size()
tf.tables_initializer().run()
sess.run(tf.tables_initializer()) # make sure to call the table initializer
In [24]:
sentence = tf.constant('<PAD> the quick brown fox jumped over the lazy dog .'.split(), tf.string)
print sentence.eval()
idx = sess.run(vocab.lookup(sentence))
print idx
idxs = tf.constant(idx)
strings = sess.run(vocab_inv.lookup(idxs))
print strings
In [ ]: