So in this lecture, we are going to apply our knowledge of Markov models to build a text classifier,
basically, now that we've done all this theory, it's time to put that theory to a real world application.
So let's start by asking the question What is a text classifier?
A text classifier is a model that will take as input a string of text and output a prediction about
the category it belongs to.
So, for example, suppose that I've trained the model to understand poems by Robert Frost and Edgar
Allan Poe.
I will then input some new text and I will be able to get a prediction about who authored that text.
Now, this might seem like an example which is not useful in the real world, but that's only because
you may have forgotten about my rule.
All data is the same because of this rule.
The same method can be applied to any problem with the same structure.
So, for example, I could train a model to predict whether or not an email is spam.
I could train a model to predict whether a movie review has positive or negative sentiment.
These are two of the most popular applications of text classifiers, and this would be using the exact
same code.
Now, I want to make note that the goal of this example is not to build the best and most accurate text
classifier.
The goal of this example is to exercise and to practice what we have learned.
If our goal is accuracy, then we might simply use a pre-trained a deep neural network.
Of course, plugging our data into one line of code does not count as an exercise that would be like
watching people on TV to exercise instead of doing exercise yourself.
So as you go through this exercise, be mindful of what its purposes and try your hardest to see the
big picture of how everything fits together.
OK, so let's recognize one fact.
Text classification as an example of supervised learning, but Markov models are unsupervised.
This is because we do not use labels.
Our training data consists only of sequences of text.
So how can we make use of markup models in a supervised context?
The answer is Bayes rule.
In fact, what we are really building in this lecture is a Bayes classifier.
The basic idea is this suppose that I have two sets of poems.
Poems by Robert Frost and poems by Edgar Allan Poe.
Now suppose that I train at two separate Markov models for each poet, so each of these will have their
own distinct matrices and PI vectors.
As you recall, one task we can perform when we have a Markov model is to figure out the probability
of a sequence given a pot.
So suppose I have a poem whose author is unknown?
I would like to know which author wrote this poem.
Well, what do I get when I compute the probability of this poem under the different Aizen pies?
The answer is I get pie of poem given author equals Robert Frost and Poem Given Author equals Edgar
Allan Poe.
The conditioning here arises from the fact that each model was trained on data by a specific author.
Now, what I would really like to know is the distribution, which is kind of opposite to what we have.
Specifically, we have of palm given author, as I recall what we would like to know this piece of author
given Palm.
If I have this distribution, then my prediction for which author wrote the poem would simply be the
one with the highest probability.
In general, I will choose the class case star, which is the AMAX of PE of class equals K given the
input x over all classes K.
So now that we understand what distribution we want, let's think about how we can find this distribution.
In fact, this is just Bay's rule.
Once again, specifically of author given poem is equal to a poem given author times b of Arthur divided
by P of Palm.
So this is the basic form of what we need, but there are several ways we can simplify this expression
on the right.
Firstly, note that all we want is the AMAX puV poem does not depend on any specific author, and therefore
it is constant with respect to author.
Therefore, it simply does not need to be considered.
We only need to consider the numerator.
When we take the AMAX, we will get the same answer, whether we include this term or not.
Furthermore, as you recall, with Markov models, we often like to work with log probabilities.
Remember that since the log is a monotonically increasing function, it does not change the answer as
to which is the ARG Max.
And since the numerator only involves multiplication, it's easy to take a log of all the terms.
So what we end up with is that we would like the ARG mass of log of palm giving author plus log p of
author.
And again, recall that log p of palm given author is just the log probability of a markup model, which
we know how to compute.
One further simplification we can make occurs when the prior piece of author is uniform, for example,
given no other information, perhaps there's no reason why Robert Frost or Edgar Allan Poe would be
more likely.
Thus, both would have a 50 percent chance of being chosen.
If this is the case, then Pivot author is also constant, and it can also be removed from the AMAX.
In this case, our decision comes down to choosing the AMAX of Poem Given author.
Of course, this is just the likelihood of once again, and so we call this method maximum likelihood.
But note that this is a special case that can only be used if we assume that the prior is uniform.
OK, so let's recap what we've discussed in this lecture.
We discovered that we can use markup models for text classification by creating a separate Markov model
for each class.
The Markov model can be used to compute the likelihood of X given class equals K for each Class K.
In order to come up with a decision rule, we must apply Bayes rule.
We would like the ARG max of P of class equals K given the input x over all classes k, this is our
prediction, which we will call K star.
The next step is to realize that this posterior probability can be simplified since we don't actually
need to compute it and we only want the ARG Max specifically, we can reduce this decision rule to the
log of the likelihood plus the log of the prior.
By the way, we call this the maximum a posteriori method or map.
Furthermore, we noted that if the prior is uniform, that is, all classes have an equal chance of
being chosen, then our problem reduces to maximum likelihood.