A language model read a text one word at a time and gave each actual next word a chance. Write perplexity(chances): one number for how confused the model was, said as “it was like choosing among k words”.
A model read “the cat sat” one word at a time, and gave each word the chance it saw it coming. A chance says how unsure the model was: 50% on the right word is as unsure as a fair pick among 2 words. Your function says it for the whole text.
perplexity(chances)| chances | the chance the model gave each actual next word | e.g. [1, 1, 1] |
| returns | how confused it was, as "like choosing among k words" | |