Latent Dirichlet Allocation (LDA) in Python.
This module allows both LDA model estimation from a training corpus and inference of topic distribution on new, unseen documents. The model can also be updated with new documents for online training.
The core estimation code is based on the onlineldavb.py script by M. Hoffman , see Hoffman, Blei, Bach: Online Learning for Latent Dirichlet Allocation, NIPS 2010.
The constructor estimates Latent Dirichlet Allocation model parameters based on a training corpus:
>>> lda = LdaModel(corpus, num_topics=10)
You can then infer topic distributions on new, unseen documents, with
>>> doc_lda = lda[doc_bow]
The model can be updated (trained) with new documents via
Model persistency is achieved through its load/save methods.
If given, start training from the iterable corpus straight away. If not given, the model is left untrained (presumably because you want to call update() manually).
num_topics is the number of requested latent topics to be extracted from the training corpus.
id2word is a mapping from word ids (integers) to words (strings). It is used to determine the vocabulary size, as well as for debugging and topic printing.
alpha and eta are hyperparameters that affect sparsity of the document-topic (theta) and topic-word (lambda) distributions. Both default to a symmetric 1.0/num_topics prior.
alpha can be also set to an explicit array = prior of your choice. It also support special values of ‘asymmetric’ and ‘auto’: the former uses a fixed normalized asymmetric 1.0/topicno prior, the latter learns an asymmetric prior directly from your data.
Turn on distributed to force distributed computing (see the web tutorial on how to set up a cluster of machines for gensim).
Calculate and log perplexity estimate from the latest mini-batch every eval_every model updates (setting this to 1 slows down training ~2x; default is 10 for better performance). Set to None to disable perplexity estimation.
>>> lda = LdaModel(corpus, num_topics=100) # train model >>> print(lda[doc_bow]) # get topic probability distribution for a document >>> lda.update(corpus2) # update the LDA model with additional documents >>> print(lda[doc_bow])
>>> lda = LdaModel(corpus, num_topics=50, alpha='auto', eval_every=5) # train asymmetric alpha from data
Estimate the variational bound of documents from corpus: E_q[log p(corpus)] - E_q[log q(corpus)]
gamma are the variational parameters on topic weights for each corpus document (=2d matrix=what comes out of inference()). If not supplied, will be inferred from the model.
Clear model state (free up some memory). Used in the distributed algo.
Perform inference on a chunk of documents, and accumulate the collected sufficient statistics in state (or self.state if None).
M step: use linear interpolation between the existing topics and collected sufficient statistics in other to update the topics.
Given a chunk of sparse document vectors, estimate gamma (parameters controlling the topic weights) for each document in the chunk.
This function does not modify the model (=is read-only aka const). The whole input chunk of document is assumed to fit in RAM; chunking of a large corpus must be done earlier in the pipeline.
If collect_sstats is True, also collect sufficient statistics needed to update the model’s topic-word distributions, and return a 2-tuple (gamma, sstats). Otherwise, return (gamma, None). gamma is of shape len(chunk) x topics.
Load a previously saved object from file (also see save).
Large arrays are mmap’ed back as read-only (shared memory).
Save the model to file.
Large internal arrays may be stored into separate files, with fname as prefix.
Print the topN most probable words for topics number of topics. Set topics=-1 to print all topics.
Unlike LSA, there is no ordering between the topics in LDA. The printed topics <= self.num_topics subset of all topics is therefore arbitrary and may change between two runs.
Set formatted=True to return the topics as a list of strings, or False as lists of (weight, word) pairs.
Train the model with new documents, by EM-iterating over corpus until the topics converge (or until the maximum number of allowed iterations is reached). corpus must be an iterable (repeatable stream of documents),
In distributed mode, the E step is distributed over a cluster of machines.
This update also supports updating an already trained model (self) with new documents from corpus; the two models are then merged in proportion to the number of old vs. new documents. This feature is still experimental for non-stationary input streams.
For stationary input (no topic drift in new documents), on the other hand, this equals the online update of Hoffman et al. and is guaranteed to converge for any decay in (0.5, 1.0>.
Update parameters for the Dirichlet prior on the per-document topic weights alpha given the last gammat.
Uses Newton’s method: http://www.stanford.edu/~jhuang11/research/dirichlet/dirichlet.pdf
Encapsulate information for distributed computation of LdaModel objects.
Objects of this class are sent over the network, so try to keep them lean to reduce traffic.
Given LdaState other, merge it with the current state. Stretch both to targetsize documents before merging, so that they are of comparable magnitude.
Merging is done by average weighting: in the extremes, rhot=0.0 means other is completely ignored; rhot=1.0 means self is completely ignored.
This procedure corresponds to the stochastic gradient update from Hoffman et al., algorithm 2 (eq. 14).
Alternative, more simple blend.
Load a previously saved object from file (also see save).
If the object was saved with large arrays stored separately, you can load these arrays via mmap (shared memory) using mmap=’r’. Default: don’t use mmap, load large arrays as normal objects.
Merge the result of an E step from one node with that of another node (summing up sufficient statistics).
The merging is trivial and after merging all cluster nodes, we have the exact same result as if the computation was run on a single node (no approximation).
Prepare the state for a new EM iteration (reset sufficient stats).
Save the object to file (also see load).
If separately is None, automatically detect large numpy/scipy.sparse arrays in the object being stored, and store them into separate files. This avoids pickle memory errors and allows mmap’ing large arrays back on load efficiently.
You can also set separately manually, in which case it must be a list of attribute names to be stored in separate files. The automatic check is not performed in this case.
ignore is a set of attribute names to not serialize (file handles, caches etc). On subsequent load() these attributes will be set to None.
For a vector theta~Dir(alpha), compute E[log(theta)].