dennybritz / rnn-tutorial-rnnlm

Recurrent Neural Network Tutorial, Part 2 - Implementing a RNN in Python and Theano
Apache License 2.0
895 stars 468 forks source link

Some basic errors I am getting with code while loading and testing the model. #8

Open neoblizz opened 8 years ago

neoblizz commented 8 years ago

Great tutorial, learning a lot reading through this! I collected the code from the tutorial as follows but I don't understand how you made it work because I get some very basic errors (I am new to RNN):

vocabulary_size = 8000
unknown_token = "UNKNOWN_TOKEN"
sentence_start_token = "SENTENCE_START"
sentence_end_token = "SENTENCE_END"

from rnn_theano import RNNTheano
from utils import load_model_parameters_theano, save_model_parameters_theano

word_to_index = dict([(w,i) for i,w in enumerate(index_to_word)])

model = RNNTheano(vocabulary_size, hidden_dim=50)
load_model_parameters_theano('./data/trained-model-theano.npz', model)

def generate_sentence(model):
    # We start the sentence with the start token
    new_sentence = [word_to_index[sentence_start_token]]
    # Repeat until we get an end token
    while not new_sentence[-1] == word_to_index[sentence_end_token]:
        next_word_probs = model.forward_propagation(new_sentence)
        sampled_word = word_to_index[unknown_token]
        # We don't want to sample unknown words
        while sampled_word == word_to_index[unknown_token]:
            samples = np.random.multinomial(1, next_word_probs[-1])
            sampled_word = np.argmax(samples)
        new_sentence.append(sampled_word)
    sentence_str = [index_to_word[x] for x in new_sentence[1:-1]]
    return sentence_str

num_sentences = 10
senten_min_length = 7

for i in range(num_sentences):
    sent = []
    # We want long sentences, not sentences with one or two words
    while len(sent) < senten_min_length:
        sent = generate_sentence(model)
    print " ".join(sent)

It's a silly error but I can't figure out a fix, will greatly appreciate if you can help me test the model.

Traceback (most recent call last):
  File "rnn_test.py", line 11, in <module>
    word_to_index = dict([(w,i) for i,w in enumerate(index_to_word)])
NameError: name 'index_to_word' is not defined
yuanzhigang10 commented 8 years ago

Is the code you pasted here a complete copy? You may have to read the original data to get index_to_word, just load_model_parameters is not enough (since you have to map index to word). The code in RNNLM.ipynb is a self-contained one, you can try this before playing with theano.

toandang commented 6 years ago

Change it to: next_wordprobs, = model.forward_propagation(new_sentence)