Machine Learning · Insper · E3.1

LDA

Advanced approach

Does fitting recover the topics, and when does it fail?

The path of this part

  1. Recover known topicsa synthetic corpus, written by the story
  2. Too few words53 words, 20 seeds
  3. The knobsα, η and the random start
Each stage answers one question: does LDA (latent Dirichlet allocation) find topics that are known? → what happens with too little evidence? → what can be set, and what does it change?
Stage 1 · does LDA find topics that are known?

A corpus written by the story itself

  • 3 topics over 9 words: each puts 0.35, 0.30, 0.25 on its own three words and \(0.10/6\) on each of the others.
  • 300 documents, mixtures \(\theta_d \sim \text{Dirichlet}(0.3)\), 30 words each: 9,000 words.
  • Fit LatentDirichletAllocation(n_components=3) on the counts alone.
Predict. Will the estimated topics be close to the true ones? Will they come out in the same order?

The topics come back

teamgoalgamerocketorbitmoonbreadovenbake
sports0.350.310.250.020.010.020.010.020.02
space0.010.010.020.360.310.260.010.010.01
cooking0.010.010.010.010.010.020.340.320.26
  • Each row of components_, divided by its sum, against the truth: within 0.01.
  • Estimated against true mixtures \(\theta\): correlation 0.97 for every topic.
The topics come out in an arbitrary order: the code lines them up with the truth first. Topic "2" of one run can be topic "0" of another.
Stage 2 · what happens with too little evidence?

And on the 53 hand-written words?

Predict. Three topics on the 11 hand-made sentences: will LDA separate sports, space and cooking?

  • Seed 0: bread fresh game oven · team goal game orbit · rocket launch bread delayed: the themes mixed.
  • Each document still goes confidently (0.86 to 0.88) to one topic.
  • Over 20 seeds, only 1 separates the nine single-theme sentences cleanly.
Nothing is wrong with the code: 53 words carry too little evidence and the random start decides. LDA needs many documents with enough words each.
Stage 3 · what can be set, and what does it change?

The knobs: two priors and a seed

\(\alpha\): doc_topic_prior

how mixed a document is; small → few topics per document. Default \(1/k\).

\(\eta\): topic_word_prior

how spread a topic is over the words; small → few words per topic. Default \(1/k\).

\(k\): n_components

fixed in advance; the advanced part of E3.2 NMF discusses how to choose it

random_state

the random start of the optimization: it decided everything on 53 words

Practice · at home

Your turn (advanced)

  • Less data, same story: 30 documents instead of 300, then 5 words each instead of 30. Which matters more?
  • A wrong prior: mixed documents (\(\text{Dirichlet}(3)\)) fitted with doc_topic_prior=0.05.
  • Seeds on the posts: is the 53-word lottery a problem with 2,155 posts?

Next: the advanced slides of E3.2 NMF.