FASTopic
FASTopic (Wu et al., 2024) is a neural topic model based on Dual Semantic-relation Reconstruction.
Figure from Wu et al. (2024)
FASTopic, instead of reconstructing Bag-of-words, like classical topic models or VAE-based models do, reconstructs the relations between topics words and documents.
Wu et al. (2025) express semantic relations for this model using the Embedding Transport Plan (ETP) method.
The model uses a combined loss function that helps the model learn semantic relations between topic and word embeddings, and learn to reconstruct these relations.
Usage
from turftopic import FASTopic
documents = [...]
model = FASTopic(10)
doc_topic_matrix = model.fit_transform(documents)
model.print_topics()
Citation
Please cite the authors of the paper, and Turftopic when using the FASTopic model:
@inproceedings{
wu2024fastopic,
title={{FAST}opic: Pretrained Transformer is a Fast, Adaptive, Stable, and Transferable Topic Model},
author={Xiaobao Wu and Thong Thanh Nguyen and Delvin Ce Zhang and William Yang Wang and Anh Tuan Luu},
booktitle={The Thirty-eighth Annual Conference on Neural Information Processing Systems},
year={2024},
url={https://openreview.net/forum?id=7t6aq0Fa9D}
}
@article{
Kardos2025,
title = {Turftopic: Topic Modelling with Contextual Representations from Sentence Transformers},
doi = {10.21105/joss.08183},
url = {https://doi.org/10.21105/joss.08183},
year = {2025},
publisher = {The Open Journal},
volume = {10},
number = {111},
pages = {8183},
author = {Kardos, Márton and Enevoldsen, Kenneth C. and Kostkan, Jan and Kristensen-McLachlan, Ross Deans and Rocca, Roberta},
journal = {Journal of Open Source Software}
}
API Reference
turftopic.models.fastopic.FASTopic
Bases: ContextualModel
Implementation of the FASTopic model with a Turftopic API. The implementation is based on the original FASTopic package, but is adapted for optimal use in Turftopic (you can pre-compute embeddings for instance).
You will need to install torch to use this model.
pip install turftopic[torch]
## OR:
pip install turftopic[pyro-ppl]
from turftopic import FASTopic
corpus: list[str] = ["some text", "more text", ...]
model = FASTopic(10).fit(corpus)
model.print_topics()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components |
int
|
Number of topics. If you're using priors on the weight, feel free to overshoot with this value. |
required |
encoder |
Union[Encoder, str]
|
Model to encode documents/terms, all-MiniLM-L6-v2 is the default. |
'sentence-transformers/all-MiniLM-L6-v2'
|
vectorizer |
Optional[CountVectorizer]
|
Vectorizer used for term extraction. Can be used to prune or filter the vocabulary. |
None
|
random_state |
Optional[int]
|
Random state to use so that results are exactly reproducible. |
None
|
DT_alpha |
float
|
Sinkhorn alpha between document embeddings and topic embeddings. |
3.0
|
TW_alpha |
float
|
Sinkhorn alpha between topic embeddings and word embeddings. |
2.0
|
theta_temp |
float
|
Temperature parameter of used in softmax to compute topic probabilities in documents. |
1.0
|
n_epochs |
int
|
Number of epochs to train the model for. |
200
|
learning_rate |
float
|
Learning rate for the ADAM optimizer. |
0.002
|
device |
str
|
Device to run the model on. Defaults to CPU. |
'cpu'
|
Source code in turftopic/models/fastopic.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
transform(raw_documents, embeddings=None)
Infers topic importances for new documents based on a fitted model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_documents |
Documents to fit the model on. |
required | |
embeddings |
Optional[ndarray]
|
Precomputed document encodings. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray of shape (n_dimensions, n_topics)
|
Document-topic matrix. |
Source code in turftopic/models/fastopic.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |