1

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 word index (m.input_data:x).

def run_epoch(session, m, data, eval_op, verbose=False):
  """Runs the model on the given data."""
  epoch_size = ((len(data) // m.batch_size) - 1) // m.num_steps
  start_time = time.time()
  costs = 0.0
  iters = 0
  state = m.initial_state.eval()
  for step, (x, y) in enumerate(reader.ptb_iterator(data, m.batch_size,
                                                    m.num_steps)):
    cost, state, _ = session.run([m.cost, m.final_state, eval_op],
                                 {m.input_data: x,
                                  m.targets: y,
                                  m.initial_state: state})

I'd like to see actual words instead of ids, how can I see them?

Hub
  • 25
  • 5

1 Answers1

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,

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

...
for step, (x, y) in enumerate(...

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

print(message)

Hope it helps.

Jin
  • 704
  • 4
  • 11