Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-026

Perplexity from Probabilities

Problem

Return the perplexity of a sequence of probabilities: exp of the negative mean log probability, that is exp(-(sum(log(p)) / N)).

Return 1.0 for an empty list.

Hint

Average the log probabilities first, then undo the mean with exp.

Test cases

case 1
in: [[0.5,0.5]]
expected: 2
case 2
in: [[0.25,0.25,0.25,0.25]]
expected: 4
case 3
in: [[1,1]]
expected: 1
case 4
in: [[0.5,0.25]]
expected: 2.82842712474619
case 5
in: [[]]
expected: 1
Starter codePython
import math
def perplexity(probs):
    # Your code here
    pass

5 test cases · run your code against them in the editor