Given a sequence of observations, many machine learning tasks require us to label each observation in the sequence with a corresponding class (or named entity) such that the overall likelihood of the labelling is maximized. For example, given a english sentence, i.e. a sequence of words, label each word with a Part-Of-Speech tag, such that the combined POS tag of the sentence is optimum.
"Machine Learning is a field of Artificial Intelligence"
- The probability of a sequence of labels, e.g. what is the probability of the following sequence of POS tags for the above sentence [NNP, NNP, VBZ, DT, VBZ, IN, NNP, NNP] ?
- The best possible sequence of labelings, that maximizes the probability of the overall prediction.
To compute the joint probability of a sequence of labels with the sequence of observation , by the application of the chain rule in probability :
Assuming that each observation is independent of other observations and only dependent on its label and each label is only dependent on the previous label, we can write the above equation as :
The conditional probability of the labeling , is given as :
The denominator is just the product of the relative frequency of each observation in the training data, i.e.
, since the observations are assumed to be independent of each other.
The quantities are called the emission probabilities and are the transition probabilities and these are determined from the training data.
Coming to the second part, that is to determine the best sequence of labels, that maximizes the joint probability of the labels and the observations, one can enumerate all possible sequence of labels and then choose the one with the highest joint probability.
Assuming there are M different possible labels and the length of the observation is n, then we need to evaluate possible sequence of labels in order to choose the best among them. But since the label for a particular observation is only dependent on the label of the previous observation, we can use a dynamic programming approach to solve this in polynomial time . This approach is known as the Viterbi algorithm.
Whereas HMM is a generative algorithm, Conditional Random Fields (CRF) are discriminative algorithms for same kind of tasks. Two advantages of CRF over HMM is that, in CRF :
- We do not assume that each observation is independent of other observations. This is very important as some words such as "is" might be very frequent after the word "this" or "are" might frequently occur after "we" or "these" and so on. HMM makes errors in this estimation as it assumes that each word is equally likely to be followed by another word.
- There could be arbitrary dependencies between labels unlike HMM where each label is dependent only on the previous label.
- The features for a particular word can depend on any other subset of words in the sequence. HMM cannot do this due to the independence assumption.
- In the presence of hidden states, the training objective in HMM uses the EM algorithm, which finds local minima/maxima, but the training objective in CRF is concave and has a globally optimum solution. But unlike HMM, CRF cannot work with hidden states i.e. without labels, it is purely a supervised algorithm.
CRF can also be used to compute the above two quantities for a given sequence labelling problem similar to HMM. Only difference is that, how it estimates the probabilities for sequence of labels.
CRF first estimates a score for a sequence (which is not a probability). For a linear chain CRF, i.e. a CRF where the current label depends only on the previous label :
where is a feature function, that normally takes binary values, which is a function of the sequence of observation, the current label, the previous label (linear chain) and the index of the observation 'i'.
are the weights for these feature functions. High positive value of this weight indicate that the feature is relevant, whereas high negative value indicate that the feature is not relevant.
Higher score implies that the label assigned is a good one.
Note that these feature functions can have complex dependencies between labels, but linear chain is the most simple and commonly used variant as it is similar to HMM in terms of the Markov assumption.
Examples of feature function, w.r.t. the above sentence might be :
if =TRUE and =NNP else
if ="ing" and =VBZ else
if ="Learning" and ="Machine" and =NNP else
The transition feature functions will be defined as :
if =VBZ and =NNP else
and so on...
In text classifications, these feature functions would simply be the TF-IDF scores of the individual features (such as words, n-grams etc.), but in NLP, these feature functions enables us to represent very sparse features.
Once we obtain the above scores, we can easily transform these scores into probabilities using softmax function :
The denominator is just the normalization constant, where the summation is over all possible labelling for a 3-sequence, i.e. possible values.
In order to compute the conditional probabilities in the above equation, we need to know the values of the unknowns .
The values of the unknowns are computed by maximizing the conditional log likelihood of the labeled training sequences and then solve for the unknowns using iterative gradient ascent techniques. The conditional log likelihood is given by the formula :
The outermost summation is over all observation and label sequence pairs (x, y) in the training data. The last term is the L2 regularization factor for the weights . Substituting back the values of , we get :
The gradient of the likelihood w.r.t. the unknowns is computed to be :
Thus the gradient ascent rule for , would be given as :
where is the learning rate.
The likelihood function defined above is a concave function, and hence the solution has a global maxima. Thus any approach taken to solve for would eventually converge to the globally optimum solution. Simplest approach would be to use Stochastic Gradient Descent (or Batch gradient descent) with momentum. Advanced techniques such as L-BFGS would speed up the convergence to the solution.
The first quantity is the expected value of the feature function over all the training examples. For example, for the feature function :
if =TRUE and =NNP else
The expected value of the above feature function would be the total number of words which are title (first letter capitalized) and the POS tag for the word is 'NNP'.
The second quantity is the expected value of the feature function as predicted by the model itself. In the term :
The first summation is over all sequence of observations 'x' in the training data, but the second summation is over all possible sequence of labelings (not only which are present in the training data) for a sequence of length as same as the observation, since the model will assign probability to each possible sequence of labels as an outcome.
As mentioned earlier in HMM, for M possible types of labels, there could be maximum of possible labelings for an n-length sequence, hence direct summation would require exponential number of steps to compute the above summation.
Note that in a linear chain CRF, each current label depends only on the previous label. Instead of enumerating all possible label sequences 'y', one can use a dynamic programming approach for computing efficiently.
For a given observation sequence 'x', let represent the score for the sequence from 1 to k ending with the output label , i.e.
Observe that, although there would be different sequences from indices 1 to k, but each possible label for position k, uses the scores of different sequences ending at position (k-1) to obtain the scores of all sequences ending at position k.
We can write the above expression for :
using chain rule of probability :
is the transition probability from the label s' for the previous word to the label s for the current word. Using Bayes' Rule, we get
Substituting back the expression for into the above equation, the normalization term cancels out, and we remain with the following :
Thus we can write the expression for :
computes the scores using dynamic programming from the front of the sequence. Similarly one can compute the scores from the back of the sequence :
Note that the above recursion computes based on the values of , i.e. computing in reverse. The method of computing the sequence scores using and is known as the Forward-Backward algorithm.
Using the Forward-Backward algorithm, we can efficiently compute ,
Thus we can compute the value of the unknowns iteratively,
Once we have computed the optimum values of feature weights , we can use the trained model to make predictions on unlabeled test sequences. Similar to HMM, in order to compute the most likely label sequence for a test input sequence of length n, we use the Viterbi algorithm.
Some useful resources on Conditional Random Fields :
- Introduction to Conditional Random Fields blog by Edwin Chen
- An Introduction to Conditional Random Fields by Sutton
- Conditional Random Fields: An Introduction by Wallach
- Conditional Random Fields by Xiaojin Zhu
- Conditional Random Fields: Probabilistic Models for Segmenting and Labeling Sequence Data
Categories: MACHINE LEARNING