PIXELBANKv9.1.0
Menu

Compute the perplexity of a language model on a sequence.

Perplexity is defined as: PPL=exp⁡(−1N∑i=1Nlog⁡P(wi∣w<i))\text{PPL} = \exp\left(-\frac{1}{N} \sum_{i=1}^{N} \log P(w_i | w_{<i})\right)

Given the log-probabilities of each token in a sequence, compute the perplexity.

Input: Space-separated log-probabilities (natural log)

Output: Perplexity, rounded to 4 decimal places.

Example:

Input:
-1.0 -2.0 -1.5
Output:
4.4817
Reasoning:
  • First, we calculate the sum of the log-probabilities: −1.0+(−2.0)+(−1.5)=−4.5-1.0 + (-2.0) + (-1.5) = -4.5
  • Then, we calculate the average log-probability: −13⋅−4.5=1.5-\frac{1}{3} \cdot -4.5 = 1.5
  • Next, we apply the exponential function to get the perplexity: exp⁡(−1.5)=0.2231\exp(-1.5) = 0.2231 is incorrect, we actually need to use −1N∑i=1Nlog⁡P(wi∣w<i)=−13⋅(−4.5)=1.5-\frac{1}{N} \sum_{i=1}^{N} \log P(w_i | w_{<i}) = -\frac{1}{3} \cdot (-4.5) = 1.5, then exp⁡(−1.5)\exp(-1.5) is not the correct step. The correct step is to calculate exp⁡(−(−1.5))\exp(-(-1.5)) since the formula is exp⁡(−1N∑i=1Nlog⁡P(wi∣w<i))\exp\left(-\frac{1}{N} \sum_{i=1}^{N} \log P(w_i | w_{<i})\right) and −1N∑i=1Nlog⁡P(wi∣w<i)=1.5-\frac{1}{N} \sum_{i=1}^{N} \log P(w_i | w_{<i}) = 1.5. So, exp⁡(1.5)\exp(1.5)
  • The final output is exp⁡(1.5)≈4.4817\exp(1.5) \approx 4.4817, rounded to 4 decimal places.

Constraints:

  • Log-probabilities are negative (or zero)
  • Use natural log
  • Round to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.
Perplexity Calculator - Easy | PixelBank