Text Classification

In many applications we need to automatically classify some input text with respect to a set of classes or labels. For example,

  • for information retrieval it is useful to classify documents into a set of topics, such as "sport" or "business",
  • for sentiment analysis we classify tweets into being "positive" or "negative" and
  • for Spam Filters we need to distinguish between Ham and Spam.

Text Classification as Structured Prediction

We can formalize text classification as the simplest instance of structured prediction where the input space X are sequences of words, and the output space Y is a set of labels such as Y={sports,business}. On a high level, our goal is to define a model a model sθ(x,y) that assigns high scores to the label y that fits the text x, and lower scores otherwise. The model will be parametrized by θ, and these parameters we will learn from some training set Dtrain of (x,y) pairs. When we need to classify a text x we have to solve the trivial (if the number of classes is low) maximization problem argmaxysθ(x,y).

In the following we will present two typical approaches to text classifiers: Naive Bayes and discriminative linear classifiers. We will also see that both in fact can use the same model structure, and differ only in how model parameters are trained.

Naive Bayes

One of the most widely used approaches to text classification relies on the so-called Naive Bayes (NB) Model. In NB we use a distribution pθNB for sθ. In particular, we use the a posteriori probability of a label y given the input text x as a score for that label given the text.

(1)sθ(x,y) =pθNB(y|x)

By Bayes Law we get

(2)pθNB(y|x)=pθNB(x|y)pθNB(y)pθNB(x)

and when an input x is fixed we can focus on

(3)pθNB(x,y)=pθNB(x|y)pθNB(y)

because in this case pθNB(x) is a constant factor. In the above pθNB(x|y) is the likelihood, and pθNB(y) is the prior.

The "naivity" of NB stems from a certain conditional independence assumption we make for the likelihood pθNB(x|y). Note that conditional independence of two events a and b given a third event c requires that p(a,b|c)=p(a|c)p(b|c). In particular, for the likelihood in NB we have:

(4)pθNB(x|y)=ilength(x)pθNB(xi|y)

That is, NB makes the assumption that the observed wors are independent of each other when conditioned on the label y.

Parametrization

The NB model has the parameters θ=(α,β) where

pθNB(f|y)=αf,ypθNB(y)=βy.

That is, α captures the per-class feature weights, and β the class priors.

Training the Naive Bayes Model

The NB model again can be trained using Maximum Likelihood estimation. This amounts to setting

αx,y=#Dtrain(x,y)#Dtrain(y)βy=#Dtrain(y)|Dtrain|

Log-linear Representation

It will be convenient to represent the NB model in log-linear form. This form will allow us to understand the MLE NB simply as one particular way of training a (log) linear model. It will also make the approach comparable to other approaches such as Conditional Log-Likelihood (aka logistic regression or maximum entropy) or SVM style training that can operate on the same parametrisation but estimate the parameters using different objectives. Finally, the log-linear representation will make it easy to implement different training algorithms that work with the same representation and hence can be easily plugged-in and out.

In log-linear form the joint NB distribution pθNB(x,y) can be written as:

(5)pθNB(x,y)=exp(iIfi(x,y)wi)=exp<f(x,y),w>

Here the fi are so called (joint) feature functions. The index set I is the union of all labels yY and all word-label pairs (x,y), and the corresponding feature functions are defined as follows:

fy(x,y)=δ(y,y)fx,y(x,y)=δ(y,y)ilength(x)δ(x,xi)

In words, the first feature function fy returns 1 if the input label y equals y, and 0 otherwise. The second feature function returns the number of times the word x appears in x if y equals y, and 0 otherwise.

If one now sets the weights according to

wy=logβywx,y=logαx,y

it is easy to show that 5 is equivalent to the original NB formulation in equation 3.

Feature Templates

Note that in the above case we have |Y| label features, and |V|×|Y| word-label features. The corresponding two types of features are often called feature templates that generate sets of actual feature functions. That is, fy is a feature template with one template argument, y, and fxy is a template with two arguments. It is common to augment the feature templates with a template name to distinguish templates that have the same argument space but different semantics. For example, we may have a template that combines the label of a document with number of times word stems have been seen. For words x that are their own stem this would create duplicate indices, and hence we use fword,x,y and fstem,x,y to distinguish both types of features.

Conditional Model

The conditional probability pθNB(y|x) can be written as

pθNB(y|x)=exp<f(x,y),w>yexp<f(x,y),w>=exp(sθ(x,y)Aθ,x)

where

sθ(x,y)=<f(x,y),w>Aθ,x=yexp<f(x,y),w>

and Aθ,x is the log-partition function.

Joint vs Input Features

In contrast to the standard formulation of linear and log-linear models we define feature functions over input-output pairs instead of only inputs. That is, we could have also used a representation where <f(x,y),w> is replaced by <f(x),wy> in which each class y receives an own weight vector.

The benefit of the joint feature function is two-fold. First it enables us to easily define features that break up the output label into sub-labels. That is, say you have the labels "sports_baseball" and "sports_football", then you can define one feature that tests whether a label starts with a certain prefix ("sports"). This allows the model to learn commonalities between both labels. Second, the one-weight vector per class approach breaks down when the output space is structured (and exponentially sized). You simply cannot maintain a different weight vector for each possible output structure y, both for computational and statistical reasons.

Conditional Loglikelihood

Training the NB model using MLE is efficient and easy to implement. In many cases it also leads to good results. However, one can argue that the MLE objective is generally not the optimal choice when the task is to predict the best output given some input. Let us recall the MLE objective:

L(Dtrain,θ)=(x,y)DtrainlogpθNB(x,y)

Via Bayes Law we can reformulate this objective as follows:

L(Dtrain,θ)=(x,y)DtrainlogpθNB(y|x)+logpθNB(x)

Notice that the pθNB(x) here is not the class prior as used in the forward definition of the NB model, pθNB(x|y)pθNB(y). Instead it is the marginal probability ypθNB(x,y) of seeing a given input text x.

This view on the MLE objective shows that for every training instance MLE means both maximizing the conditional probability of seeing y given x, and maximizing the marginal probability of x. Now consider the way we use the NB model to make a prediction for a given x: we search for the label y with maximum conditional probability pθNB(y|x). Since x is fixed, the marginal probability pθNB(x) has no impact on this decision. This means part of what what we optimize during training is completely irrelevant at test time.

The above problem may lead to errors: for a given training instance we may increase the marginal probability of x at the price of reducing pθNB(y|x) if this means we can still increase their product. This may lead to the true y having smaller conditional probability than wrong ones.

One way to overcome this problem is to directly optimize the conditional log-likelihood (CL). This objective is defined as follows:

CL(Dtrain,θ)=(x,y)DtrainlogpθNB(y|x)=(x,y)Dtrainsθ(x,y)Aθ,x

This objective directly aims to maximize the condional class probability given the inputs, ignoring the marginal probability of x.

Optimizing Conditional Loglikelihood

Due to the normalization factor Aθ,x there is no closed-form solution to the CL problem, and compared to the MLE solution we cannot just count. Instead we need to iteratively optimize the objective. One popular method to optimize the CL objective is via gradient ascent, or its stochastic version, stochastic gradient ascent Often one uses the term gradient descent even when ascent is meant, and we then speak of stochastic gradient descent (SGD).

The underlying idea of such gradient-based methods is to iteratively move along the gradient of the function until this gradient disappears. That is, for a function f(θ) to be optimized, in each step i a gradient ascent algorithm performs the following update to the parameter vector θ:

θiθi1+αif(θi1)

There are various ways of choosing the learning rate α dynamically depending on the iteration i, but for simplicity we will set it to 1 in the following.

To apply gradient ascent methods to optimizing the conditional loglikelihood we need to find its gradient with respect to the parameters. This gradient has a very intuitive form:

CL(θ)=(x,y)Dtrainf(x,y)Epθ(y|x)[f(x,y)]

That is, for each training instance this gradient moves towards the feature representation f(x,y) of the gold solution, and away from the expectation of the feature representation under the current model Epθ(y|x)[f(x,y)].

In particular, this gradient is zero (and hence at an optimal solution) when the empirical expectation of the feature function under the training set is identical to the (conditional) model expectation. This gives rise to a dual view of the conditional likelihood objective: we can see solutions to this problem as parameters that force the empirical and model moments to match. In fact, one can arrive at the log-linear formulation and the CL objective also by searching for any distribution that matches the given feature moments and has maximal entropy. Particularly in the context of text classification the CL based approach is therefore often referred to as Maximum Entropy approach. On many text classification datasets it can be shown to outperform the MLE approach substantially.

Note that the CL objective is strictly concave. This means that when the gradient becomes zero we have found the single optimum to this function.