C2_W3 assigment calculate perplexity problem

Hello I have a problem with calculate_perplexity function.
No matter how I try I cannot make it calculate perplexity correctly.

Here is my output from test sentence.

Here are what I have done already.

  1. In the first for loop I already ran i starting from n to N-1 (already check to include N-1)
  2. In defining n_gram, i already make sure it is splicing from string → I used sentence[:t]
  3. In probability I use estimate_probability function from previous code and use parameter from calculate_perplexity function.

I’m not sure why it still calculating wrong value.
Thanks

The problem should be at point 2, the slicing should start from the end of the previous word (with length n)!

Wow, finally got it. Thanks a lot.
So my slicing method is wrong. It shouldn’t start from zero.

1 Like