CRF Training › The Log-Likelihood Objective
Language AI HandbookAll chapters 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 and their correct label sequences , 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 , we model 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 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.
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 and label sequence , the CRF defines the conditional probability as:
where:
- : the label sequence of length
- : the observation sequence (features at each position)
- : the -th feature function, which examines the previous label , current label , the full observation sequence , and position
- : the weight for feature (what we're learning)
- : the partition function, which sums over all possible label sequences to normalize probabilities
The partition function deserves special attention:
where:
- : ranges over all possible label sequences
- The sum includes sequences, where 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 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:
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, , 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 examples, we sum the log-likelihoods:
where:
- : the total log-likelihood as a function of all weights
- : the vector of all feature weights
Our goal is to find .
Let's visualize what this objective looks like for a simple case:

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.
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.
Consider a minimal CRF with two positions and two labels: P (person) and O (other). Suppose we have two features: if the current word is capitalized and the current label is P, and if the transition is PO. With weights and , and input tokens ["Alice", "runs"], the correct label sequence is [P, O].
The score of the correct sequence [P, O] is (feature 1 fires for "Alice"/P, feature 2 fires for the PO 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 . The probability of the correct sequence is , so its log-likelihood is approximately . If we increase to 3, the correct sequence gets more probability and the log-likelihood improves.