CRF Training › The Log-Likelihood Objective

Michael BrenndoerferUpdated May 4, 20258 min read
Language AI HandbookAll chapters
Part 6 of 67Sequence Labeling
Chapter 43CRF Training › The Log-Likelihood Objective
Reading section 2 of 11 · The Log-Likelihood Objective · 8 minSection 2 of 11 · 8 min

Book contents

Section 2 of 11 · 8 min read

CRF Training › The Log-Likelihood ObjectiveOne section · Sections in this chapter2 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Training a CRF means finding feature weights that make correct label sequences probable. Given training data consisting of observation sequences x(i)\mathbf{x}^{(i)} and their correct label sequences y(i)\mathbf{y}^{(i)}, we want to maximize the conditional probability of seeing these correct labels.

The key insight is that we are learning a discriminative model, not a generative one. We never model the probability of observing a particular sentence; we only model the probability of a label sequence given that sentence. This is the same philosophical stance taken by logistic regression versus naive Bayes: rather than modeling the joint distribution P(x,y)P(\mathbf{x}, \mathbf{y}), we model P(y∣x)P(\mathbf{y} | \mathbf{x}) directly. For sequence labeling, this matters because modeling the sentence itself is hard (it requires a language model), while modeling labels conditioned on a sentence is much more tractable. The CRF inherits this advantage from its discriminative design.

Think of each training example as a contest between the correct label sequence and every competing alternative. The CRF assigns a score to each sequence using a weighted sum of features. Maximizing the log-likelihood of the correct sequences is equivalent to training the model to assign the correct sequence a score that is higher, on average, than the scores of all competing sequences. The partition function Z(x)Z(\mathbf{x}) is what forces this competition: it aggregates the scores of all alternatives and normalizes them into a probability distribution. Making the correct sequence more probable necessarily means making alternative sequences less probable in relative terms.

Log-Likelihood

The log-likelihood is the sum of log probabilities of the correct label sequences across all training examples. Taking the log converts products into sums and makes optimization numerically stable.

For a single training example with observation sequence x\mathbf{x} and label sequence y\mathbf{y}, the CRF defines the conditional probability as:

P(y∣x)=1Z(x)exp⁡(∑t=1T∑kλkfk(yt−1,yt,x,t))P(\mathbf{y} | \mathbf{x}) = \frac{1}{Z(\mathbf{x})} \exp\left(\sum_{t=1}^{T} \sum_{k} \lambda_k f_k(y_{t-1}, y_t, \mathbf{x}, t)\right)

where:

  • y=(y1,y2,…,yT)\mathbf{y} = (y_1, y_2, \ldots, y_T): the label sequence of length TT
  • x\mathbf{x}: the observation sequence (features at each position)
  • fk(yt−1,yt,x,t)f_k(y_{t-1}, y_t, \mathbf{x}, t): the kk-th feature function, which examines the previous label yt−1y_{t-1}, current label yty_t, the full observation sequence x\mathbf{x}, and position tt
  • λk\lambda_k: the weight for feature kk (what we're learning)
  • Z(x)Z(\mathbf{x}): the partition function, which sums over all possible label sequences to normalize probabilities

The partition function deserves special attention:

Z(x)=∑y′exp⁡(∑t=1T∑kλkfk(yt−1′,yt′,x,t))Z(\mathbf{x}) = \sum_{\mathbf{y}'} \exp\left(\sum_{t=1}^{T} \sum_{k} \lambda_k f_k(y'_{t-1}, y'_t, \mathbf{x}, t)\right)

where:

  • y′\mathbf{y}': ranges over all possible label sequences
  • The sum includes ∣L∣T|L|^T sequences, where ∣L∣|L| is the number of possible labels

This summation over exponentially many sequences is what makes CRF training computationally interesting. With 9 labels and 20 tokens, we'd have 920≈10199^{20} \approx 10^{19} possible sequences. Enumerating them directly is impossible. Even if each evaluation took a nanosecond, computing the partition function by brute force would take longer than the age of the universe. The forward-backward algorithm, covered in the next section, is how we escape this combinatorial trap.

The Objective Function

Taking the log of the conditional probability gives us the log-likelihood for a single example:

log⁡P(y∣x)=∑t=1T∑kλkfk(yt−1,yt,x,t)−log⁡Z(x)\log P(\mathbf{y} | \mathbf{x}) = \sum_{t=1}^{T} \sum_{k} \lambda_k f_k(y_{t-1}, y_t, \mathbf{x}, t) - \log Z(\mathbf{x})

where:

  • The first term is the score of the correct label sequence, a simple weighted sum of features that fire along the correct path
  • The second term, log⁡Z(x)\log Z(\mathbf{x}), is the log of the partition function and represents the log-sum-exp of scores over all possible sequences

The first term is cheap to compute: just walk along the correct label sequence and sum the feature weights at each step. The second term is the bottleneck. It requires, at least conceptually, evaluating the score of every possible label sequence and then computing a softmax-like normalization. This is where the forward-backward algorithm earns its keep.

Notice that the objective is a difference of two quantities. The first rewards the correct sequence by adding its score. The second penalizes all sequences by subtracting the log of their collective scored mass. Maximizing this difference means pushing the correct sequence score up while keeping the partition function from growing too large, which is exactly the behavior you want from a probabilistic classifier.

For the full training set with NN examples, we sum the log-likelihoods:

L(λ)=∑i=1Nlog⁡P(y(i)∣x(i))\mathcal{L}(\boldsymbol{\lambda}) = \sum_{i=1}^{N} \log P(\mathbf{y}^{(i)} | \mathbf{x}^{(i)})

where:

  • L(λ)\mathcal{L}(\boldsymbol{\lambda}): the total log-likelihood as a function of all weights
  • λ=(λ1,λ2,…,λK)\boldsymbol{\lambda} = (\lambda_1, \lambda_2, \ldots, \lambda_K): the vector of all KK feature weights

Our goal is to find λ∗=arg⁡max⁡λL(λ)\boldsymbol{\lambda}^* = \arg\max_{\boldsymbol{\lambda}} \mathcal{L}(\boldsymbol{\lambda}).

Let's visualize what this objective looks like for a simple case:

Out[3]:
Visualization
3D surface plot showing a concave log-likelihood function with two weight parameters, which demonstrates the smooth optimization landscape.
Log-likelihood surface for a CRF with two weights. The surface is concave (bowl-shaped when negated), guaranteeing a unique global maximum. The contours show how the log-likelihood changes as we adjust the weights. Gradient-based optimization follows the contours uphill toward the optimum.

The log-likelihood function for CRFs has a important property: it is concave. This means there are no local maxima to get trapped in. Any hill-climbing algorithm will find the global optimum given enough iterations. This mathematical guarantee makes CRF training well-behaved compared to neural network training, where the loss surface contains local minima and saddle points.

The concavity follows from the log-sum-exp structure of the partition function. The log-sum-exp of linear functions is convex, so subtracting it from a linear function yields a concave result. In practice, this means you can initialize weights to zero and trust that any reasonable optimizer will converge to the same solution regardless of starting point. When your CRF training converges poorly, the culprit is almost always numerical issues, inappropriate learning rates, or degenerate features rather than getting stuck in a bad local optimum.

Historical Context: Maximum Entropy and CRFs

The log-likelihood objective for CRFs is deeply related to maximum entropy models, which were popular in NLP throughout the 1990s and early 2000s. A maximum entropy model (also called a log-linear model or multinomial logistic regression) assigns the distribution over labels that is as uniform as possible while matching the empirical feature expectations observed in training data. Lafferty, McCallum, and Pereira's landmark 2001 paper "Conditional Random Fields: Probabilistic Models for Segmenting and Labeling Sequence Data" extended this maximum entropy philosophy to sequential outputs by allowing label transitions to appear as features. The result was a structured model that inherited the convex training objective of maximum entropy classifiers while capturing the dependencies that positional classifiers ignore.

Worked Example: A Two-Position CRF

Consider a minimal CRF with two positions and two labels: P (person) and O (other). Suppose we have two features: f1=1f_1 = 1 if the current word is capitalized and the current label is P, and f2=1f_2 = 1 if the transition is P→\toO. With weights λ1=2\lambda_1 = 2 and λ2=1\lambda_2 = 1, and input tokens ["Alice", "runs"], the correct label sequence is [P, O].

The score of the correct sequence [P, O] is 2⋅1+1⋅1=32 \cdot 1 + 1 \cdot 1 = 3 (feature 1 fires for "Alice"/P, feature 2 fires for the P→\toO transition). The score of [O, P] is 0 (neither feature fires). The score of [P, P] is 2 (only feature 1 fires). The score of [O, O] is 0. The partition function Z=e3+e0+e2+e0≈20.09+1+7.39+1=29.48Z = e^3 + e^0 + e^2 + e^0 \approx 20.09 + 1 + 7.39 + 1 = 29.48. The probability of the correct sequence is e3/29.48≈0.68e^3 / 29.48 \approx 0.68, so its log-likelihood is approximately −0.39-0.39. If we increase λ1\lambda_1 to 3, the correct sequence gets more probability and the log-likelihood improves.