By now it has become very clear that Latent Dirichlet Allocation (LDA) has a variety of valuable, real-world use cases. However, most real-world use cases involve large volumes of data which can be problematic for LDA. This is because both of the traditional implementations of LDA (variational inference and collapsed Gibbs sampling) require the entire corpus (or some encoding of it) to be loaded into main memory. Obviously, if you are working with a single machine and a data set that is sufficiently large, this can be infeasible. One solution is to parallelize the algorithm and scale out until you have the required resources. However, this presents an entire new set of problems - acquiring a cluster of machines, modifying your LDA code such that it can work in a MapReduce framework, etc. A much better solution would be to segment your large data set into small batches and sequentially read each of these batches into main memory and update your LDA model as you go in an online fashion. This way you are only keeping a small fraction of your large data set in main memory at any given time. Furthermore, consider a scenario where your corpus is constantly growing such as an online discussion forum. As your corpus grows you want to see how the topics are changing. With traditional variational inference you would have to rerun the entire batch algorithm with the old data and the new data but it would be much more efficient to simply update your model with only the new data. In their paper Online Learning for Latent Dirichlet Allocation, Blei et al. present an algorithm for achieving this kind of functionality. This blog post aims to give a summary of this paper and also show some results from my own Scala implementation.
Variational Inference vs Stochastic Variational Inference
Let’s start off with a very general graphical model that includes observations, local hidden variables, and global hidden variables.
In the above graphical model, there are observations and one local hidden variable for each observation . There are also global hidden variables with a known prior . I will first compare variation inference with stochastic variational inference in the context of this graphical model because it can be generalized to a variety of different graphical models with local and global hidden variables e.g. the LDA model, Gaussian mixture models, hidden Markov models and many more.
The joint distribution of this graphical model is
Also, our assumption that are the global parameters and are the local parameters is formalized by the fact that the observation and the corresponding local variable are conditionally independent given the global variables i.e.
Another assumption that we make in this model is that the conditional distributions of the hidden variables given the observations and other hidden variables (also called the complete conditionals) are in the exponential family which means they take the general form
Where is called the base measure, is called the log-normalizer, is called the natural parameter, and is called the sufficient statistics. Furthermore, we also assume that the prior distribution over is part of the exponential family.
The main goal is to compute the posterior distribution of the hidden variables given the observed variables
Unfortunately, the denominator is intractable to compute so we must use approximate inference techniques such as variational inference.
Variational inference turns the inference problem into an optimization problem. A new distribution over the hidden variables (called the variational distribution) is introduced. This new distribution has properties such that it can be efficiently computed. The variational distribution is a function of a set of free parameters that are optimized such that the variational distribution is as close as possible to the actual target posterior distribution where closeness is measured in terms of KL divergence. Minimizing the KL divergence between the variational distribution and the target posterior is equivalent to maximizing the evidence lower bound (ELBO) (proof not shown here) which is
As stated earlier, the variational distribution has the property that it can be efficiently computed. This is done by making each hidden variable independent of each other.
Furthermore, each hidden variable is governed by its own variational parameter so the variational parameters governs the global variables and the variational parameters govern the local variables . and take the same form as the complete conditionals and , but the natural parameters are now and , respectively to give
We maximize the ELBO objective function with a coordinate ascent procedure. We find its gradient with respect to the global variational parameter and find the value of that sets the gradient to zero. We do the same thing for the local parameters . We iterate between these updates until we converge to the maximum of the ELBO. The updates are given without proof, but the general procedure is to write the ELBO in terms of parameter of interest (either or ) then take the gradient and set it to zero.
Therefore the updates of each variational parameter are equal to the expected value of the natural parameters of the complete conditionals. The complete coordinate ascent algorithm is given below.
As you can see, in the local parameter update (steps 3 and 4), we have to iterate over every data point in the data set which is computationally expensive and (as we will see later) not necessary.
Stochastic Variational Inference
Stochastic variational inference uses a stochastic optimization technique to sequentially maximize the ELBO using unbiased samples from the data set. Updates are performed with the following formula
where is a noisy (but unbiased) gradient of the objective function obtained from a subsample of the data set. If the step size satisfies the following constraints
then it is guaranteed to converge to the global optimum if the objective function is convex, or a local optima if it is not convex. Now let’s look at how this noisy gradient can be computed for a single data point. First we write the ELBO in terms of a global term and a sum of local terms.
Consider a randomly chosen data point index sampled from . For this data point let us define
This is equivalent to the original ELBO if the entire data set was made up of . There are two important facts that one must understand about
The expectation of with respect to the data point is equivalent to the original ELBO. As a consequence, the gradient of can be thought of as a noisy gradient of the original ELBO because it is unbiased. However, we do not want to take the usual gradient of . Instead we want to take the natural gradient. The usual gradient assumes that the parameter space is Euclidean but it turns out that it is better to assume that it has a Riemannian metric structure (in the context of minimizing KL divergence) which is what the natural gradient does. A full explanation of the natural gradient is beyond the scope of this blog post but this paper by Amari gives a good overview. The The natural gradient of is
and setting this gradient to zero gives the update
The full algorithm is shown below
The procedure consists of sampling a single data point then finding the optimal local parameters for that data point then updating the global variational parameters under the assumption that the entire dataset consisted of replicas of that data point. Then this “intermediate” global variational parameter is combined with the previous “overall” global parameter via a weighted average to produce a new “overall” global parameter. In this way, the global parameters can be updated after each sample is seen, rather than once after each iteration over the entire data set as in traditional variational inference (however, in pactice “mini-batches” of data points are used rather than a single point). Now let’s finally see how we can apply stochastic variational inference to Latent Dirichlet Allocation to get a scalable online learning algorithm.
Stochastic Variational Inference and LDA
Topic models aim to uncover the hidden thematic coherent topics that exist in a corpus of documents. The most popular topic model is Latent Dirichlet Allocation (LDA). In LDA, documents are thought of as distributions over topics and the topics themselves are distributions over words. The graphical model for LDA can be thought of as a special case of the general graphical model shown earlier. In the case of LDA, the global parameters are the topic distributions which all documents depend on and the local parameters are the document-topic proportions which are independent between documents and the topic assignments for each word in the document. So, in this context, “local” refers to document-specific variables and “global” refers to corpus-specific variables. The observed variables are the words that appear in each document (in bag-of-words format). LDA is explained in greater detail in my previous blog post on the subject but here is the graphical model as a reminder.
In terms of notation, let’s assume there are unique words in the vocabulary, documents in the corpus, and topics. The next step is defining the complete conditionals for the LDA model (i.e. the distributions of each variable given all of the other variables both hidden and observed). The complete conditionals for the local topic assignments , the local topic proportions , and global topic distributions are
where is the document index in the corpus, is the word index in the vocabulary, and is the topic index. As you can see, the complete conditionals of the local variables only depend on other local variables from the same local context (i.e. the same document) and the global variables, they do not depend on local variables from other documents. As per mean-field variational inference, the variational distributions for these variables take the same form as their complete conditionals, that is
Next we find the updates for each of these variational parameters by taking the expectation of the natural parameters of the complete conditionals which are
Now let’s use the procedure mapped out in the stochastic variational inference algorithm which is to first randomly sample a document from the corpus then update the local variational parameters (the topic assignments for each word) and (the topic proportions for the document) for this document. Then we update the variational parameters for the global topic distribution for that sampled document . Then we merge the global parameter for the sampled document with the overall global parameter (with an update weighted by ). We repeat this procedure until we think that convergence has occurred. This procedure is better illustrated below.
Scala code that implements stochastic variational inference for LDA can be found in this github repo (warning! experimental so use at your own risk). In this experiment, we are using the NIPS dataset which consists of the abstracts of 1,736 NIPS papers however this algorithm could handle a much larger data set than this. The following Scala code shows how we run this experiment.
//set location of directory containing data val docDirectory = "NIPS_dataset/" //create vocabulary from data with minimum frequency cutoff of 10 val testVocab = CountVocab(docDirectory, 10).getVocabulary //create a corpus object that can be streamed into online LDA model val testCorpus = new StreamingCorpus(testVocab, 5, docDirectory) //create online LDA model object with 5 topics, a decay of 0.5 and 1736 documents val oldaTest = new OnlineLDA(testCorpus, 5, 0.5, 1736) //learn the model oldaTest.inference() //show the topics by displaying the 10 most probable words in each topic oldaTest.printTopics(10)
The resulting topics are shown below
Topic 1: state, learning, reinforcement, policy, action, control, actions, states, controller, robot Topic 2: recognition, speech, training, word, classifier, classification, classifiers, tree, words, hmm Topic 3: cells, neurons, cell, neuron, visual, response, figure, synaptic, model, activity Topic 4: network, learning, model, neural, data, networks, input, set, figure Topic 5: disparity, binding, similarity, protein, structural, clause, instruction, energy, structure, spin
If you have some background knowledge in the machine learning domain you can see that these five topics are both distinct and thematically coherent. The topics appear to describe five different fields of machine learning. Topic 1 seems to describe reinforcement learning, Topic 2 seems to describe speech recognition, Topic 3 seems to describe neuroscience in general, Topic 4 seems to describe neural networks, and finally Topic 5 seems to describe the field of bioinformatics. Some of the topics contain redundant words but this can be reduced by preprocessing the vocabulary (eg. word stemming).
- In traditional variational inference for LDA (and variational inference in general) we must iterate through the entire data set before we can update the global parameters. This is slow and memory intensive. It also turns out that it is unnecessary because the entire dataset contains redundant information - instead we can iteratively update our global parameters based on small samples from the data set. This is much less memory intensive.
- In terms of LDA, this means that we can iteratively update our model by learning sequentially from small mini-batches of documents taken from the corpus. This means that at any given time, we only need to keep a small mini-batch of documents in memory which means that we can scale our LDA model to an arbitrarily large corpus!
- Take a look at my experimental Scala code that implements this.