Inferensys

Glossary

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.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
SEQUENCE MODELING

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.

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.

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.

PROBABILISTIC GRAPHICAL MODELS

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.

01

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.

P(Y|X)
Directly Modeled
Arbitrary
Feature Overlap
02

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.

Y_t ↔ Y_{t-1}
Adjacent Dependency
BIO
Tagging Schema
03

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.

Viterbi
Decoding Algorithm
Z(X)
Partition Function
04

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.

f(y,X,t)
Feature Function
λ
Learned Weight
05

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.

Forward-Backward
Training Algorithm
L2
Regularization
06

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.

BiLSTM-CRF
Classic Architecture
i2b2
Clinical Benchmark
CONDITIONAL RANDOM FIELDS

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).

Prasad Kumkar

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.