Sub topic 4
Dirichlet priors: alpha and eta
This chapter covers the two hyperparameters that shape every LDA model. I assume you know the LDA generative story from the previous chapter, can read a categorical distribution, and have run gensim once. Here you learn what alpha and eta mean, how they change the topics you get, and how to set them.
Why LDA needs priors
LDA draws two kinds of probability vectors:
- For each document
d, a document-topic mixturetheta_d. It has one entry per topic and sums to 1. - For each topic
k, a topic-word distributionphi_k. It has one entry per vocabulary word and sums to 1.
Both vectors come from a Dirichlet distribution. A Dirichlet is a distribution over probability vectors. A sample from it is a list of non-negative numbers that sums to 1. It has a concentration parameter that controls how evenly it spreads probability mass.
alphais the concentration of the Dirichlet that generatestheta_d. It has lengthK, the number of topics.etais the concentration of the Dirichlet that generatesphi_k. It has lengthV, the vocabulary size. The literature usually calls itbeta.gensimcalls iteta. scikit-learn calls the twodoc_topic_priorandtopic_word_prior.
How concentration changes samples
When all entries share one value, the Dirichlet is symmetric. The value then decides how peaked the samples are:
| Concentration | Behaviour | Effect in LDA |
|---|---|---|
| Below 1 | Samples are sparse. A few entries get most of the mass. | `alpha`: documents use few topics. `eta`: topics use few words. |
| Equal to 1 | Every probability vector is equally likely. | No preference for sparse or smooth. |
| Above 1 | Samples are close to uniform. | `alpha`: documents mix many topics. `eta`: topics spread over many words. |
You can see this directly by sampling with NumPy:
import numpy as np
rng = np.random.default_rng(0)
for alpha in [0.1, 1.0, 10.0]:
theta = rng.dirichlet([alpha] * 5, size=3)
print(f"alpha={alpha}")
print(np.round(theta, 2))Each printed row is one sampled mixture over five topics. At alpha=0.1 most rows have one dominant entry and several values near zero. At alpha=10.0 every entry is close to 0.2.
Choosing alpha
alpha encodes your belief about how many topics one document covers.
- Small alpha (for example 0.01 to 0.1) suits short, focused documents such as news headlines or tweets.
- Large alpha suits long documents that really do mix many themes.
- Alpha near 1/K is the usual default. As
Kgrows, the default shrinks, so mixtures stay sparse.
alpha can also be asymmetric, with a different value per topic. Topics with a larger alpha entry are used more often across the corpus. This lets a few broad topics, such as a general "news" topic, absorb common material instead of splitting it across every topic.
Choosing eta
eta encodes how concentrated each topic is on a few words.
- Small eta (for example 0.01) pushes each topic toward a short list of characteristic words. Topics are sharper and easier to label, but a rare word can dominate a topic.
- Large eta smooths topics so that most vocabulary words get non-trivial probability. Top words become generic and overlap between topics.
Because eta has V entries, a scalar value spreads a total pseudo-count of eta * V over the vocabulary. A large vocabulary therefore makes a given eta weaker per word. Keep that in mind when you change the vocabulary size.
Setting priors in gensim
LdaModel accepts these forms:
| Argument | Accepted values | Meaning |
|---|---|---|
| `alpha` | `"symmetric"` (default), `"asymmetric"`, `"auto"`, a float, or an array of length `num_topics` | `"symmetric"` uses `1/num_topics`. `"asymmetric"` uses a fixed decreasing vector. `"auto"` learns the vector from the data. |
| `eta` | `None` (default), `"symmetric"`, `"auto"`, a float, an array of length `num_terms`, or a `num_topics` by `num_terms` matrix | `None` and `"symmetric"` use `1/num_topics`. `"auto"` learns a value per word. A matrix gives per-topic word priors, which you can use to seed topics. |
The next script fits LDA with three values of alpha. For each document it records the largest topic probability, then averages those values. A higher average means more peaked mixtures.
from gensim import corpora
from gensim.models import LdaModel
docs = [
["goal", "match", "player", "team", "season"],
["election", "vote", "party", "government", "policy"],
["team", "coach", "league", "season", "win"],
["minister", "government", "vote", "law", "policy"],
]
dictionary = corpora.Dictionary(docs)
corpus = [dictionary.doc2bow(doc) for doc in docs]
for alpha in [0.05, 1.0, 10.0]:
lda = LdaModel(
corpus=corpus,
id2word=dictionary,
num_topics=4,
alpha=alpha,
eta=0.1,
passes=20,
random_state=42,
)
peaks = [
max(p for _, p in lda.get_document_topics(bow, minimum_probability=0.0))
for bow in corpus
]
print(f"alpha={alpha}: mean peak topic probability = {sum(peaks) / len(peaks):.3f}")Expect the mean peak to fall as alpha rises. This corpus has four documents, so the exact numbers are noisy. Use a real corpus to judge the effect.
Learning the priors from data
With "auto", gensim updates the priors during training by maximising the likelihood of the corpus.
from gensim import corpora
from gensim.models import LdaModel
docs = [
["goal", "match", "player", "team", "season"],
["election", "vote", "party", "government", "policy"],
["team", "coach", "league", "season", "win"],
["minister", "government", "vote", "law", "policy"],
]
dictionary = corpora.Dictionary(docs)
corpus = [dictionary.doc2bow(doc) for doc in docs]
lda = LdaModel(
corpus=corpus,
id2word=dictionary,
num_topics=2,
alpha="auto",
eta="auto",
passes=20,
random_state=42,
)
print("learned alpha:", lda.alpha)
print("eta shape:", lda.eta.shape)
print(lda.get_document_topics(corpus[0], minimum_probability=0.0))lda.alpha is a vector with one value per topic, so "auto" produces an asymmetric prior. lda.eta has one value per vocabulary word.
Common errors
- Tuning alpha to fix a bad K. If topics are merged or split badly, change
num_topicsfirst. Priors change how mass is distributed. They do not change how many themes the data contains. - Forgetting that the default depends on K. Going from 10 to 100 topics divides the default
alphaby ten. Mixtures become sparser without any change in your code. - Reading "auto" as "optimal". It maximises corpus likelihood, which does not always give the topics people find most coherent. Compare against the coherence scores from a later chapter.
- Trusting learned priors on tiny corpora. With a handful of documents the estimates are unstable and can collapse to extreme values.
- Mixing up names. A paper's
betaisetaingensim. Ingensim,LdaModelalso has a separatedecayparameter, which is unrelated.
A practical workflow
- Fix preprocessing and vocabulary first, because both change what
etameans per word. - Fit with defaults and inspect topics and example documents.
- If documents look too mixed, lower
alpha. If topics look vague, lowereta. - Try
"auto"and compare the learned values with your manual choices. - Keep a fixed
random_statewhile comparing settings, so differences come from the prior and not from random initialisation.
Exercise
You model 280-character posts, each about one subject, with 20 topics. Topics come out with clear top words, but many posts show three or four topics with similar probabilities. Which prior do you change, in which direction, and why?
Show answer
Lower alpha, for example from the default 1/20 = 0.05 to 0.01. The mixtures are too spread out, and alpha controls how many topics a document uses. A smaller alpha makes each post's topic mixture sparse, so one topic dominates. Eta is not the cause, because the topics already have clear top words.
Exercise
You switch from num_topics=10 to num_topics=100 and leave alpha at the default "symmetric". What happens to the default alpha value, and how does that affect the document mixtures?
Show answer
The default is 1/num_topics, so it falls from 0.1 to 0.01. Smaller alpha favours sparse mixtures, so each document concentrates on fewer topics than before.