Skip to content
DeepForgeby svx
All problems
HardNLPnlp-169

LDA Document-Topic Step

Problem

Perform one document-topic E-step of LDA. For each topic, gamma = sum over words of count_w * P(word | topic), then normalize the gammas into a distribution over topics.

Return a uniform distribution when the total gamma is 0 (including a document with no words), and an empty dictionary when there are no topics.

Hint

Unnormalized gammas weight each topic by how well it explains the document counts.

Test cases

case 1
in: [{"cat":2,"dog":1},{"t1":{"cat":0.5,"dog":0.5},"t2":{"cat":0.1,"dog":0.9}}]
expected: {"t1":0.5769230769230769,"t2":0.4230769230769231}
case 2
in: [{"cat":0,"dog":0},{"t1":{"cat":0.5,"dog":0.5},"t2":{"cat":0.1,"dog":0.9}}]
expected: {"t1":0.5,"t2":0.5}
case 3
in: [{},{"t1":{"cat":0.5,"dog":0.5},"t2":{"cat":0.1,"dog":0.9}}]
expected: {"t1":0.5,"t2":0.5}
case 4
in: [{"cat":1},{}]
expected: {}
case 5
in: [{"cat":1},{"t1":{"cat":1}}]
expected: {"t1":1}
Starter codePython
def lda_doc_topic_step(doc_counts, topic_word_probs):
    # Your code here
    pass

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