0
votes
def create_dataset(csv_path, vocab):

dataset = tf.data.TextLineDataset(csv_path).skip(1)
dataset = dataset.map(lambda sentence : tf.string_split([sentence]).values)
dataset = dataset.map(lambda tokens : (vocab.lookup(tokens), tf.size(tokens)))
return dataset

Above is the function I am using to lookup into table Below I am trying to pad the sentence with from lookup table

def input_fn( sentence, labels, id_pad):

dataset = tf.data.Dataset.zip((sentence, labels))

padded_shapes = ((tf.TensorShape([None]),
                  tf.TensorShape([])),
                 tf.TensorShape([]))

padded_value = ((id_pad,0),
                "")

dataset = (dataset
           .padded_batch(128,padded_shapes=padded_shapes,padding_values=padded_value)
           .prefetch(1))

iterator = dataset.make_initializable_iterator()
((sentence, sentence_length),(label)) = iterator.get_next()
init_op = iterator.initializer
inputs = {
    'sentence':sentence,
    'sentence_length': sentence_length,
    'label': label,
    'init_op':init_op
}

return inputs

Below I am creating and running the session:

vocab = tf.contrib.lookup.index_table_from_file( 'data\\vocab.txt', num_oov_buckets=1)
sentence_data = create_dataset('data\\csv\\amazon_feature.csv',vocab)
label_data = tf.data.TextLineDataset('data\\csv\\amazon_label.csv').skip(1)
id_pad = vocab.lookup(tf.constant('<PAD>'))
input = input_fn(sentence_data,label_data,id_pad)
with tf.Session() as sess:
sess.run([input['init_op'], tf.tables_initializer(), tf.global_variables_initializer()])

Error stacktrace is this.

Traceback (most recent call last): File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1322, in _do_call return fn(*args) File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1307, in _run_fn options, feed_dict, fetch_list, target_list, run_metadata) File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1409, in _call_tf_sessionrun run_metadata) tensorflow.python.framework.errors_impl.FailedPreconditionError: Table not initialized. [[Node: string_to_index_Lookup/hash_table_Lookup = LookupTableFindV2[Tin=DT_STRING, Tout=DT_INT64, _device="/job:localhost/replica:0/task:0/device:CPU:0"](string_to_index/hash_table, Const, string_to_index/hash_table/Const)]]

During handling of the above exception, another exception occurred:

1
Can you please print the error message traceback in the question?Soumendra
Updated the question with error messageAdarsh Kumar

1 Answers

0
votes

u should run tables_initializer firstly,then run others.like below:

with tf.Session as sess:

sess.run(tf.tables_initializer())

...