Inferensys

Glossary

Conditional Random Fields (CRF)

A discriminative probabilistic graphical model used for structured prediction that models the conditional probability of a label sequence given an observation sequence, commonly used as a decoding layer in Named Entity Recognition.
ML engineer running AI model benchmarks, performance charts on multiple screens, late night home office setup.
PROBABILISTIC GRAPHICAL MODEL

What is Conditional Random Fields (CRF)?

A discriminative probabilistic model for structured prediction that models the conditional probability of a label sequence given an observation sequence, widely used as a decoding layer in Named Entity Recognition.

A Conditional Random Field (CRF) is a discriminative undirected probabilistic graphical model that computes the conditional probability P(Y|X) of an entire label sequence Y given an observation sequence X. Unlike generative models such as Hidden Markov Models, a CRF directly models the decision boundary between labels without expending modeling capacity on the joint distribution of the observations, making it highly effective for sequence labeling tasks like NER and part-of-speech tagging.

In a linear-chain CRF, the model defines a log-linear score over the entire sequence using feature functions that capture transition dependencies between adjacent labels and emission scores from the underlying encoder. During inference, the Viterbi algorithm efficiently decodes the globally optimal label path, preventing illegal transitions such as an I-ORG tag following an O tag. This structural constraint is the primary advantage of a CRF over independent token-level softmax classifiers.

STRUCTURED PREDICTION

Key Features of Conditional Random Fields

Core architectural properties and operational mechanisms that make Conditional Random Fields a foundational decoding layer for sequence labeling tasks like Named Entity Recognition.

01

Discriminative Modeling

Unlike generative models (e.g., Hidden Markov Models) that model the joint probability P(X, Y), CRFs directly model the conditional probability P(Y|X) of a label sequence given an observation sequence. This eliminates the need to model complex dependencies in the input features, allowing the model to focus exclusively on the decision boundary. By conditioning on the entire observation sequence, a CRF can incorporate overlapping, non-independent features of the input without violating statistical assumptions.

02

Linear-Chain Structure

The most common CRF architecture for NER is the linear-chain CRF, which models a first-order Markov dependency between adjacent output labels. This structure defines an undirected graphical model where each label Yi is connected to the observation sequence X and to its immediate neighbors Yi-1 and Yi+1. This captures transition constraints—for example, an I-ORG tag cannot follow a B-PER tag—enforcing valid BIO label sequences during decoding.

03

Global Normalization

A defining characteristic of CRFs is that they compute a global partition function Z(X) that sums over all possible label sequences. This ensures the model produces a properly normalized probability distribution over entire sequences, not just local decisions. Unlike Maximum Entropy Markov Models (MEMMs), which normalize per-state and suffer from the label bias problem, CRFs avoid favoring states with fewer outgoing transitions, leading to more robust sequence-level predictions.

04

Feature Engineering Flexibility

CRFs can incorporate arbitrary, overlapping feature functions without independence assumptions. Feature templates can capture:

  • Token-level features: word identity, prefixes/suffixes, capitalization patterns, part-of-speech tags
  • Contextual features: surrounding word windows, conjunctions of multiple attributes
  • Label transition features: co-occurrence patterns between adjacent labels This flexibility made CRFs the dominant NER approach before deep learning, and they remain powerful when combined with neural feature extractors.
05

Viterbi Decoding

At inference time, CRFs use the Viterbi algorithm—a dynamic programming method—to find the single most probable label sequence. The algorithm recursively computes the maximum score path to each state at each position, storing backpointers to reconstruct the optimal sequence. This guarantees a globally optimal solution in O(T × S²) time, where T is sequence length and S is the number of states, ensuring that predicted label sequences respect learned transition constraints.

06

Log-Likelihood Training

CRFs are trained by maximizing the conditional log-likelihood of the correct label sequence given the observations. The gradient involves the difference between the empirical feature counts from the training data and the expected feature counts under the current model distribution. Computing these expectations requires running the forward-backward algorithm to calculate marginal probabilities over all possible label paths, making training computationally intensive but statistically principled.

CONDITIONAL RANDOM FIELDS

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the probabilistic graphical models that power structured prediction in modern NLP pipelines.

A Conditional Random Field (CRF) is a discriminative probabilistic graphical model designed for structured prediction that models the conditional probability P(Y|X) of an entire label sequence Y given an observation sequence X. Unlike generative models such as Hidden Markov Models (HMMs) that model the joint distribution P(X,Y), a CRF directly models the decision boundary between label sequences. In sequence labeling tasks like Named Entity Recognition (NER), a linear-chain CRF defines a log-linear model over cliques of adjacent labels, computing the unnormalized score of a label sequence as the sum of emission scores (how well a token fits a label) and transition scores (how likely one label follows another). The partition function normalizes these scores over all possible label sequences, and during inference, the Viterbi algorithm efficiently finds the optimal path. This global normalization is the key mechanism that prevents label bias, a weakness of locally normalized models like Maximum Entropy Markov Models (MEMMs).

DECODING LAYER COMPARISON

CRF vs. Other Sequence Decoding Methods

A comparison of Conditional Random Fields against alternative decoding architectures used for sequence labeling in Named Entity Recognition pipelines.

FeatureLinear-Chain CRFSoftmax (Token-Level)Global Pointer

Modeling Approach

Discriminative graphical model; models conditional probability of entire label sequence given observation sequence

Independent per-token classification; no explicit sequence-level dependencies

Multiplicative attention scoring all start-end token pairs; direct span enumeration

Label Dependency Modeling

Global Sequence Normalization

Decoding Algorithm

Viterbi decoding (dynamic programming)

Argmax per token (greedy)

Thresholded span scoring with hand-crafted or learned thresholds

Invalid Sequence Prevention

Learns transition constraints (e.g., I-ORG cannot follow B-PER)

Natively avoids invalid spans by scoring only valid start-end pairs

Training Objective

Maximum conditional log-likelihood of the correct label sequence

Cross-entropy loss per token

Binary cross-entropy over all possible spans (class imbalance handled via loss weighting)

Inference Speed

Slower; Viterbi has O(T·N²) complexity where T is sequence length and N is label count

Fastest; O(T·N) complexity

Moderate; O(T²·N) complexity due to scoring all span pairs

Typical NER F1 on CoNLL-2003

93.5% (with BERT encoder)

92.8% (with BERT encoder)

93.1% (with BERT encoder)

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.