Variational Autoencoding Topic Models
Topic models based on Variational Autoencoding are generative models based on ProdLDA (citation) enhanced with contextual representations.
You will also hear people refer to these models as CTMs or Contextualized Topic Models. This is confusing, as technically all of the models in Turftopic are contextualized, but most of them do not use autoencoding variational inference. We will therefore stick to calling these models Autoencoding topic models.
You will need to install Turftopic with Pyro to be able to use these models:
pip install turftopic[pyro-ppl]
The Model
Autoencoding Topic Models are generative models over word content in documents, similarly to classical generative topic models, such as Latent Dirichlet Allocation. This means that we have a probabilistic description of how words in documents are generated based on latent representations (topic proportions).
Where these models differ from LDA is that they:
- Use a Logistic Normal distribution for topic proportions instead of a Dirichlet.
- Words in a document are determined by a product of experts, rather than drawing a topic label for each word in a document.
- Use Amortized Variational Inference: A mapping between parameters of the topic proportions and input representations is learned by an artificial neural network (encoder network), instead of sampling the posterior.
Note that term importance estimation is built into the model, instead of
Depending on what the input of the encoder network is, we are either talking about a ZeroShotTM or a CombinedTM. ZeroShotTM(default) only uses the contextual embeddings as the input, while CombinedTM concatenates these to Bag-of-Words representations.
You can choose either, by modifying the combined
parameter of the model:
from turftopic import AutoEncodingTopicModel
zeroshot_tm = AutoEncodingTopicModel(10, combined=False)
combined_tm = AutoEncodingTopicModel(10, combined=True)
Comparison with the CTM Package
The main difference is in the implementation. CTM implements inference from scratch in Torch, whereas Turftopic uses a 3rd party inference engine (and probabilistic programming language) called Pyro. This has a number of implications, most notably:
- Default hyperparameters are different, as such you might get different results with the two packages.
- Turftopic's inference is more stable, and is less likely to fail due to issues with numerical stability. This is simply because Pyro is a very well tested and widely used engine, and is a more reliable choice than writing inference by hand.
- Inference in CTM might be faster, as it uses a specific implementation that does not need to be universal in opposition to Pyro.
Turftopic, similarly to Clustering models might not contain some model specific utilites, that CTM boasts.
API Reference
turftopic.models.ctm.AutoEncodingTopicModel
Bases: ContextualModel
Variational autoencoding topic models with contextualized representations (CTM). Uses amortized variational inference with neural networks to estimate posterior for ProdLDA.
from turftopic import AutoEncodingTopicModel
corpus: list[str] = ["some text", "more text", ...]
model = AutoEncodingTopicModel(10, combined=False).fit(corpus)
model.print_topics()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_components
|
int
|
Number of topics. |
required |
encoder
|
Union[Encoder, SentenceTransformer]
|
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
|
combined
|
bool
|
Indicates whether encoder inputs should be combined with bow representations. When False the model is equivalent to ZeroShotTM, when True it is CombinedTM. |
False
|
dropout_rate
|
float
|
Dropout in the encoder layers. |
0.1
|
hidden
|
int
|
Size of hidden layers in the encoder network. |
100
|
batch_size
|
int
|
Batch size when training the network. |
42
|
learning_rate
|
float
|
Learning rate for the optimizer. |
0.01
|
n_epochs
|
int
|
Number of epochs to run during training. |
50
|
random_state
|
Optional[int]
|
Random state to use so that results are exactly reproducible. |
None
|
Source code in turftopic/models/ctm.py
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
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/ctm.py
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 |
|