Paper commentary
BERTopic: Neural topic modeling with a class-based TF-IDF procedure
BERTopic builds topics from clusters using class-based TF-IDF word weights
- Problem Bag-of-words models like LDA and NMF ignore word context. Centroid-based clustering picks topic words that mislead when clusters are not spherical.
- Method BERTopic embeds documents with a pre-trained language model, clusters them with HDBSCAN, and weights words per cluster with class-based TF-IDF instead of centroid distance.
- Results Reached .066 topic coherence on Trump's tweets, the highest of the Table 1 models, with NPMI averaged over 10 to 50 topics.
Why centroid-based word selection falls short
LDA and NMF describe each document as a bag of words, so they disregard semantic relations between words and the context in which a word appears. A bag-of-words input can therefore misrepresent a document, because it does not capture what a word means within its sentence. Contextual embeddings from BERT and related models place texts with similar meaning close together in vector space, which made them attractive for topic modeling.
Earlier clustering-based approaches take topic words from the embedding space. Sia et al. (2020) pick words that lie near a cluster's centroid, and Top2Vec does something similar on jointly learned Doc2Vec vectors while finding its clusters with HDBSCAN. That centroid assumption fails when a cluster does not sit inside a sphere around its centroid, so the nearest words can be misleading.
Sia et al. re-rank candidates by their frequency in the cluster, but the candidates still come from the centroid view.
BERTopic keeps the clustering idea and changes how topic words are chosen: instead of measuring closeness to a centroid, it weights words with a class-based TF-IDF computed per cluster. The method is organized as three separate steps, so each one can be changed without redesigning the others.
From documents to clusters
Each document is first embedded with a pre-trained language model. BERTopic does this through the Sentence-BERT framework, which maps sentences and paragraphs to dense vectors. The experiments use the all-mpnet-base-v2 model as the embedding model for BERTopic and CTM. Any other embedding model can be used in this step if it was fine-tuned on semantic similarity, and the authors argue that clustering quality should follow improvements in embedding models.
The embeddings are then reduced with UMAP before clustering, because distances to the nearest and farthest points converge as dimensionality grows. UMAP was chosen over PCA and t-SNE because it keeps more of the local and global structure, and since it places no computational restriction on the embedding dimension, it works across language models of different sizes.
Allaoui et al. (2020) report that UMAP reduction improves k-Means and HDBSCAN in both clustering accuracy and time.
HDBSCAN then clusters the reduced vectors. It extends DBSCAN to clusters of varying density and can model points as noise, so unrelated documents are not forced into any cluster. Each cluster is assigned one topic, and the documents in that cluster form the input for extracting topic words.
Because the topic-word step depends only on the clustering output, its settings can change without re-clustering; for example, the n-gram length of the topic words can be increased, or stopwords can be removed only for the topic representations. The authors link this separation to the stability of BERTopic across embedding models.
Weighting words per cluster
The classic TF-IDF weight of term t in document d multiplies the term frequency by the logarithm of the number of documents N divided by the number of documents that contain t: \( W_{t,d} = tf_{t,d} \cdot \log(N / df_t) \). The inverse document frequency part gives more weight to terms that appear in fewer documents, so a term that is rare across the corpus scores higher in a given document.
BERTopic moves the unit from the document to the cluster. All documents in a cluster are concatenated into one class c, and the weighting is then computed over classes. Adding one inside the logarithm keeps its argument above one, so the weight stays positive.
The result is a word-weight distribution for each cluster, and these distributions serve as the topic representations. To reach a user-specified number of topics, the c-TF-IDF representation of the least common topic is merged into that of its most similar topic, and the merging repeats until the target is met.
Local topics and smoothing over time
Dynamic topic modeling tracks how topics change over time in sequentially organized documents. Blei and Lafferty (2006) introduced it as an extension of LDA. BERTopic treats a topic as one global object whose word weights may still differ by period. The authors' example contrasts car-related documents from 1990 and 2020, where 'Tesla'-style terms appear only in the later period.
BERTopic is first fitted on the full corpus without temporal information, which gives global IDF values. Each timestep i then receives local weights by multiplying the term frequency of its documents by those global values: \( W_{t,c,i} = tf_{t,c,i} \cdot \log\left(1 + \frac{A}{tf_t}\right) \). Because the local weights reuse the global values, no new embedding or clustering is needed, which makes the computation fast.
The same idea can also use metadata such as author or journal.
Without smoothing, the representation at timestep t does not depend on timestep t-1, so the topics need not evolve linearly. Smoothing divides each topic's c-TF-IDF vector at each timestep by its L1-norm and then averages the normalized vectors at t and t-1. The normalization keeps the size of a topic's documents from dominating the comparison.
Smoothing is optional, because topics do not always evolve linearly.
Datasets, baselines and metrics
The 20 NewsGroups dataset has 16309 news articles across 20 categories, and the BBC News dataset has 2225 documents from between 2004 and 2005. Both were preprocessed thoroughly: punctuation and stopwords were removed, lemmatization was applied, and documents with fewer than 5 words were dropped. Trump's tweets, 44253 tweets excluding re-tweets from 2009 to 2021, received only light preprocessing so that the datasets differ in form.
For the dynamic experiments, Trump's tweets are binned into 10 timesteps and the UN general debate transcriptions into 9. The HDBSCAN and UMAP parameters were kept fixed across BERTopic and Top2Vec so that their comparison is fair. BERTopic was run with four language models: the Universal Sentence Encoder, Doc2Vec, all-MiniLM-L6-v2 and all-mpnet-base-v2.
The dynamic version is compared with LDA Sequence.
Topic coherence is measured with NPMI, which ranges from -1 to 1, and topic diversity is the share of unique words across all topics, ranging from 0 to 1. The authors note that NPMI may track human judgment for classical models but may not do so for neural topic models, so the scores give only an indication of performance.
All models ran on a machine with 2 cores of an Intel Xeon CPU at 2.00GHz and a Tesla P100 GPU.
Static comparison against baselines
Table 1 averages each score over 15 runs, from 10 to 50 topics with 3 runs per step. On Trump's tweets, BERTopic-MPNET reaches topic coherence .066, the highest value among the models in that table. The other models score -.011 for LDA, .009 for NMF, .009 for CTM, -.213 for Top2Vec with MPNET and -.169 for Top2Vec with Doc2Vec on the same dataset.
On Trump's tweets, however, its topic diversity of .663 is below the .855 of CTM.
On 20 NewsGroups, BERTopic-MPNET reaches .166 topic coherence and .851 topic diversity, while Top2Vec with Doc2Vec is higher on coherence at .192. On BBC News, Top2Vec with Doc2Vec scores .171 coherence against .167 for BERTopic-MPNET.
CTM's topic diversity is higher than BERTopic-MPNET's on all three datasets: .886 against .851 on 20 NewsGroups, .819 against .794 on BBC News and .855 against .663 on Trump's tweets. The high diversity of CTM matches earlier results that used a different diversity measure (Bianchi et al., 2020a).
Sensitivity to the embedding model
Table 2 reports BERTopic with the Universal Sentence Encoder, Doc2Vec, MiniLM and MPNET under the same protocol. On 20 NewsGroups, topic coherence stays between .149 for the Universal Sentence Encoder and .173 for Doc2Vec. The results are read as stability across language models, and MiniLM is suggested as the faster choice when GPU capacity is limited.
Doc2Vec is the weakest variant on Trump's tweets, with coherence -.088 and diversity .536, the lowest of the four variants on both measures. This points to Doc2Vec struggling to represent the Trump dataset, a weakness that also shows for Top2Vec with Doc2Vec in Table 1 at -.169 coherence.
Top2Vec with Doc2Vec is competitive on coherence for 20 NewsGroups, at .192, while Top2Vec with MPNET scores .068 on the same dataset and -.027 on BBC News. Coherence and diversity are reported to drop on all datasets when MPNET replaces Doc2Vec. In Table 1, however, the Trump diversity of Top2Vec with MPNET (.698) is higher than with Doc2Vec (.658).
Because BERTopic separates embedding from topic-word construction, it stays competitive whichever embedding model is used.
Dynamic evaluation against LDA Sequence
On Trump's tweets, BERTopic reaches topic coherence .079 and topic diversity .862, against .009 and .715 for LDA Sequence. Dynamic scores are averaged over timesteps and 3 runs, with NPMI computed at 50 topics for each timestep.
On the UN datasets, BERTopic reaches .231 coherence against .173 for LDA Sequence, but its diversity of .779 is below the .820 of LDA Sequence. The authors therefore describe BERTopic as leading only on coherence for the UN data.
BERTopic-Evolve, the smoothed variant, reaches .079 coherence and .863 diversity on Trump's tweets, and .226 coherence and .769 diversity on the UN datasets. These values are close to those of BERTopic without smoothing, so the linear-evolution assumption changes the measured scores little on these datasets.
Wall time on growing Trump subsets
Wall time was measured on subsets of Trump's tweets ranging from 1000 to 43000 documents in steps of 2000. CTM with MPNET is slow compared with all other models, so it is left out of the comparison of the remaining models.
NMF and LDA are faster than the neural topic modeling techniques. BERTopic and Top2Vec take similar time when they use the same language model, and BERTopic with MiniLM runs at a speed similar to BERTopic with Doc2Vec.
The embeddings were created on a GPU, and the authors expect wall time to increase substantially when documents are embedded without one. Doc2Vec can serve as the language model without a GPU, but its coherence and diversity stability was questioned by the earlier experiments.
Limitations
BERTopic assigns each document to a single topic, which does not reflect documents that cover several topics. HDBSCAN's probability matrix can serve as a proxy for the topic distribution of a document, but training still assumes one topic per document. Topic words are built from bags-of-words, so they can be related to one another and redundant; maximal marginal relevance was suggested for the top words but was not explored.
Coherence and diversity are proxies for a subjective judgment, so they indicate model performance only approximately. Experiments cover three datasets for the static comparison, and the evaluation uses only NPMI and topic diversity. The Table 3 caption refers to 9 timesteps for each dataset, while the setup gives 10 timesteps for Trump's tweets.
No timing without a GPU is reported, so the wall-time results apply to the GPU setting only.
Paper info
- Authors
- Maarten Grootendorst
- Venue
- arXiv · Preprint
- First published
- 2022-3-11
- Added
- 2026-11-12
- Updated
- 2022-3-11
- Version
- v1
- arXiv
- 2203.05794
- Category
- cs.CL
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Maarten Grootendorst (2022). BERTopic: Neural topic modeling with a class-based TF-IDF procedure. arXiv:2203.05794v1. https://arxiv.org/abs/2203.05794v1
BibTeX
@misc{grootendorst2022bertopic,
title = {{BERTopic: Neural topic modeling with a class-based TF-IDF procedure}},
author = {Maarten Grootendorst},
year = {2022},
eprint = {2203.05794},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2203.05794v1}
}