Skip to content Skip to sidebar Skip to footer

Getting Word From Id At Tensorflow Rnn Sample

I'm trying to modify Tensorflow's RNN sample here. https://www.tensorflow.org/versions/r0.8/tutorials/recurrent/index.html At ptb_word_lm.py I guess they are inputting int array of

Solution 1:

You need to retain vocabulary ( which is an index from word to id ) first.

At the top of main, retain 4th returned value from reader.ptb_raw_data() like below.

raw_data = reader.ptb_raw_data(FLAGS.data_path)
train_data, valid_data, test_data, vocabulary = raw_data

Then pass the vocabulary to run_epoch().

test_perplexity = run_epoch(session, mtest, test_data, tf.no_op(), vocabulary)

Inside of the run_epoch(), when you want to convert ids to words in first step of x,

defrun_epoch(session, m, data, eval_op, vocabulary, verbose=False):

...
for step, (x, y) inenumerate(...

message ="x: "for i inrange(0, m.num_steps):
    key = vocabulary.keys()[vocabulary.values().index(x[0][i])]
    message += key + " "print(message)

Hope it helps.

Post a Comment for "Getting Word From Id At Tensorflow Rnn Sample"