Skip to content
mlmentorship

Conditional random fields (CRFs)

A CRF models labels for a whole sequence and scores transitions jointly. Linear-chain CRFs improve taggers when neighboring labels constrain each other.

Published · 7 min read ·Specialist ·Intermediate

Summary

A CRF is a discriminative, undirected graphical model that defines over a structured output (e.g. a label sequence), scoring entire labelings jointly through feature functions over cliques, most commonly a linear-chain CRF that couples adjacent labels.

CRFs are the classic answer to structured prediction: when your outputs are interdependent (the label of token depends on token ), independent per-token softmax classification is wrong because it can produce illegal or incoherent label sequences (e.g. an I-PER tag right after an O tag in BIO tagging). A CRF layer fixes this by modeling transitions.

They remain interview-relevant because:

  • A linear-chain CRF on top of a BiLSTM or transformer encoder was the standard NER / POS / chunking architecture and still appears in production sequence taggers.
  • CRF vs HMM is the cleanest way to show you understand generative vs discriminative modeling of sequences.
  • The training objective is a clean example of a globally-normalized log-likelihood with a forward-algorithm partition function.

The model

A linear-chain CRF scores a full label sequence given input :

where is the emission / unary score (how well label fits position , often the logits from a neural encoder), is a learned transition score between adjacent labels, and

is the partition function: a sum over all possible labelings. couples the whole sequence through global normalization. Per-token softmax instead normalizes each position locally and independently.

Learning objective

Transition scores can overturn the locally best labels

Transition penalties can defeat the locally best labelsLocal emissions favor AA, but its transition penalty is -5. Globally, AB or BA scores 3 and beats AA at -1.Transition cost can undo localemission gainsAA: emissions 4, transition -5Complete-path score -1AB: emissions 3, transition 0Complete-path score 3BB: emissions 2, transition 0Complete-path score 2
Derivation
Independent emissions compared with CRF whole-path scoring For the two tokens Ada Lovelace, independent selection picks O with emission 3 and I-PER with emission 3, totaling 6 but forming an incoherent BIO sequence. In the CRF example, the O to I-PER transition has score minus 5, so that complete path scores 1. The B-PER to I-PER path combines emissions 2 and 3 with transition score plus 1, scores 6, and is selected as coherent. These are illustrative learned scores rather than universal hard constraints. INDEPENDENT SOFTMAX · MAXIMIZE EACH EMISSION Ada Lovelace O emission = 3 I-PER emission = 3 3 + 3 = 6 · INVALID BIO: O → I-PER LINEAR-CHAIN CRF · SCORE COMPLETE PATHS Ada Lovelace +1 B-PER emission = 2 I-PER emission = 3 chosen: 2 + 3 + 1 = 6 · VALID BIO rejected O → I-PER: 3 + 3 − 5 = 1
the token emissions stay fixed. Softmax takes each column's maximum and cannot penalize the invalid O → I-PER pair. The CRF adds a learned score for each adjacent-label transition, compares complete paths, and selects B-PER → I-PER in this example. The forward algorithm sums the exponentiated scores of all paths to compute Z; Viterbi finds the highest-scoring path.
Check: Why does the locally preferred O-to-I-PER path lose after transition scoring?
Read it this way: Local emissions favor AA, but its transition penalty is -5. Globally, AB or BA scores 3 and beats AA at -1.
Does the path with the best local emissions still win after its transition penalty?

Training and inference

The partition function looks intractable ( terms) but factorizes over the chain:

  • Training: maximize . The gradient needs and the marginals, both computed by the forward algorithm (the same dynamic program as HMM forward-backward) in .
  • Decoding: find with the Viterbi algorithm, also .

So a CRF reuses exactly the forward-backward and Viterbi machinery, but on a discriminatively trained, globally normalized model.

CRF vs HMM vs softmax tagger

ModelsNormalizationFeatures
HMM generativelocal (per emission/transition)tied to generative story
MEMM, per-steplocal (per step) → label biasrich, but biased
Linear-chain CRFglobal (one per sequence)rich, no label bias
Independent softmaxlocal, independentno transition modeling

The CRF’s global normalization is what cures the label-bias problem of MEMMs (locally normalized models that can’t redistribute probability mass once committed at a step).

The neural CRF (BiLSTM-CRF / Transformer-CRF)

In modern systems the encoder (BiLSTM or transformer) produces the emission scores , and a small learned transition matrix sits on top. The whole stack is trained end-to-end with the CRF negative log-likelihood. The encoder captures rich context; the CRF enforces valid, coherent label transitions. This combination reliably beats a softmax-per-token head on tasks with strong output structure (NER, slot filling).

What an interviewer expects you to say

  1. State that a CRF models over the whole sequence, with emission + transition scores and a global partition function .
  2. Explain why it beats independent softmax: it models label dependencies / transitions and avoids illegal sequences.
  3. Know that training uses the forward algorithm (for ) and decoding uses Viterbi, both .
  4. Place it on the generative-vs-discriminative map (HMM is the generative cousin) and mention the label-bias problem CRFs fix relative to MEMMs.
  5. Bonus: the BiLSTM-CRF / encoder-CRF pattern (neural encoder for emissions, CRF layer for structure).

Common confusions

  • “CRF = HMM.” HMM is generative and locally normalized; CRF is discriminative and globally normalized. CRFs can use arbitrary, overlapping input features.
  • “You need a CRF whenever you tag sequences.” Only when output structure matters. With a strong contextual encoder (large transformer), the marginal gain of a CRF head shrinks because the encoder already captures most dependencies, but it still helps enforce hard constraints.
  • “The partition function is intractable.” For a chain it’s an forward pass. It’s only intractable for general (loopy) graph structures.
  • “CRFs are obsolete.” The CRF layer is still a standard, cheap way to enforce coherent label sequences on top of any encoder.

Related: Forward-backward and Viterbi, Hidden Markov models, Belief propagation, Graphical models.