This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
(Learn how and when to remove this template message) |

Part of a series on |

Machine learning and data mining |
---|

**Conditional random fields** (**CRFs**) are a class of statistical modeling method often applied in pattern recognition and machine learning and used for structured prediction. Whereas a classifier predicts a label for a single sample without considering "neighboring" samples, a CRF can take context into account. To do so, the prediction is modeled as a graphical model, which implements dependencies between the predictions. What kind of graph is used depends on the application. For example, in natural language processing, linear chain CRFs are popular, which implement sequential dependencies in the predictions. In image processing the graph typically connects locations to nearby and/or similar locations to enforce that they receive similar predictions.

Other examples where CRFs are used are: labeling or parsing of sequential data for natural language processing or biological sequences,^{[1]} POS tagging, shallow parsing,^{[2]} named entity recognition,^{[3]} gene finding, peptide critical functional region finding,^{[4]} and object recognition^{[5]} and image segmentation in computer vision.^{[6]}

CRFs are a type of discriminative undirected probabilistic graphical model.

Lafferty, McCallum and Pereira^{[1]} define a CRF on observations and random variables as follows:

Let be a graph such that

, so that is indexed by the vertices of . Then is a conditional random field when the random variables , conditioned on , obey the Markov property with respect to the graph: , where means that and are neighbors in .

What this means is that a CRF is an undirected graphical model whose nodes can be divided into exactly two disjoint sets and , the observed and output variables, respectively; the conditional distribution is then modeled.

For general graphs, the problem of exact inference in CRFs is intractable. The inference problem for a CRF is basically the same as for an MRF and the same arguments hold.^{[7]}
However, there exist special cases for which exact inference is feasible:

- If the graph is a chain or a tree, message passing algorithms yield exact solutions. The algorithms used in these cases are analogous to the forward-backward and Viterbi algorithm for the case of HMMs.
- If the CRF only contains pair-wise potentials and the energy is submodular, combinatorial min cut/max flow algorithms yield exact solutions.

If exact inference is impossible, several algorithms can be used to obtain approximate solutions. These include:

- Loopy belief propagation
- Alpha expansion
- Mean field inference
- Linear programming relaxations

Learning the parameters is usually done by maximum likelihood learning for . If all nodes have exponential family distributions and all nodes are observed during training, this optimization is convex.^{[7]} It can be solved for example using gradient descent algorithms, or Quasi-Newton methods such as the L-BFGS algorithm. On the other hand, if some variables are unobserved, the inference problem has to be solved for these variables. Exact inference is intractable in general graphs, so approximations have to be used.

In sequence modeling, the graph of interest is usually a chain graph. An input sequence of observed variables represents a sequence of observations and represents a hidden (or unknown) state variable that needs to be inferred given the observations. The are structured to form a chain, with an edge between each and . As well as having a simple interpretation of the as "labels" for each element in the input sequence, this layout admits efficient algorithms for:

- model
*training*, learning the conditional distributions between the and feature functions from some corpus of training data. *decoding*, determining the probability of a given label sequence given .*inference*, determining the*most likely*label sequence given .

The conditional dependency of each on is defined through a fixed set of *feature functions* of the form , which can be thought of as measurements on the input sequence that partially determine the likelihood of each possible value for . The model assigns each feature a numerical weight and combines them to determine the probability of a certain value for .

Linear-chain CRFs have many of the same applications as conceptually simpler hidden Markov models (HMMs), but relax certain assumptions about the input and output sequence distributions. An HMM can loosely be understood as a CRF with very specific feature functions that use constant probabilities to model state transitions and emissions. Conversely, a CRF can loosely be understood as a generalization of an HMM that makes the constant transition probabilities into arbitrary functions that vary across the positions in the sequence of hidden states, depending on the input sequence.

Notably, in contrast to HMMs, CRFs can contain any number of feature functions, the feature functions can inspect the entire input sequence at any point during inference, and the range of the feature functions need not have a probabilistic interpretation.

CRFs can be extended into higher order models by making each dependent on a fixed number of previous variables . In conventional formulations of higher order CRFs, training and inference are only practical for small values of (such as *k* ≤ 5),^{[8]} since their computational cost increases exponentially with .

However, another recent advance has managed to ameliorate these issues by leveraging concepts and tools from the field of Bayesian nonparametrics. Specifically, the CRF-infinity approach^{[9]} constitutes a CRF-type model that is capable of learning infinitely-long temporal dynamics in a scalable fashion. This is effected by introducing a novel potential function for CRFs that is based on the Sequence Memoizer (SM), a nonparametric Bayesian model for learning infinitely-long dynamics in sequential observations.^{[10]} To render such a model computationally tractable, CRF-infinity employs a mean-field approximation^{[11]} of the postulated novel potential functions (which are driven by an SM). This allows for devising efficient approximate training and inference algorithms for the model, without undermining its capability to capture and model temporal dependencies of arbitrary length.

There exists another generalization of CRFs, the **semi-Markov conditional random field (semi-CRF)**, which models variable-length *segmentations* of the label sequence .^{[12]} This provides much of the power of higher-order CRFs to model long-range dependencies of the , at a reasonable computational cost.

Finally, large-margin models for structured prediction, such as the structured Support Vector Machine can be seen as an alternative training procedure to CRFs.

**Latent-dynamic conditional random fields** (**LDCRF**) or **discriminative probabilistic latent variable models** (**DPLVM**) are a type of CRFs for sequence tagging tasks. They are latent variable models that are trained discriminatively.

In an LDCRF, like in any sequence tagging task, given a sequence of observations **x** = , the main problem the model must solve is how to assign a sequence of labels **y** = from one finite set of labels Y. Instead of directly modeling P(**y**|**x**) as an ordinary linear-chain CRF would do, a set of latent variables **h** is "inserted" between **x** and **y** using the chain rule of probability:^{[13]}

This allows capturing latent structure between the observations and labels.^{[14]} While LDCRFs can be trained using quasi-Newton methods, a specialized version of the perceptron algorithm called the **latent-variable perceptron** has been developed for them as well, based on Collins' structured perceptron algorithm.^{[13]} These models find applications in computer vision, specifically gesture recognition from video streams^{[14]} and shallow parsing.^{[13]}

This is a partial list of software that implement generic CRF tools.

- RNNSharp CRFs based on recurrent neural networks (C#, .NET)
- CRF-ADF Linear-chain CRFs with fast online ADF training (C#, .NET)
- CRFSharp Linear-chain CRFs (C#, .NET)
- GCO CRFs with submodular energy functions (C++, Matlab)
- DGM General CRFs (C++)
- GRMM General CRFs (Java)
- factorie General CRFs (Scala)
- CRFall General CRFs (Matlab)
- Sarawagi's CRF Linear-chain CRFs (Java)
- HCRF library Hidden-state CRFs (C++, Matlab)
- Accord.NET Linear-chain CRF, HCRF and HMMs (C#, .NET)
- Wapiti Fast linear-chain CRFs (C)
^{[15]} - CRFSuite Fast restricted linear-chain CRFs (C)
- CRF++ Linear-chain CRFs (C++)
- FlexCRFs First-order and second-order Markov CRFs (C++)
- crf-chain1 First-order, linear-chain CRFs (Haskell)
- imageCRF CRF for segmenting images and image volumes (C++)
- MALLET Linear-chain for sequence tagging (Java)
- PyStruct Structured Learning in Python (Python)
- Pycrfsuite A python binding for crfsuite (Python)
- Figaro Probabilistic programming language capable of defining CRFs and other graphical models (Scala)
- CRF Modeling and computational tools for CRFs and other undirected graphical models (R)
- OpenGM Library for discrete factor graph models and distributive operations on these models (C++)
- UPGMpp
^{[5]}Library for building, training, and performing inference with Undirected Graphical Models (C++) - KEG_CRF Fast Linear CRFs (C++)

This is a partial list of software that implement CRF related tools.

- MedaCy Medical Named Entity Recognizer (Python)
- Conrad CRF based gene predictor (Java)
- Stanford NER Named Entity Recognizer (Java)
- BANNER Named Entity Recognizer (Java)

- ^
^{a}^{b}.mw-parser-output cite.citation{font-style:inherit}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .id-lock-free a,.mw-parser-output .citation .cs1-lock-free a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/6/65/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited a,.mw-parser-output .id-lock-registration a,.mw-parser-output .citation .cs1-lock-limited a,.mw-parser-output .citation .cs1-lock-registration a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/d/d6/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription a,.mw-parser-output .citation .cs1-lock-subscription a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/a/aa/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-ws-icon a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/4/4c/Wikisource-logo.svg")right 0.1em center/12px no-repeat}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-maint{display:none;color:#33aa33;margin-left:0.3em}.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit} Lafferty, J., McCallum, A., Pereira, F. (2001). "Conditional random fields: Probabilistic models for segmenting and labeling sequence data".*Proc. 18th International Conf. on Machine Learning*. Morgan Kaufmann. pp. 282–289.CS1 maint: uses authors parameter (link) **^**Sha, F.; Pereira, F. (2003). *shallow parsing with conditional random fields*.**^**Settles, B. (2004). "Biomedical named entity recognition using conditional random fields and rich feature sets" (PDF). *Proceedings of the International Joint Workshop on Natural Language Processing in Biomedicine and its Applications*. pp. 104–107.**^**Chang KY; Lin T-p; Shih L-Y; Wang C-K (2015). *Analysis and Prediction of the Critical Regions of Antimicrobial Peptides Based on Conditional Random Fields*. PLoS ONE. doi:10.1371/journal.pone.0119490. PMC 4372350.- ^
^{a}^{b}J.R. Ruiz-Sarmiento; C. Galindo; J. Gonzalez-Jimenez (2015). "UPGMpp: a Software Library for Contextual Object Recognition.". *3rd. Workshop on Recognition and Action for Scene Understanding (REACTS)*. **^**He, X.; Zemel, R.S.; Carreira-Perpinñán, M.A. (2004). "Multiscale conditional random fields for image labeling". IEEE Computer Society. CiteSeerX 10.1.1.3.7826. - ^
^{a}^{b}Sutton, Charles; McCallum, Andrew (2010). "An Introduction to Conditional Random Fields". arXiv:1011.4088v1 [stat.ML]. **^**Lavergne, Thomas; Yvon, François (September 7, 2017). "Learning the Structure of Variable-Order CRFs: a Finite-State Perspective". *Proceedings of the 2017 Conference on Empirical Methods in Natural Language Processing*. Copenhagen, Denmark: Association for Computational Linguistics. p. 433.**^**Chatzis, Sotirios; Demiris, Yiannis (2013). "The Infinite-Order Conditional Random Field Model for Sequential Data Modeling". *IEEE Transactions on Pattern Analysis and Machine Intelligence*.**35**(6): 1523–1534. doi:10.1109/tpami.2012.208. hdl:10044/1/12614. PMID 23599063.**^**Gasthaus, Jan; Teh, Yee Whye (2010). "Improvements to the Sequence Memoizer" (PDF). *Proc. NIPS*.**^**Celeux, G.; Forbes, F.; Peyrard, N. (2003). "EM Procedures Using Mean Field-Like Approximations for Markov Model-Based Image Segmentation". *Pattern Recognition*.**36**(1): 131–144. CiteSeerX 10.1.1.6.9064. doi:10.1016/s0031-3203(02)00027-4.**^**Sarawagi, Sunita; Cohen, William W. (2005). "Semi-Markov conditional random fields for information extraction" (PDF). In Lawrence K. Saul; Yair Weiss; Léon Bottou (eds.). *Advances in Neural Information Processing Systems 17*. Cambridge, MA: MIT Press. pp. 1185–1192.- ^
^{a}^{b}^{c}Xu Sun; Takuya Matsuzaki; Daisuke Okanohara; Jun'ichi Tsujii (2009). *Latent Variable Perceptron Algorithm for Structured Classification*. IJCAI. pp. 1236–1242. - ^
^{a}^{b}Morency, L. P.; Quattoni, A.; Darrell, T. (2007). "Latent-Dynamic Discriminative Models for Continuous Gesture Recognition" (PDF). *2007 IEEE Conference on Computer Vision and Pattern Recognition*. p. 1. CiteSeerX 10.1.1.420.6836. doi:10.1109/CVPR.2007.383299. ISBN 978-1-4244-1179-5. **^**T. Lavergne, O. Cappé and F. Yvon (2010). Practical very large scale CRFs Archived 2013-07-18 at the Wayback Machine. Proc. 48th Annual Meeting of the ACL, pp. 504-513.