A Conditional Random Field (CRF) is a discriminative undirected probabilistic graphical model used for sequence labeling and segmentation. Unlike generative models that model the joint probability P(X, Y), a CRF directly models the conditional probability P(Y|X) of a label sequence Y given an observation sequence X. This allows the model to incorporate complex, overlapping features of the entire input sequence without making strong independence assumptions about the observations, making it highly effective for structured prediction tasks like medical named entity recognition.
Glossary
Conditional Random Fields (CRF)

What is Conditional Random Fields (CRF)?
A discriminative probabilistic graphical model for segmenting and labeling sequence data, often used as a decoding layer to capture dependencies between adjacent clinical entity tags.
In clinical NLP, a CRF is frequently employed as the decoding layer on top of neural network encoders, such as a BiLSTM-CRF architecture. The CRF layer learns a transition matrix that captures the logical dependencies between adjacent output labels, enforcing valid sequences like I-Disease following B-Disease and preventing impossible transitions. This ensures globally coherent predictions for BIO tagging schemes, significantly improving the extraction of multi-token clinical concepts like 'congestive heart failure' from unstructured text.
Core Characteristics of CRFs
Conditional Random Fields are discriminative models that excel at structured prediction by modeling the conditional probability of an entire label sequence given an input sequence, rather than modeling the joint distribution.
Discriminative vs. Generative Modeling
CRFs directly model the conditional probability P(Y|X) of a label sequence Y given an observation sequence X. Unlike generative Hidden Markov Models (HMMs) that model the joint probability P(X,Y), CRFs do not waste modeling effort on the distribution of the observations themselves. This allows them to incorporate arbitrary, overlapping features of the input sequence without making naive independence assumptions about the data. In clinical NER, this means a CRF can easily use features like 'is the word capitalized?', 'is it followed by a dosage number?', and 'does it appear in RxNorm?' simultaneously.
Linear-Chain Structure for Sequence Labeling
The most common CRF architecture for NER is the linear-chain CRF. It models dependencies only between adjacent output labels (Y_t and Y_{t-1}). This structure creates a Markov blanket where the label for the current token is conditionally independent of all other labels given its immediate neighbors and the input sequence. This is perfectly suited for BIO tagging in clinical text, as it captures valid transitions—for example, an I-DRUG tag cannot follow a B-DISEASE tag without an intervening O tag—enforcing grammatical label consistency.
Global Normalization and the Partition Function
Unlike local classifiers that make independent per-token decisions, CRFs perform global normalization over the entire sequence. The model computes a partition function Z(X) that sums over all possible label sequences for a given input. This is computationally expensive but ensures the output is a valid probability distribution over entire sequences. During inference, the Viterbi algorithm efficiently finds the single most probable label path. This prevents illegal BIO transitions that a greedy token classifier might produce, such as starting a medication entity with an I-DRUG tag.
Feature Engineering with Arbitrary Functions
A CRF's power comes from its ability to ingest hand-crafted feature functions that fire based on the input sequence and label transitions. These features are not limited to the current token; they can inspect any position in the sequence. In a clinical NER system, a feature function might be: f(y_t, y_{t-1}, X, t) = 1 if X[t] is 'mg' and y_t is I-DOSAGE. These binary indicator functions are weighted by learned parameters λ. This transparency makes CRFs highly interpretable—you can inspect exactly which features activated for a prediction, a critical requirement for clinical validation.
Training via Maximum Likelihood Estimation
CRFs are trained using maximum conditional likelihood estimation. The objective is to maximize the log-probability of the correct label sequence across all training instances. The gradient of this objective has an intuitive form: it is the difference between the empirical count of a feature firing in the training data and the expected count under the model's current distribution. This requires computing the marginal probabilities of label pairs at each position, typically done with the forward-backward algorithm. L2 regularization is standard to prevent overfitting to sparse clinical annotation features.
CRF as a Decoding Layer on Neural Networks
In modern clinical NLP, hand-engineered features are often replaced by neural feature extractors like BiLSTMs or Transformers. The CRF is then placed as the final decoding layer on top of these networks. The neural network produces emission scores for each token-label pair, and the CRF layer adds learned transition scores between adjacent labels. This hybrid architecture—often called BiLSTM-CRF or Transformer-CRF—combines the representational power of deep learning with the structured prediction guarantees of a CRF, yielding state-of-the-art performance on medical NER benchmarks like i2b2.
Frequently Asked Questions
Explore the core mechanics and clinical applications of Conditional Random Fields, a foundational probabilistic model for structured prediction in medical NLP pipelines.
A Conditional Random Field (CRF) is a discriminative probabilistic graphical model specifically designed for segmenting and labeling sequential data. Unlike a generative model that models the joint probability P(X, Y), a CRF directly models the conditional probability P(Y|X) of a label sequence Y given an observation sequence X. It works by defining a conditional probability distribution over the label sequence using a set of feature functions that capture dependencies between adjacent labels and the input sequence. During inference, the Viterbi algorithm is used to find the globally most probable label sequence, ensuring that the predicted tags form a coherent structure rather than a series of independent, potentially conflicting, decisions. This makes it exceptionally powerful for tasks like Medical Named Entity Recognition, where the label for a token (e.g., B-DRUG) strongly depends on the label of the previous token (I-DRUG).
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Explore the foundational sequence modeling concepts, alternative architectures, and evaluation methodologies that contextualize the role of Conditional Random Fields in clinical NLP pipelines.
Sequence Labeling
The predictive modeling task for which CRFs are optimized. Sequence labeling assigns a categorical label to each element in a sequence, such as a token in a clinical sentence. Unlike independent classification, the label assigned to one token depends on the labels of adjacent tokens. This is critical for medical NER, where a token like 'cell' is labeled differently in 'T-cell lymphoma' versus 'sickle cell anemia' based on neighboring context. CRFs model these transitions explicitly.
BIO Tagging
The standard annotation scheme used to train CRF-based clinical NER models. Tokens are tagged as B (Beginning of an entity), I (Inside an entity), or O (Outside). For example, 'metastatic melanoma' becomes 'B-Disease I-Disease'. CRFs excel here because they learn that an I-Disease tag cannot follow an O tag, enforcing valid tag sequences. This structural constraint prevents the model from predicting fragmented or logically impossible entity spans.
Hidden Markov Models
The generative predecessor to CRFs. HMMs model the joint probability of observations and labels, requiring a strict independence assumption between input features. CRFs, as a discriminative model, directly model the conditional probability of the label sequence given the observation sequence. This allows CRFs to incorporate overlapping, long-range, and non-independent features of the entire input sentence—like word shape and surrounding lexical context—without violating model assumptions, leading to superior accuracy on clinical text.
Linear-Chain CRF
The specific CRF topology most commonly used in clinical NLP. It models a first-order Markov dependency between adjacent output labels. The probability of a label sequence y given an input sequence x is computed using feature functions that score label transitions and state-observation pairs. During inference, the Viterbi algorithm efficiently finds the single most probable label sequence, ensuring that a predicted entity like 'acute myocardial infarction' is a contiguous, valid span.
BiLSTM-CRF Architecture
A dominant neural architecture for medical NER that uses a CRF as the final decoding layer. A Bidirectional LSTM processes the input sequence to generate rich contextual embeddings for each token. These embeddings are then fed into a CRF layer, which decodes the globally optimal tag sequence. The BiLSTM handles feature extraction, while the CRF enforces structural consistency between tags, combining the representational power of deep learning with the constraint satisfaction of probabilistic graphical models.
F1 Score
The primary evaluation metric for CRF-based clinical NER systems. It is the harmonic mean of precision (the fraction of predicted entities that are correct) and recall (the fraction of true entities that were predicted). In medical contexts, a CRF's ability to enforce valid tag sequences directly improves precision by preventing impossible entity boundaries. A high F1 score indicates the model correctly identifies clinical concepts like 'congestive heart failure' without producing fragmented or spurious extractions.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us