Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError #6

Open
Soe2Lwin opened this issue Mar 2, 2020 · 5 comments
Open

KeyError #6

Soe2Lwin opened this issue Mar 2, 2020 · 5 comments

Comments

@Soe2Lwin
Copy link

Soe2Lwin commented Mar 2, 2020

I have got this key error. Please help me. Thank you
KeyError Traceback (most recent call last)

in ()
2 print("Review:",seq2text(x_tr[i]))
3 print("Original summary:",seq2summary(y_tr[i]))
----> 4 print("Predicted summary:",decode_sequence(x_tr[i].reshape(1,max_text_len)))
5 print("\n")

in decode_sequence(input_seq)
17 # Sample a token
18 sampled_token_index = np.argmax(output_tokens[0, -1, :])
---> 19 sampled_token = reverse_target_word_index[sampled_token_index]
20
21 if(sampled_token!='eostok'):

KeyError: 0

@shifatul-i
Copy link

shifatul-i commented Mar 9, 2020

sampled_token_index = np.argmax(output_tokens[0, -1, :]) + 2

solved here: #3

@Soe2Lwin
Copy link
Author

Thank @shifatul-i for your help
Key error issue is ok and I have got empty predicted summary like this. Please help me for error.
Screenshot from 2020-03-10 10-33-12

Thank you so much

@vjzo
Copy link

vjzo commented Mar 11, 2020

I was successfully able to run the entire code set on the problem I am working on.

when I run apply the code to my problem, I get the following output for the code as follows.

Code:
for i in range(len(x_val)):
print(“Review:”,seq2text(x_val[i]))
print(“Original summary:”,seq2summary(y_val[i]))
print(“Predicted summary:”,decode_sequence(x_val[i].reshape(1,max_text_len)))
print("\n")

Output example:
Review: please serial number issue truck
Original summary: over the truck and found the and forward not working with good and the issues is picked up new parts from the shop returned
Predicted summary: located truck and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and

How do I fix the output to get a better result. Any help would be highly appreciated. Thanks.

@shrenik007
Copy link

I was successfully able to run the entire code set on the problem I am working on.

when I run apply the code to my problem, I get the following output for the code as follows.

Code:
for i in range(len(x_val)):
print(“Review:”,seq2text(x_val[i]))
print(“Original summary:”,seq2summary(y_val[i]))
print(“Predicted summary:”,decode_sequence(x_val[i].reshape(1,max_text_len)))
print("\n")

Output example:
Review: please serial number issue truck
Original summary: over the truck and found the and forward not working with good and the issues is picked up new parts from the shop returned
Predicted summary: located truck and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and

How do I fix the output to get a better result. Any help would be highly appreciated. Thanks.

Facing similar issue

@MichaelJanz
Copy link

It looks like your models are overfitting. @VJSo007 @shrenik007
Did you set dropout = 0.4 and recurrent_dropout=0.4 in your LSTM layers?

If yes, you can improve text generation by applying a randomness, or even better beam-search.

Randomness can be applying be using
sampled_token_index = np.random.choice(len(prediction_output), p=prediction_output)
in decode_sequence()

Thats my decode_sentence function:
`def decode_sequence(input_seq):
# Encode the input as state vectors.
e_out, e_h, e_c = encoder_model.predict(input_seq)
print(e_out.shape)
print(e_h.shape)
print(e_c.shape)
# Generate empty target sequence of length 1.
target_seq = np.zeros((1,1))

# Chose the 'start' word as the first word of the target sequence
target_seq[0, 0] = target_word_index['sostock']

print(np.array(target_seq).shape)
print(e_out.shape)
print(e_h.shape)
print(e_c.shape)
stop_condition = False
decoded_sentence = ''
while not stop_condition:
    output_tokens, h, c = decoder_model.predict([[target_seq] + [e_out, e_h, e_c]])
    output_tokens = output_tokens[:1]
    h_1 = h[:1]
    c_1 = c[:1]
    # Sample a token
    #Use a random choice as described in this stackoverflow solution:
    #https://stackoverflow.com/questions/47125723/keras-lstm-for-text-generation-keeps-repeating-a-line-or-a-sequence
    #sampled_token_index = np.argmax(output_tokens[0, -1, :])
    prediction_output = output_tokens[0,-1,:]
    print(output_tokens)
    sampled_token_index = np.random.choice(len(prediction_output), p=prediction_output)
    sampled_token = reverse_target_word_index[sampled_token_index]

    if(sampled_token!='eostock'):
        decoded_sentence += ' '+sampled_token

    # Exit condition: either hit max length or find stop word.
    if (sampled_token == 'eostock' or len(decoded_sentence.split()) >= (max_len_summary-1)):
        stop_condition = True

    # Update the target sequence (of length 1).
    target_seq = np.zeros((1,1))
    target_seq[0, 0] = sampled_token_index

    # Update internal states
    e_h, e_c = h, c

return decoded_sentence`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants