Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: FailedPreconditionError: Table not initialized (using tf.data.Dataset API)

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!

like image 805
anon_swe Avatar asked Oct 26 '18 16:10

anon_swe


1 Answers

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 !

like image 141
abcdaire Avatar answered Oct 20 '22 00:10

abcdaire