Latent Dirichlet allocation
A topic model in which every document is its own mixture of shared topics: the generative story, Dirichlet priors as pseudo-counts, Dirichlet–multinomial conjugacy, collapsed Gibbs sampling on counts, and mean-field variational inference.
One topic per document is not enough
The topic Dirichlet processes ended with mixture models whose every object belongs to exactly one cluster. Apply that to text, with a whole document as the object, and each document gets exactly one theme. Real documents are not like that: a report on a football club's finances is about sport and about money at once. This topic builds a mixture of mixtures, in which every document mixes several shared themes in its own proportions.
A word of warning first. In this lesson a topic almost always means an LDA topic, a distribution over words; earlier lessons of the course are named in italics.
Text is treated as a bag of words: a document is the multiset of its tokens, and word order is thrown away. Take a vocabulary of words (team, goal, market, price) and two topics, each a probability vector over the vocabulary:
A mixture of unigrams picks one topic per document with probabilities and draws all the words from it:
For the document "goal team price" with this gives . The sport topic alone explains "goal team" well but has to pay for "price", and no single topic does better.
LDA instead gives each document its own topic proportions and lets every word choose its topic separately. The probability of one word becomes a mixture:
With the words "goal" and "team" get each, "price" gets , and the document gets : about times what the sport topic alone gives and more than three times the mixture of unigrams.
The generative story
LDA is a generative model with topics, a vocabulary of size and documents, document having tokens. Two hyperparameters set symmetric Dirichlet priors: for the proportions, for the topics. The story:
- For each topic : draw the word distribution .
- For each document : draw the topic proportions .
- For each position in document : draw the topic assignment ,
- and then the word .
Only the words are observed. The joint distribution of everything is
Two kinds of latent variables sit in this formula. The topics are global: every document draws its words from the same topics. The proportions and the assignments are local: each belongs to one document or one token. This split will matter again in the topic Stochastic variational inference.
Summing out gives back the mixture of the previous block, and in matrix form the model says something familiar. Stack the into a matrix and the into a matrix . Then the matrix of word probabilities is : a non-negative low-rank factorization, with rows of both factors on the probability simplex. Its non-Bayesian ancestor, pLSA, fits as free parameters; LDA puts a prior on them, which controls overfitting and gives a principled way to handle a new document.
Note what the model ignores. Given and , the tokens of a document are independent and identically distributed, so reordering them changes nothing.
Dirichlet priors as pseudo-counts
The Dirichlet distribution came up in the topic Dirichlet processes. On the simplex its density is
with a scalar means for all . Note the reuse of letters: in the previous topic was the DP concentration, and in Bayesian linear regression was a noise precision; here both are Dirichlet parameters. A symmetric Dirichlet has mean whatever is, so controls the shape around the mean:
- : the density grows towards the corners and edges of the simplex; documents use few topics.
- : the density is uniform on the simplex.
- : the density peaks at the centre; documents mix all topics evenly.
plays the same role for topics: a small makes each topic concentrate on few words.
The Dirichlet is conjugate to the categorical distribution (see Conjugate priors and the exponential family). If of the tokens of document are assigned to topic , the likelihood is , and
So is a pseudo-count: as if every topic had already been seen times in every document. A document with counts gets the posterior mean with , with , and with : the larger , the harder the estimate is pulled towards uniform.
The hyperparameters matter in practice: on synthetic data with known topics, an off by a factor of ten can hide the true topics altogether. Popular defaults are with , or ; they can also be learned by evidence maximization, as in Bayesian model selection.
Integrating out proportions and topics
We want the posterior over all the latent variables, . Its normalizer sums over every assignment of topics to tokens: terms for tokens. The tiny corpus below has ; a real one has millions of tokens. As with the Gaussian mixtures of EM and Bayesian PCA, the exact computation is hopeless, and we approximate.
Conjugacy lets us integrate out the continuous variables exactly. For one document,
and the same calculation for each topic gives the collapsed joint
where counts the tokens of each word assigned to topic , and . Only counts remain.
The ratio of functions has a sequential reading. Integrating out, the predictive probability that the next token of document gets topic is the posterior mean from the previous block, , and the probability of a whole sequence of assignments is a product of such predictive steps, each with the counts updated by the steps before. Take a document with assignments and :
Each token of a topic makes the next one more likely, like drawing balls from a Pólya urn and returning two of the same colour.
Now the conditional for one token. Let token be word in document , and let a superscript mean "counts of all tokens except ". The conditional is the product of two predictive probabilities, one for the topic within the document and one for the word within the topic:
Collapsed Gibbs sampling
The first denominator is the same for every topic, so the sampler uses
This is collapsed Gibbs sampling (Griffiths and Steyvers, 2004): Gibbs sampling, as in the topic Gibbs sampling and Metropolis–Hastings, on the collapsed model where only is left. The first factor says "topics popular in this document", the second "topics in which this word is common". A sweep visits every token in turn: remove it from the counts, compute weights, draw a topic, put the token back under the new topic.
A tiny corpus with , , , ; each word is followed by its current topic:
| Document | Tokens (topic) |
|---|---|
| 1 | team (1), goal (1), team (1), goal (1), market (2) |
| 2 | market (2), price (2), price (2), goal (1) |
| 3 | goal (1), team (1), price (2) |
The count tables are for the documents; topic 1 holds team 3, goal 4 (), topic 2 holds market 2, price 3 ().
Update the token goal in document 2. Without it, document 2 has ; topic 1 has goals out of tokens, topic 2 has out of . The weights:
Topic 1 wins with probability : the document leans to economics, but "goal" has never been seen there, and the word factor is stronger.
After burn-in the counts give point estimates, again the Dirichlet posterior means:
They are usually averaged over several well-separated samples of one chain.
Mean-field variational inference
The deterministic alternative is mean-field variational inference (Blei, Ng and Jordan, 2003), with the family
Here and are not integrated out but approximated. Each factor follows from the coordinate update of the topic Variational inference, . The term of with is , and averaging it gives
where is the digamma function, the derivative of ; has the same form with . The Dirichlet factors are conjugate updates with expected counts in place of counts:
The algorithm alternates a local step (for each document, iterate and with the topics fixed) and a global step (recompute from all documents).
Note that is not . For the mean is , while gives .
Compared with collapsed Gibbs, mean field is deterministic, usually needs fewer passes and reports an ELBO to monitor. It is also biased by its family: the factorized ignores dependencies and underestimates posterior variance. The Gibbs sampler is asymptotically exact but noisy, and its convergence is harder to judge.
Where LDA breaks and what comes next
A model is usually judged by held-out perplexity,
the effective number of equally likely words the model hesitates between: a uniform model over words has perplexity . Since is itself intractable, it is estimated, for example by document completion: infer from half of a held-out document and score the other half. Lower is better, but likelihood does not always agree with human judgement, so topics are also scored by coherence: how often their top words occur together in documents.
Where LDA breaks:
- Label switching. The posterior is symmetric in the topic labels, so topic 3 in one run may be topic 7 in another. Compare runs only after matching topics, e.g. with the Hungarian algorithm.
- Local optima. Some restarts merge two themes into one topic and split another; run several restarts and keep the best ELBO or likelihood.
- Choosing . Training fit almost always improves with ; held-out perplexity and coherence often disagree. The hierarchical Dirichlet process infers the number of topics, at the price of a harder sampler.
- Bag of words. Order and negation are invisible, and frequent stop words such as "the" and "of" dominate topics unless they are removed.
The last problem is cost. One Gibbs sweep and one mean-field iteration both touch every token of the corpus, and in mean field the topics are recomputed only after the local steps of all documents. With millions of documents, each improvement of the topics waits for a full pass over the data. The topic Stochastic variational inference fixes exactly this: it treats the global step as a natural-gradient step on the ELBO, estimated from a small minibatch of documents.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.