All problems
LDA Perplexity
Problem
Return the LDA corpus perplexity exp(-sum_d sum_w n_dw * log P(w | d) / total_words), where P(w | d) = sum over topics of theta_dk * phi_kw.
Return 1.0 when the corpus has no words.
Hint
Mix the topic-word distributions with each document's topic weights before taking logs.
Test cases
case 1
in: [[{"cat":2,"dog":1}],[{"t1":0.5,"t2":0.5}],{"t1":{"cat":0.5,"dog":0.5},"t2":{"cat":0.1,"dog":0.9}}]
expected: 2.5131581370971796
case 2
in: [[],[],{}]
expected: 1
case 3
in: [[{"cat":0}],[{"t1":1}],{"t1":{"cat":1}}]
expected: 1
case 4
in: [[{"cat":1},{"dog":1}],[{"t1":1},{"t2":1}],{"t1":{"cat":0.8,"dog":0.2},"t2":{"cat":0.1,"dog":0.9}}]
expected: 1.1785113019775793
Starter codePython
import math
def lda_perplexity(doc_counts, thetas, phi):
# Your code here
pass4 test cases · run your code against them in the editor