**Building a Q&A machine learning model, for answering White House Press Briefing questions about Brexit, using Latent Dirichlet Allocation.**

## Introduction

There is a lot of fuss about life-like chat AI these days. The truth is that we are still not yet there and it will likely be a while before the technology exists. Fundamentally a life-like chat AI, that is genuinely life-like, would be able to pass the Turing test, which would be a massive milestone in AI development.

As the technology does not yet exist, it is hard to say precisely what is missing to make a life-like chat AI possible. It is likely that a life-like chat AI would need 3 main components: language understanding, long-term contextual awareness, and a general knowledge-base. Each of these parts is on their own somewhat possible today, but the technology to combine them doesn’t yet exist. For example, language understanding allows us to create a translation AI, which in theory could be used to “translate” from questions to answers. However, the amount of different questions one can ask makes it hard to embed that amount of information in a neural network. Hence, we likely need an external knowledge-base that it can access, and that connection hasn’t been made efficiently yet. Another example is that we can teach AIs to play Go and Chess which requires long-term contextual awareness. However, it is only possible to do with reinforcement learning which heavily relies on training with the help of a simulation system. Chats are not something we can efficiently simulate, because the rules are not explicitly known.

Not all aspects of a chat AI are out of reach today. In this article it is shown how to implement a Q&A “AI”, using a machine learning technique for natural language processing.

A Q&A “AI” can be useful in many cases, such as aiding help-desk support. The base technology also extends to smarter and more flexible searching through documents, or even discovering categories in your documents that you weren’t aware of. If your users produce your documents, this could reveal user groups that you weren’t aware you had.

### Brexit Questions and Answers in the White House

In this example, we use the press briefings Q&A sessions from the White House during the Obama Presidency. They are in the public domain documents and are available here: https://obamawhitehouse.archives.gov/briefing-room/press-briefings.

We build a semantic model from the press briefing questions. This means the “AI” understands the political language typically used in those press briefing sessions. To ensure we’re not providing outdated or offensive answers, we create a curated list of mostly Brexit questions. This means that only questions related to Brexit can be answered, but you can still add answers to questions about other subjects.

Please follow the walk-through guide below and feel free play around with training it using your own questions and answers to see how well the “AI” understands them on a general level. All the evaluation is done locally on your machine using TensorFlow.js, so don’t worry about sending us inappropriate questions.

## Semantic Search Machine

The core concept of semantic search is to have an algorithm that can represent a document of words as a mixture of topics. If there are 3 topics, this could be 20% of topic 1, 80% of topic 2, and 0% of topic 3.

The intrusion here is that a specific topic is more likely to contain some words compared to others. The classic example of this is that a topic about cats is more likely to contain words such as milk, meow, and kitten. While a topic about dogs is more likely to contain words such as puppy, bark, and bone. However dogs are also mammals, and cat also has an endoskeleton. Therefore, a document about dogs could also contain the word milk. Likewise, a document about cats can also contain the word bone. It is just less likely. In the end, the topic mixture of a document should be the mixture that is most likely.

The exact algorithm of how this topic mixture is identified is explained later. For now, think of it as a black-box algorithm.

### Similarity measure

To compute the similarity between two documents, where one is the question asked by the user and the other being a reference question with a predefined answer, the cosine similarity measure is used. Consider, $q$ being the topic representation for the question, while $r$ is the topic representation for the reference question. Then the cosine similarity can be computed as$s=qrq∙r $

The cosine similarity is $1$ if the topic mixture is the same. Likewise, it is $−1$ if the topics are the opposite.

### Indexing

The cosine similarity is rather cheap to compute, but even so, it does need to be recomputed for all query-reference question pairs. A simple solution to speed this up, is not to store $r$ but rather $rr $. The same should be done for the query question. This way, only the dot product $q~ ∙r~$ needs to be computed, and this can typically be computed very fast on a GPU, even an integrated GPU is usually sufficient for this.

This linear time search algorithm is usually sufficient for even a reasonably sized Q&A database. If not, there are easy ways to turn the cosine similarity into a proper distance metric which enables logarithmic time search algorithms.

## LDA as a Generative Model

The easiest way to understand how Latent Dirichlet Allocation identifies topics in documents, is to ask the opposite question, “given a set of topics what document can one expect to see?”.

Given a model that can answer this question, one can then flip the model to answer the following questions, “given a document which of the defined topics are most likely to generate that document?”.

Finally, with the answer to that question. One can solve the problem of, “given a corpus of documents, what kinds of topics are most likely?”.

### The LDA parameters

The Latent Dirichlet Allocation consists of two groups of parameters:

- $α_{t}$: Controls the likelihood of the topic $t$ being used.
- $β_{t,w}$: Controls the likelihood of the word $w$ being used in topic $t$.

### Topic composition

The first step is to use the $α_{t}$ parameters to select a composition of topics randomly. Let us say there are three topics, a “composition of topics” could then be: 20% probability of topic $1$, 80% probability of topic $2$, and 0% probability of topic $3$.

Such a composition of topics, or probabilities to be more general, can be sampled using a Dirichlet distribution. Intuitively, the Dirichlet distribution uses the $α_{t}$ parameters, that must all be greater than $0$, the following way:

- The higher the $α_{t}$ is, the higher the probability for topic $t$ is.
- If the $α_{t}$ parameters are less than $1$. Then only a few topics are selected, and the rest gets assigned 0% probability.

For 3 topics, the “composition of topics” can be visualised as points on a triangle. If the point is close to $1$ then there is a high probability of topic $1$. Similarly, if a point is a very far way the probability is small. Consequently, this means that if a point is on the edge then at least one of the topics gets 0% probability. This

In Latent Dirichlet Allocation, the $α_{t}$ should be close to zero, because often a document is only composed of a small set of topics. With the $α_{t}$ close to zeros, topics often get 0% probability of being selected when generating a document. Although, other documents may be composed of a different set of topics.

### Selecting words

With a known topic composition for the document. The next step is to generate the words one by one, this is done by repeating the following three steps:

- Using a known topic composition for the given document a specific topic is sampled uniform randomly.
- Once a specific topic $t$ is sampled, the word probabilities for that topic are also sampled, again using a Dirichlet distribution with the parameters $β_{t,w}$. It is essential to not sample new word probabilities for the same topic for than once, as this ruins the meaning of a topic.
- Finally, once the word probabilities for the topic $t$ are known, a specific word is sampled uniformly from the word probabilities.

### The generative algorithm

In mathematical terms, the algorithm can be expressed as:

- Choose document length $N$.
- Sample topic composition: $θ∼Dir(α)$.
- For each word $n$, in a document with $N$ words in total:
- Sample a topic: $t_{n}∼Cat(θ)$.
- Sample the word composition: $ϕ_{tn}∼Dir(β_{tn})$.
- Sample a word: $w_{n}∼Cat(ϕ_{tn})$.

This algorithm completes the answer to “given a set of topics what document can one expect to see?”. As promised from this answer, one can derive math to answer the questions “given a document which of the defined topics are most likely to generate that document?” and “given a corpus of documents, what kinds of topics are most likely?”.

## Digging Into the Math

This section is where things get tough. So feel free to skip this. Deriving these results is not incredibly complex, but involves advanced mathematical concepts such as solving a constrained optimisation problem with some probability theory mixed in. For this reason, only the highlights are covered here, for more thorough explaining please see the original Latent Dirichlet Allocation paper.

### Notation and the generative distribution

Continuing with the generative model as described before. The topic composition distribution is denoted $p(θ∣α)$. The sampling of the topic for word $n$ is denoted $p(z_{n}∣θ)$. Finally, the distribution of the word given the topic is denoted $p(w_{n}∣z_{n},β)$. Note here that the distribution $p(w_{n}∣z_{n},β)$ is not explicitly split up into the sampling of the word composition and the word itself, as this is not directly relevant for the LDA model. All this gives the following generative distribution:$p(θ,z,w∣α,β)=p(θ∣α)n=∏N p(z_{n}∣θ)p(w_{n}∣z_{n},β)$

### Inference of the topics

To infer the topics gives a document Bayes theorem is used:$p(θ,z∣w,α,β)=p(wα,β)p(θ,z,wα,β) $However, computing this distribution is computationally infeasible as the $p(w∣α,β)$ becomes:$p(w∣α,β)=∏(α)(∑α) ∫(k∏ θ_{k})(n=∏N k=∑K c=∏V (θ_{k}β_{k,w}_{wnc})∂θ$

The primary reason why this is impossible to simplify to something more computationally feasible is that $z$ depends on both $θ$ and $β$. And simultaneously $θ$ depends on $α$.

The trick to solving this problem is to create an approximation to the desired distribution $p(θ,z,w∣α,β)$, where these dependencies don’t exist. That distribution looks like this:$q(θ,z∣γ,ϕ)=q(θ∣γ)n=∏N q(z_{n}∣ϕ_{n})$In this distribution $γ$ is related to $α$ and $ϕ$ is related to $θ$. Note that $θ$ itself also appears in the distribution however that is as an output variable. The purpose of $ϕ$ is to remove the coupling where $θ$ would otherwise be used both as an output variable and an input variable.

In the end, $γ$ is the most relevant output. As this informs us of the Dirichlet parameters, that were most likely to generate a topic composition that generated the input document. Hence, it will indirectly, but directly enough, inform us what topics the document are composed of.

To find the $γ$ and $ϕ$ parameters, the parameters will be optimized such that the $q(θ,z∣γ,ϕ)$ distribution matches the $p(θ,z∣w,α,β)$ the best.

One metric for the difference between two random distributions is the Kullback–Leibler divergence. The goal is then to minimise this difference.$(γ_{∗},ϕ_{∗})=argγ,ϕmin D_{KL}(q(θ,z∣γ,ϕ)∣∣p(θ,z∣w,α,β))$Solving this optimisation problem is where it gets tough. Thus that won’t be discussed here. Instead, see Appendix A.3 in original Latent Dirichlet Allocation paper for the details.

The highlight of this, is that the $γ$ and $ϕ$ parameters can be found using the following algorithm:

- Initialize $ϕ_{n,k}=K1 $
- Initialize $γ_{k}=α_{k}+KN $
- repeat until convergence of $γ$:
- for each word at position $n$:
- for each topic $k$: $ϕ_{n,k}=β_{k,wn}exp(Ψ(γ_{k}))$
- normalize: $ϕ_{n}$

- for each topic $k$: $γ_{k}=α_{k}+∑_{n=}ϕ_{n,k}$

- for each word at position $n$:

### Parameter estimation

The inference part of the LDA is by far the most complex part. Once this algorithm is implemented it is fairly straight forward to optimize the $β$ parameters. The $α$ parameter is a Dirichlet prior and is thus a hyper parameter that doesn’t need optimization. The original paper do suggests a Newton-Raphson approach for optimizing $α$, but in practice this is not necessary.

Optimizing $β$ is done with the following EM-algorithm:

**E-step:** Compute $γ_{d}$ and $ϕ_{d}$ for each document $d$, as described in the previous section.

**M-step:** Update $β$ with:$β_{k,c}∝d=∑D n=∑N ϕ_{d,n,k}w_{d,n}$

## Conclusion

While Latent Dirichlet Allocation is not a very new approach to language understanding, it is powerful when the Q&A dataset isn’t massive. The White House press briefings contain about 60,000 questions and answers. Such size may sound like a lot, but a modern translation AI that would use neural networks would typically depend on millions of translated sentences. Because Latent Dirichlet Allocation is built on strong mathematical assumptions about how topics and documents relate, it can achieve much more than a neural network would be able to do when the dataset is small. On the other hand, these mathematical assumptions are also what limits it. Therefore, if the dataset were much more massive, a neural network would typically be superior.

In the end, machine learning is not always about reaching for some ultra-advanced neural network but instead about achieving meaningful results given the data and resources that are available.

## Contact

If you think your business could benefit from a Q&A solution like this, or something entirely different, then please feel free to contact us at https://www.staging1.nearform.com/contact/.

Image: palesa