Q10 calculate_perplexity

I am struggling at Q 10 calculating perplexity and this cause my answer in Q 11 wrong as well,
I have rechecked my code several times but still getting wrong output,

Can anyone help me

Hey @Wish_Suharitdamrong,
Welcome to the community. Can you please ensure that you haven’t used any global variables in your implementation? See if you have missed out on using any argument that has been passed to the function.

Cheers,
Elemento

Yes, I have recheck it again I didn’t use any global variable in calculate_perplexity function.

Hey @Wish_Suharitdamrong,
In that case, please DM me your implementation for the function, so that I can try to find out the issue.

Cheers,
Elemento

Hey @Wish_Suharitdamrong,
I tried your implementation. It’s perfectly fine and passing all the test-cases. I believe the issue lies in your implementation of estimate_probability, since that is the only other function used. Can you please DM me your implementation for the same?

Cheers,
Elemento

1 Like

Hey @Wish_Suharitdamrong,
There are 2 errors in your implementation of estimate_probability. First error lies in how you determine the previous_n_gram_count. You are checking if previous_n_gram is available in n_gram_counts, i.e., in the dictionary, whereas, you are supposed to check if it is available in the keys of the dictionary. By the way, a more concise way of writing the same thing is to use the get method of a Python dictionary. You can use it as follows:

<dict_name>.get(<key_name>, <default_value>)

Second error lies in how you determine the n_plus1_gram. While forming it, you are using previous_n_gram[0], i.e., only the first word in the previous_n_gram tuple, whereas, you are supposed to use the entire tuple, i.e., all the words in the previous_n_gram tuple to form the n_plus1_gram tuple.

Correcting these errors will resolve your query.

Cheers,
Elemento