Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-083

Bigram Perplexity

Problem

Given a 2D table of bigram counts, compute the conditional entropy H(Y | X) in bits and return the perplexity 2^H. Rows with zero total are skipped.

Hint

Perplexity is the exponentiated conditional entropy of the next token.

Test cases

case 1
in: [[[1,1],[1,1]]]
expected: 2
case 2
in: [[[10,0],[0,10]]]
expected: 1
case 3
in: [[[3,1],[0,4]]]
expected: 1.324675564281052
case 4
in: [[[1,2,3],[3,2,1]]]
expected: 2.7494592739972052
Starter codePython
import math
def bigram_perplexity(counts):
    # Your code here
    pass

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