So far, we covered Markov Chains. Now, we’ll dive into more complex models: Hidden Markov Models.
Hidden Markov Models (HMM) are widely used for :
- speech recognition
- writing recognition
- object or face detection
- part-of-speech tagging and other NLP tasks…
I recommend checking the introduction made by Luis Serrano on HMM on YouTube
We will be focusing on Part-of-Speech (PoS) tagging. Part-of-speech tagging is the process by which we can tag a given word as being a noun, pronoun, verb, adverb…
PoS can, for example, be used for Text to Speech conversion or Word sense disambiguation.
In this specific case, the same word
bear has completely different meanings, and the corresponding PoS is therefore different.
Let’s consider the following scenario. In your office, 2 colleagues talk a lot. You know they either talk about Work or Holidays. Since they look cool, you’d like to join them. But you’re too far to understand the whole conversation, and you only get some words of the sentence
Before joining the conversation, in order not to sound too weird, you’d like to guess whether he talks about Work or Holidays. For example, here is the kind of sentence your friends might be pronouncing :
You only hear distinctively the words python or bear, and try to guess the context of the sentence. Since your friends are Python developers, when they talk about work, they talk about Python 80% of the time.
These probabilities are called the Emission probabilities.
You listen to their conversations and keep trying to understand the subject every minute. There is some sort of coherence in the conversation of your friends. Indeed, if one hour they talk about work, there is a lower probability that the next minute they talk about holidays.
We can define what we call the Hidden Markov Model for this situation :
The probabilities to change the topic of the conversation or not are called the transition probabilities.
- The words you understand are called the observations since you observe them. Logic.
- The subject they talk about is called the hidden state since you can’t observe it
Discrete Hidden Markov Models
An HMM is a sequence made of a combination of 2 stochastic processes :
- an observed one : , here the words
- a hidden one : , here the topic of the conversation. This is called the state of the process.
A HMM model is defined by :
- the vector of initial probabilities , where
- a transition matrix for unobserved sequence :
- a matrix of the probabilities of the observations
What are the main hypothesis behind HMMs ?
- independence of the observations conditionally to the hidden states :
- the stationary Markov Chain :
- Joint probability for a sequence of observations and states :
An HMM is a subcase of Bayesian Networks.
How can we find the transition probabilities?
They are based on the observations we have made. We can suppose that after carefully listening, every minute, we manage to understand the topic they were talking about. This does not give us the full information on the topic they are currently talking about though.
You have 15 observations, taken over the last 15 minutes, W denotes Work and H Holidays.
We notice that in 2 cases out of 5, the topic Work lead to the topic Holidays, which explains the transition probability in the graph above.
How can we find the emission probabilities?
Well, since we have observations on the topic they were discussing, and we observe the words that were used during the discussion, we can define estimates of the emission probabilities :
What is the probability for each topic at a random minute?
Suppose that you have to grab a coffee, and when you come back, they are still talking. You have no clue what they are talking about! What is at that random moment the probability that they are talking about Work or Holidays?
We can count from the previous observations: 10 times they were talking about Holidays, 5 times about Work. Therefore, it states that we have chance that they talk about Work, and chance that they talk about Holidays.
If you hear the word “Python”, what is the probability of each topic?
If you hear the word “Python”, the probability that the topic is Work or Holidays is defined by Bayes Theorem!
We can replace the probabilities :
Which heads to 57%.
If you hear a sequence of words, what is the probability of each topic?
Let’s start with 2 observations in a row. Let’s suppose that we hear the words “Python” and “Bear” in a row. What are the possible combinations?
- Python was linked to Work, Bear was linked to work
- Python was linked to Holidays, Bear was linked to work
- Python was linked to Holidays, Bear was linked to Holidays
- Python was linked to Work, Bear was linked to Holidays
These scenarios can be summarized this way :
Therefore, the most likely hidden states are Holidays and Holidays. What if you hear more than 2 words? Let’s say 50? It becomes challenging to compute all the possible paths! This is why the Viterbi Algorithm was introduced, to overcome this issue.
Decoding with Viterbi Algorithm
The main idea behind the Viterbi Algorithm is that when we compute the optimal decoding sequence, we don’t keep all the potential paths, but only the path corresponding to the maximum likelihood.
Here’s how it works. We start with a sequence of observed events, say
Python, Python, Python, Bear, Bear, Python. This sequence corresponds simply to a sequence of observations : .
For the first observation, the probability that the subject is Work given that we observe Python is the probability that it is Work times the probability that it is Python given that it is Work.
The most likely sequence of states simply corresponds to :
We can then move on to the next day. Here’s what will happen :
For each position, we compute the probability using the fact that the previous topic was either Work or Holidays, and for each case, we only keep the maximum since we aim to find the maximum likelihood. Therefore, the next step is to estimate the same thing for the Holidays topic and keep the maximum between the 2 paths.
If you decode the whole sequence, you should get something similar to this (I’ve rounded the values, so you might get slightly different results) :
The most likely sequence when we observe
Python, Python, Python, Bear, Bear, Python is, therefore
Work, Work, Work, Holidays, Holidays, Holidays.
If you finally go talk to your colleagues after such a long stalking time, you should expect them to be talking about holidays :)
Let’s go a little deeper in the Viterbi Algorithm and formulate it properly.
The joint probability of the best sequence of potential states ending in-state at time and corresponding to observations is denoted by . This is one of the potential paths described above.
By recursion, it can be shown that :
Where denotes a probability of the matrix of observations and denotes a value of the transition matrix for unobserved sequence. Those parameters are estimated from the sequence of observations and states available. The is simply the maximum we take at each step when moving forward.
I won’t go into further details here. You should simply remember that there are 2 ways to solve Viterbi, forward (as we have seen) and backward.
When we only observe partially the sequence and face incomplete data, the EM algorithm is used.
Generating a sequence
As we have seen with Markov Chains, we can generate sequences with HMMs. In order to do so, we need to :
- generate first the hidden state then , e.g Work then Python
- then generate the transition to
- then from , generate
How does the process work? As stated above, this is now a 2 step process, where we first generate the state, then the observation.
Conclusion : I hope this was clear enough! HMMs are interesting topics, so don’t hesitate to drop a comment!
Like it? Buy me a coffee