I'm using the tf.data.Dataset
API in conjunction with tf.contrib.lookup.index_table_from_tensor
.
My dataset was created like this:
dataset = tf.data.Dataset.from_tensor_slices(({'reviews': x}, y)))
Here's what I'm doing:
data_table = tf.contrib.lookup.index_table_from_tensor(tf.constant(data_vocab))
labels_table = tf.contrib.lookup.index_table_from_tensor(tf.constant(labels_vocab))
Then I map a preprocessing function across my dataset
:
def preprocess(x, y):
# split on whitespace
x['reviews'] = tf.string_split([x['reviews']])
# turn into integers
return data_table.lookup(x['reviews']), labels_table.lookup(y)
All good so far. However, when I try to pass my dataset to my Keras model for training, I get:
tensorflow.python.framework.errors_impl.FailedPreconditionError: Table not initialized.
I googled around and people suggest that I need to include:
sess = tf.Session()
sess.run(tf.tables_initializer())
But now I get:
tensorflow.python.framework.errors_impl.FailedPreconditionError: Table not initialized.
[[Node: hash_table_Lookup = LookupTableFindV2[Tin=DT_STRING, Tout=DT_INT64](hash_table_lookup_placeholder, StringSplit:1, hash_table_lookup_placeholder_1)]]
[[Node: IteratorGetNext_1 = IteratorGetNext[output_shapes=[[?,?], [?,20]], output_types=[DT_INT64, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator_1)]]
Any idea why my lookup table is still not initialized / how to fix this?
Thanks!
Hi it's quiet weird maybe the following working example will help you:
x = ['this is aswesome', 'i dont like it', 'i love it', 'i hate it']
y = ['positive','negative','positive','negative']
data_vocab = list({word for sentence in x for word in sentence.split(' ')})
label_vocab = list(set(y))
dataset = tf.data.Dataset.from_tensor_slices(({'reviews': x}, y))
data_table=tf.contrib.lookup.index_table_from_tensor(tf.constant(data_vocab))
labels_table = tf.contrib.lookup.index_table_from_tensor(tf.constant(label_vocab))
def preprocess(x, y):
# split on whitespace
x['reviews'] = tf.string_split([x['reviews']])
# turn into integers
return data_table.lookup(x['reviews']), labels_table.lookup(y)
preprocessed = dataset.map(preprocess)
it = preprocessed.make_initializable_iterator()
sess = tf.Session()
sess.run(it.initializer)
sess.run(tf.tables_initializer())
If you call sess.run(it.get_next())
you get (SparseTensorValue(indices=array([[0, 0],
[0, 1],
[0, 2]]), values=array([2, 7, 4]), dense_shape=array([1, 3])), 1)
Hope this will help you !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With