Conditional Random Fields for Sequence Prediction
This is the third and (maybe) the last part of a series of posts about sequential supervised learning applied to NLP. In this post I will talk about Conditional Random Fields (CRF), explain what was the main motivation behind the proposal of this model, and make a final comparison between Hidden Markov Models (HMM), Maximum Entropy Markov Models (MEMM) and CRF for sequence prediction.
You can find the first and second posts here:
Introduction
CRFs were proposed roughly only year after the Maximum Entropy Markov Models, basically by the same authors. Reading through the original paper that introduced Conditional Random Fields, one finds at the beginning this sentence:
“The critical difference between CRF and MEMM is that the latter uses perstate exponential models for the conditional probabilities of next states given the current state, whereas CRF uses a single exponential model to determine the joint probability of the entire sequence of labels, given the observation sequence. Therefore, in CRF, the weights of different features in different states compete against each other.”
This means that in the MEMMs there is a model to compute the probability of the next state, given the current state and the observation. On the other hand CRF computes all state transitions globally, in a single model.
The main motivation for this proposal is the so called Label Bias Problem occurring in MEMM, which generates a bias towards states with few successor states.
Label Bias Problem in MEMMs
Recalling how the transition probabilities are computed in a MEMM model, from the previous post, we learned that the probability of the next state is only dependent on the observation (i.e., the sequence of words) and the previous state, that is, we have exponential model for each state to tell us the conditional probability of the next states:
This causes the so called Label Bias Problem, and Lafferty et al. 2001 demonstrate this through experiments and report it. I will not demonstrate it, but just give the basic intuition taken also from the paper:
Given the observation sequence: r i b
“In the first time step, r matches both transitions from the start state, so the probability mass gets distributed roughly equally among those two transitions. Next we observe i. Both states 1 and 4 have only one outgoing transition. State 1 has seen this observation often in training, state 4 has almost never seen this observation; but like state 1, state 4 has no choice but to pass all its mass to its single outgoing transition, since it is not generating the observation, only conditioning on it. Thus, states with a single outgoing transition effectively ignore their observations.”
[…]
“the top path and the bottom path will be about equally likely, independently of the observation sequence. If one of the two words is slightly more common in the training set, the transitions out of the start state will slightly prefer its corresponding transition, and that word’s state sequence will always win.”

Transitions from a given state are competing against each other only.

Per state normalization, i.e. sum of transition probability for any state has to sum to 1.

MEMM are normalized locally over each observation where the transitions going out from a state compete only against each other, as opposed to all the other transitions in the model.

States with a single outgoing transition effectively ignore their observations.

Causes bias: states with fewer arcs are preferred.
The idea of CRF is to drop this local per state normalization, and replace it by a global per sequence normalisation.
So, how dow we formalise this global normalisation? I will try to explain it in the sections that follow.
Undirected Graphical Models
A Conditional Random Field can be seen as an undirected graphical model, or Markov Random Field, globally conditioned on , the random variable representing observation sequence.
Lafferty et al. 2001 define a Conditional Random Field as:

is a random variable over data sequences to be labeled, and is a random variable over corresponding label sequences.

The random variables and are jointly distributed, but in a discriminative framework we construct a conditional model from paired observation and label sequences:
Let be a graph such that , so that is indexed by the vertices of .
is a conditional random field when each of the random variables , conditioned on , obey the Markov property with respect to the graph:
where means that and are neighbours in G. Thus, a CRF is a random field globally conditioned on the observation . This goes already in the direction of what the MEMM doesn’t give us, states globally conditioned on the observation.
This graph may have an arbitrary structure as long as it represents the label sequences being modelled, this is also called general Conditional Random Fields.
However the simplest and most common graph structured in NLP, which is the one used to model sequences is the one in which the nodes corresponding to elements of form a simple firstorder chain, as illustrated in the figure below:
This is also called linearchain conditional random fields, which is the type of CRF on which the rest of this post will focus.
Linearchain CRFs
Let is a sequence of words and a corresponding sequence of tags:
This can been seen as another loglinear model, but “giant” in the sense that:
 The space of possible values for , i.e., , is huge, where is the since of the sequence.
 The normalisation constant involves a sum over the set .
will represent a global feature vector defined by a set of feature functions , where each feature function can analyse the whole sequence, the current and previous positions in the labels sequence, and the current position in the sentence:
we can defined an arbitrary number of feature functions. The k’th global feature is then computed by summing the over all the different state transitions . In this way we have a “global” feature vector that maps the entire sequence: .
Thus, the full expanded linearchain CRF equation is:
Having the framework defined by the equation above we now analyse how to perform two operations: parameter estimation and sequence prediction.
Inference
Inference with a linearchain CRF resolves to computing the sequence that maximizes the following equation:
We want to try all possible sequences computing for each one the probability of “fitting” the observation with feature weights . If we just want the score for a particular labelling sequence , we can ignore the exponential inside the numerator, and the denominator:
then, we replace by it’s definition:
Each transition from state to state has an associated score:
Since we took the out, this score could be positive or negative, intuitively, this score will be relatively high if the state transition is plausible, relatively low if this transition is implausible.
The decoding problem is then to find an entire sequence of states such that the sum of the transition scores is maximized. We can again solve this problem using a variant of the Viterbi algorithm, in a very similar way to the decoding algorithm for HMMs or MEMMs.
The denominator, also called the partition function:
is useful to compute a marginal probability. For example, this is useful for measuring the model’s confidence in it’s predicted labeling over a segment of input. This marginal probability can be computed efficiently using the forwardbackward algorithm. See the references section for demonstrations on how this is achieved.
Parameter Estimation
We also need to find the parameters that best fit the training data, a given a set of labelled sentences:
where each pair is a sentence with the corresponding word labels annotated. To find the parameters that best fit the data we need to maximize the conditional likelihood of the training data:
the parameter estimates are computed as:
where is an L2 regularization term.
The standard approach to finding is to compute the gradient of the objective function, and use the gradient in an optimization algorithm like LBFGS.
Wrapping up: HMM vs. MEMM vs. CRF
It is now helpful to look at the three sequence prediction models, and compared them. The figure bellow shows the graphical representation for the Hidden Markov Model, the Maximum Entropy Markov Model and the Conditional Random Fields.
 Hidden Markov Models:
 Maximum Entropy Markov Models:
 Conditional Random Fields:
CRF Important Observations

MEMMs are normalized locally over each observation, and hence suffer from the Label Bias problem, where the transitions going out from a state compete only against each other, as opposed to all the other transitions in the model.

CRFs avoid the label bias problem a weakness exhibited by Maximum Entropy Markov Models (MEMM). The big difference between MEMM and CRF is that MEMM is locally renormalized and suffers from the label bias problem, while CRFs are globally renormalized.

The inference algorithm in CRF is again based on Viterbi algorithm.

Output transition and observation probabilities are not modelled separately.

Output transition dependent on the state and the observation as one conditional probability.
Software Packages

pythoncrfsuite: is a python binding for CRFsuite which is a fast implementation of Conditional Random Fields written in C++.

CRF++: Yet Another CRF toolkit: is a popular implementation in C++ but as far as I know there are no python bindings.

MALLET:includes implementations of widely used sequence algorithms including hidden Markov models (HMMs) and linear chain conditional random fields (CRFs), it’s written in Java.

FlexCRFs supports both firstorder and secondorder Markov CRFs, it’s written in C/C++ using STL library.

pythonwapiti is a python wrapper for wapiti, a sequence labeling tool with support for maxent models, maximum entropy Markov models and linearchain CRF.
References

“Conditional Random Fields: Probabilistic Models for Segmenting and Labeling Sequence Data”

“An Introduction to Conditional Random Fields” Sutton, Charles; McCallum, Andrew (2010)
Acknowledgments
The writing of this post is also the outcome of many discussions and white board sessions I had together with Tobias Sterbak and Sebastian Mika.
Related posts
conditionalrandomfields
sequenceprediction
viterbi
]