61
$\begingroup$

I came across term perplexity which refers to the log-averaged inverse probability on unseen data. Wikipedia article on perplexity does not give an intuitive meaning for the same.

This perplexity measure was used in pLSA paper.

Can anyone explain the need and intuitive meaning of perplexity measure?

$\endgroup$
5
  • $\begingroup$ How do i calculate perplexity for pLSA. I have datamatrix $X$ which has the count and by TEM algorithm $p(d)$ and $p(w|d)$ are calculated. $\endgroup$
    – Learner
    Commented May 4, 2011 at 11:44
  • 3
    $\begingroup$ I've checked the indices of 5 data mining / machine learning / predictive analytics books by Nisbett, Larose, Witten, Torgo, and Shemueli (plus coauthors) and this term doesn't occur in any of them. I'm perplexed :) $\endgroup$
    – zbicyclist
    Commented May 6, 2017 at 2:35
  • 2
    $\begingroup$ Perplexity is another fancy name for uncertainty. It can be considered as an intrinsic evaluation against extrinsic evaluation. Jan Jurafsky explains it elegantly with examples in accordance with language modeling here at youtube.com/watch?v=BAN3NB_SNHY $\endgroup$
    – bicepjai
    Commented Jul 5, 2017 at 22:27
  • 2
    $\begingroup$ @zbicyclist, If you're looking for examples in the wild, it's particularly common in NLP, and specifically for the evaluation of things like language models. $\endgroup$ Commented Dec 18, 2017 at 18:03
  • 1
    $\begingroup$ In some fields (e.g. economics) people talk about the numbers equivalent so that e.g. $\exp(H)$ where $H$ is entropy based on natural logarithms is an equivalent number of equally common categories. So, two categories each with probability 0.5 yield entropy of $\ln 2$ and exponentiation gets back 2 as the number of equally common categories. For unequal probabilities the numbers equivalent is not in general an integer. $\endgroup$
    – Nick Cox
    Commented May 14, 2019 at 19:33

4 Answers 4

25
$\begingroup$

You have looked at the Wikipedia article on perplexity. It gives the perplexity of a discrete distribution as

$$2^{-\sum_x p(x)\log_2 p(x)}$$

which could also be written as

$$\exp\left({\sum_x p(x)\log_e \frac{1}{p(x)}}\right)$$

i.e. as a weighted geometric average of the inverses of the probabilities. For a continuous distribution, the sum would turn into a integral.

The article also gives a way of estimating perplexity for a model using $N$ pieces of test data

$$2^{-\sum_{i=1}^N \frac{1}{N} \log_2 q(x_i)}$$

which could also be written

$$\exp\left(\frac{{\sum_{i=1}^N \log_e \left(\dfrac{1}{q(x_i)}\right)}}{N}\right) \text{ or } \sqrt[N]{\prod_{i=1}^N \frac{1}{q(x_i)}}$$

or in a variety of other ways, and this should make it even clearer where "log-average inverse probability" comes from.

$\endgroup$
4
  • $\begingroup$ Is there any particular distinction between when e is used as the exponent rather than 2? $\endgroup$
    – Henry E
    Commented May 11, 2017 at 12:23
  • 2
    $\begingroup$ @HenryE: no, and common logarithms base $10$ would work too - logarithms in different bases are proportional to each other and clearly $a^{\log_a x} = b^{\log_b x}$ $\endgroup$
    – Henry
    Commented May 11, 2017 at 13:32
  • $\begingroup$ I figured as much. I came across this answer when I was trying to understand why a piece of code was using e to calculate perplexity when all the other formulations I'd previously seen had been using 2. I realise now how important it is to know what value a framework uses as a base for the log loss calculation $\endgroup$
    – Henry E
    Commented May 11, 2017 at 15:12
  • $\begingroup$ looks like exponential entropy $\endgroup$
    – develarist
    Commented Aug 31, 2020 at 17:04
32
$\begingroup$

I found this rather intuitive:

The perplexity of whatever you're evaluating, on the data you're evaluating it on, sort of tells you "this thing is right about as often as an x-sided die would be."

http://planspace.org/2013/09/23/perplexity-what-it-is-and-what-yours-is/

$\endgroup$
2
17
$\begingroup$

I've wondered this too. The first explanation isn't bad, but here are my 2 nats for whatever that's worth.


First of all, perplexity has nothing to do with characterizing how often you guess something right. It has more to do with characterizing the complexity of a stochastic sequence.

We're looking at a quantity, $$2^{-\sum_x p(x)\log_2 p(x)}$$

Let's first cancel out the log and the exponentiation.

$$2^{-\sum_{x} p(x)\log_2 p(x)}=\frac{1}{\prod_{x} p(x)^{p(x)}}$$

I think it's worth pointing out that perplexity is invariant with the base you use to define entropy. So in this sense, perplexity is infinitely more unique/less arbitrary than entropy as a measurement.

Relationship to Dice

Let's play with this a bit. Let's say you're just looking at a coin. When the coin is fair, entropy is at a maximum, and perplexity is at a maximum of $$\frac{1}{\frac{1}{2}^\frac{1}{2}\times\frac{1}{2}^\frac{1}{2}}=2$$

Now what happens when we look at an $N$ sided dice? Perplexity is $$\frac{1}{\left(\frac{1}{N}^\frac{1}{N}\right)^N}=N$$

So perplexity represents the number of sides of a fair die that when rolled, produces a sequence with the same entropy as your given probability distribution.

Number of States

OK, so now that we have an intuitive definition of perplexity, let's take a quick look at how it is affected by the number of states in a model. Let's start with a probability distribution over $N$ states, and create a new probability distribution over $N+1$ states such that the likelihood ratio of the original $N$ states remain the same and the new state has probability $\epsilon$. In the case of starting with a fair $N$ sided die, we might imagine creating a new $N + 1$ sided die such that the new side gets rolled with probability $\epsilon$ and the original $N$ sides are rolled with equal likelihood. So in the case of an arbitrary original probability distribution, if the probability of each state $x$ is given by $p_x$, the new distribution of the original $N$ states given the new state will be $$p^\prime_x=p_x\left(1-\epsilon\right)$$, and the new perplexity will be given by:

$$\frac{1}{\epsilon^\epsilon\prod_x^N {p^\prime_x}^{p^\prime_x}}=\frac{1}{\epsilon^\epsilon\prod_x^N {\left(p_x\left(1-\epsilon\right)\right)}^{p_x\left(1-\epsilon\right)}} = \frac{1}{\epsilon^\epsilon\prod_x^N p_x^{p_x\left(1-\epsilon\right)} {\left(1-\epsilon\right)}^{p_x\left(1-\epsilon\right)}} = \frac{1}{\epsilon^\epsilon{\left(1-\epsilon\right)}^{\left(1-\epsilon\right)}\prod_x^N p_x^{p_x\left(1-\epsilon\right)}} $$

In the limit as $\epsilon\rightarrow 0$, this quantity approaches $$\frac{1}{\prod_x^N {p_x}^{p_x}}$$

So as you make make rolling one side of the die increasingly unlikely, the perplexity ends up looking as though the side doesn't exist.

$\endgroup$
4
  • 3
    $\begingroup$ Surely that's only ~1.39 nats worth? $\endgroup$ Commented Feb 5, 2018 at 22:48
  • $\begingroup$ Can you elaborate how you get $$\prod_x^N {p^\prime_x}^{p^\prime_x} = (1-\epsilon)^{1-\epsilon}\prod_x^N {p_x}^{p_x(1-\epsilon)}$$? I can only do $$\prod_x^N {p^\prime_x}^{p^\prime_x} = \prod_x^N {(p_x (1-\epsilon)) }^{p_x(1-\epsilon)} = \prod_x^N {(1-\epsilon) }^{p_x(1-\epsilon)} \prod_x^N {p_x }^{p_x(1-\epsilon)}$$ $\endgroup$
    – user2740
    Commented Oct 20, 2018 at 21:19
  • $\begingroup$ $$\prod_x^N\left{(1-\epsilon\right)}^{p_x\left(1-\epsilon\right)}={\left(1-\epsilon\right)}^{\sum_x^N p_x \left(1-\epsilon\right)}={\left(1-\epsilon\right)}^{\left(1-\epsilon\right)\sum_x^N p_x}={\left(1-\epsilon\right)}^{\left(1-\epsilon\right)}$$ $\endgroup$ Commented Oct 22, 2018 at 0:27
  • $\begingroup$ What conclusion should we draw from the section “Number of States”? $\endgroup$
    – beroal
    Commented Jan 25 at 15:16
7
$\begingroup$

There is actually a clear connection between perplexity and the odds of correctly guessing a value from a distribution, given by Cover's Elements of Information Theory 2ed (2.146): If $X$ and $X'$ are iid variables, then

$P(X=X') \ge 2^{-H(X)} = \frac{1}{2^{H(X)}} = \frac{1}{\text{perplexity}}$ (1)

To explain, perplexity of a uniform distribution X is just |X|, the number of elements. If we try to guess the values that iid samples from a uniform distribution X will take by simply making iid guesses from X, we will be correct 1/|X|=1/perplexity of the time. Since the uniform distribution is the hardest to guess values from, we can use 1/perplexity as a lower bound / heuristic approximation for how often our guesses will be right.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.