How-to-build-own-text-summarizer-using-deep-learning icon indicating copy to clipboard operation
How-to-build-own-text-summarizer-using-deep-learning copied to clipboard

KeyError

Open Soe2Lwin opened this issue 5 years ago • 5 comments

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

Soe2Lwin avatar Mar 02 '20 15:03 Soe2Lwin

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

solved here: https://github.com/aravindpai/How-to-build-own-text-summarizer-using-deep-learning/issues/3

shifatul-i avatar Mar 09 '20 22:03 shifatul-i

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

Soe2Lwin avatar Mar 10 '20 04:03 Soe2Lwin

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.

vjzo avatar Mar 11 '20 13:03 vjzo

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

shrenik007 avatar Apr 16 '20 13:04 shrenik007

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`

MichaelJanz avatar Jun 22 '20 09:06 MichaelJanz