Named Entity Recognition
A tagger that learns names by their spelling cannot find a name it never saw. What generalizes is context, provided training removes the name the way test-time novelty does.
Overview & Motivation
A filing says “Northgate Energy guided to 7 percent growth in fiscal 2026.” An information-extraction system has to return three typed spans: Northgate Energy, an organization; 7 percent, a percentage; fiscal 2026, a date. The sequence labeling topic turned that into one BIO tag per token and tagged the corpus’s entities with a linear-chain CRF. Its finance case study ended on a warning: almost every error it had left was a name the tagger had never seen in training.
This page takes that warning as its subject. We split the corpus’s companies in two. The taggers train on documents about companies and are tested on documents about those and on documents about others, whose organizations, people, and products never appear in training. On the held-out documents, the sequence labeling topic’s CRF does not find a single organization. Read the lab’s sentence, from the held-out test: “northgate energy guided to percent growth in fiscal”. The word-feature CRF finds the percentage and the date and tags northgate energy as outside every entity.
Why that happens, and what fixes it, needs four pieces of mathematics first:
- Encodings. Tags are one way to write a set of spans. We prove which tag sequences are valid, and when BIOES is more expressive than BIO.
- Constrained decoding. Decoding only valid sequences is exactly conditioning the model on validity.
- A CRF on a contextual encoder. The masked-LM topic’s encoder supplies the features, and the CRF’s gradient tells the encoder what to change.
- Span evaluation and F1-optimal decoding. What span F1 averages, and how to decode the span set that maximizes it.
Then the held-out names. The fix that works is old, word dropout, but only in one of its two forms, and we prove why the other fails.
Spans and their encodings
A sentence has tokens , and is the set of entity types: here ORG, PERSON, GPE (places), PRODUCT, SECTOR, DATE, MONEY, and PERCENT.
Definition 1 (Span set).
A span covers the tokens and has type . A span set is a set of pairwise disjoint spans. A recognizer maps a sentence to a span set.
A tagger cannot output a set directly, so we encode the set as one tag per token.
Definition 2 (BIO and BIOES).
BIO tags the first token of a span B-, its other tokens I-, and every other token O: tags. BIOES tags a one-token span S- and a longer span B-, I-, , I-, E-, with O elsewhere: tags.
The lab’s sentence is northgate/B-ORG energy/I-ORG guided/O to/O percent/B-PERCENT growth/O in/O fiscal/B-DATE in BIO. In BIOES the organization ends on E-ORG, and the one-word percentage and date become S-PERCENT and S-DATE. (The tokenizer keeps letters only, so “7” and “2026” are gone.)
Not every tag sequence encodes a span set. “O I-ORG” continues a span that never began, and in BIOES so does “B-ORG O”: a span that opens and never closes. Picture all sequences as paths through a trellis. The valid ones are the paths that avoid certain starts, certain steps, and certain ends.
Proposition 1 (Encodings are bijections onto first-order languages).
For each scheme, the encoding is a bijection from the span sets on tokens onto the valid tag sequences, and a sequence is valid exactly when its first tag passes a start mask, each adjacent pair passes a transition mask, and its last tag passes an end mask:
- BIO: I- may follow only B- or I-, so it may not start a sentence; every tag may end one.
- BIOES: after B- or I- only I- or E- may follow; after O, E, or S only O, B, or S may follow; a sentence starts on O, B, or S and ends on O, E, or S.
With the 0–1 transition matrix and the start and end masks, the number of valid sequences is , where and . Reading tags left to right, opening a span at any tag that does not continue an open span of its type (the CoNLL scorer’s convention), maps all sequences onto the span sets, many to one.
Proof.
Decoding a valid sequence reads each span back: a B or S tag opens a span, I continues it, and in BIO the next tag that does not continue it closes it, while in BIOES an E or S closes it. So decoding inverts encoding on valid sequences, and encoding is injective. Conversely, every pattern the masks forbid is one that no encoding produces (an I or E with nothing open, a B or I followed by something other than I or E, a span still open at the end), so the valid sequences are exactly the images. The mask conditions involve only one tag or two adjacent tags, which is what first-order means, and counts the paths of length through the 0–1 graph of allowed steps. For the recursion, look at token : either it lies outside every span, leaving a span set on tokens, or it ends a span of length and one of types, leaving a span set on tokens. The repair reads some span set off every sequence, and it reads the same set off, for example, “O I-ORG” and “O B-ORG”.
∎With all eight types, tokens have span sets, against BIO sequences and BIOES sequences. The harness checks the bijection, the mask characterization, and the count by enumerating every sequence for one and two types up to .
BIOES carries more information per tag: whether a span ends here. Does that make it a richer model?
Proposition 2 (BIO against BIOES).
(a) For every BIO chain with potentials , the BIOES chain , , where maps S- to B- and E- to I-, gives every span set the same probability, both chains constrained to valid sequences.
(b) With token-local features, where depends on the sentence only through , the inclusion is strict. Take two training sentences, “a d” with gold span set and “c d” with . Every BIO chain gives the two gold span sets total probability at most , while BIOES chains give each one probability as close to as we like.
(c) If each token’s features include the word to its left, BIO chains also approach probability on both.
Proof.
(a) The map sends each valid BIOES sequence to the valid BIO sequence of the same span set, so it is a bijection between the two valid sets (Proposition 1), and by construction the score of is the score of . Equal scores over sets in bijection give equal probabilities after normalizing.
(b) Both gold span sets need . Given , the chain’s conditional law of is proportional to , which depends on the sentence only through , and both sentences have . Call that law . The first gold set needs and the second needs , so their probabilities are at most and , which sum to at most . In BIOES the two sentences need different first tags, S for “a” and B for “c”, which token-local weights can separate, and after B the masks force E.
(c) A feature “the left word is ” on token lets differ between the sentences, which breaks the argument in (b).
∎Fit by penalized maximum likelihood, the token-local BIO chain gives the two gold sets and ; BIOES gives and ; BIO with the left word gives and . Ratinov and Roth (2009) found BILOU, their name for BIOES, better than BIO in practice. Proposition 2 says what such a finding can and cannot mean: once the features see a token’s neighbors, as every tagger below does, the two schemes describe the same span distributions, and any difference is estimation, with twice the tags to estimate.
Constrained decoding
A CRF trained on valid sequences still gives invalid ones positive probability, and its Viterbi path can be invalid. The fix is to forbid them, and the next theorem says what forbidding computes.
Theorem 1 (Constrained decoding is conditioning).
Let be the valid sequences of Proposition 1, and modify a chain’s log-potentials by setting for every tag the start mask forbids, for every tag the end mask forbids, and for every forbidden step. Then the sequence recursions of the sequence labeling topic, run on the modified chain, return
(a) the Viterbi path ;
(b) the node marginals ;
(c) , where is the modified chain’s partition function.
Proof.
The modification multiplies each path’s potential by
a product of factors on single nodes and adjacent pairs. So the modified potentials are again a chain, and the sequence labeling topic’s Theorem 1 applies to it unchanged: its max pass returns the best path with nonzero potential, which is the best valid path; its sum pass returns and marginals of the distribution proportional to , which is . Finally .
∎Two things follow. Constrained Viterbi is not a heuristic repair; it is the exact mode of the model conditioned on producing a span set. And the cost is nothing: the same passes with some entries set to . The harness checks all three parts against enumeration on random chains in both schemes. Every CRF tagger below decodes an invalid sequence in fewer than of test sentences without the constraint, so for CRFs the constraint rarely binds. For the independent softmax head of the next section it binds constantly.
A CRF on a contextual encoder
The sequence labeling topic scored each token with hand-built features: the word, its prefix and suffix, its neighbors. The masked-LM topic trained an encoder whose final state at each position summarizes the whole sentence around that token. We put the CRF on top of it.
Definition 3 (A CRF on encoder states).
Let be an encoder’s final state at token . The encoder CRF has unary potentials , transition weights , and start and end weights, and with the start and end weights folded into and . The softmax head is the case with no start or end weights: an independent classifier at each token.
Theorem 2 (The gradient at the potentials).
Write and . Then
So the loss sends the encoder , where is the indicator vector of the gold tag. For the softmax head, is the per-token softmax and this is the cross-entropy gradient.
Proof.
. The score is linear in the potentials, with derivative in and the count of steps in . For , the derivative in is , and in it is the expected number of steps, . This is the sequence labeling topic’s Theorem 2 with the potentials themselves as the parameters. The chain rule through gives the encoder’s gradient. With and no start or end weights the chain factorizes over tokens, so is the softmax of .
∎The vector is “what the model believes minus what is true,” position by position, and Collobert et al. (2011) trained taggers end to end on exactly this sentence-level objective. The harness checks Theorem 2 by complex-step differentiation on random chains and checks its chain rule through the masked-LM encoder, fed into the fine-tuning topic’s backward pass, by central differences on its weights. On this page we keep the encoder frozen: its states are fixed features, and only the CRF is trained.
Corollary 1 (Frozen states keep the objective strictly concave).
With the encoder fixed, the penalized conditional log-likelihood of an encoder CRF, in and the start and end weights, is strictly concave for every and has exactly one maximizer.
Proof.
Fixed states make every feature a constant, so the encoder CRF is the sequence labeling topic’s linear-chain CRF with dense real-valued features. Its Theorem 2 gives a negative semidefinite Hessian, and the penalty subtracts (formalML convex-analysis, Theorem 3, for the second-order condition). The objective grows to in every direction, so its maximum is attained, at one point.
Fine-tuning gives this up: the score becomes a non-convex function of the encoder’s weights. Devlin et al. (2019) fine-tune BERT for the same task with a softmax head and no CRF, so whether a CRF still helps a fine-tuned encoder is a fair question, and one this page leaves open. On frozen states the answer is clear (the Heads & constraints tab). The softmax head decodes an invalid BIO sequence in of the seen-roster test sentences and of the held-out ones. Theorem 1’s constraint removes those and raises its span F1 from to (seen) and from to (held out). The CRF head on the same states scores and . The frozen states know a token is part of a name; they were never trained to know where the name ends, and the transition weights supply that.
Span evaluation in depth
The sequence labeling topic scored taggers by exact-match span F1, the CoNLL-2003 convention (Tjong Kim Sang and De Meulder 2003). That is one point in a small space of choices.
Definition 4 (Match criteria and span F1).
Fix a match relation between a gold span and a predicted span of the same sentence:
- strict: the same boundaries and type;
- boundary: the same boundaries, any type;
- type: overlapping, with the same type;
- partial: overlapping.
Precision is the share of predicted spans that match some gold span, recall the share of gold spans that some predicted span matches, and F1 their harmonic mean, . Under strict matching both shares count the same pairs, so , with and the gold and predicted spans. Per-type F1 restricts both sets to one type.
These are the four criteria of the SemEval-2013 drug-interaction task (Segura-Bedmar, Martínez, and Herrero-Zazo 2013, §3.1), which defers the crediting of partial matches to the MUC scoring conventions; we count any overlap as a match. Two facts about them are worth stating precisely.
Proposition 3 (What span F1 averages, and relaxing the match).
(a) Under strict matching, the micro F1 over all types is a mixture of the per-type F1s,
with weights that depend on the predictions. Macro F1 is the mixture with equal weights.
(b) If every pair that matches under one relation matches under a second, then precision, recall, and F1 under the second are at least their values under the first. So strict boundary partial and strict type partial, in all three measures.
Proof.
(a) , so ; summing over gives , because the types partition both sets. (b) A predicted span matched under the first relation is matched under the second, so precision can only rise, and likewise recall. F1 increases in each of and .
∎Part (a) is a warning about reading micro F1. A tagger that predicts many spans of an easy type raises that type’s weight, and with it the micro score, even if its F1 on the hard types stays put. Here MONEY and PERCENT are read off one word each (million, percent), so every tagger scores near on them, and organizations are the hard type: per-type F1 is the number to watch. Part (b) makes the gaps between criteria diagnostic. The encoder CRF on the held-out test scores strict but under type matching: it finds the organizations and misplaces their boundaries, an error a strict score alone cannot separate from missing them. The Held-out names tab switches between the four criteria.
F1-optimal span decoding
Viterbi decoding maximizes the probability of the whole tag sequence (the sequence labeling topic’s Theorem 4), and span F1 is not that. Can we decode for F1 directly? The first ingredient is the probability of each span.
Proposition 4 (Span marginals).
Let and be the forward and backward messages of a constrained chain (Theorem 1), and write and . In BIO, the probability that the span set contains is
where each links the tag before it to the next, and the last factor is when is the last token. In BIOES the span is S- at , or B-, I-, , E-, closed by . The marginals sum to the expected number of spans, .
Proof.
In BIO a valid sequence contains the span exactly when , , and (or the sentence ends). Summing the potentials of all such paths factorizes as in the sequence labeling topic’s Theorem 1: prefixes ending in B- at give , the pinned middle contributes its product, and suffixes starting with any at give the last sum. BIOES is the same with its own pinned pattern. Each span begins with exactly one B or S tag, so the number of spans is the number of such tags, and expectations add.
∎Now the objective. With marginals over a whole test corpus and , a predicted span set has expected true positives and the gold set has expected size , so the F1 of expected counts is .
Theorem 3 (F1-optimal span decoding).
Maximize over span sets, one per sentence.
(a) if and only if .
(b) Starting from , the iteration , increases strictly until it stops, after finitely many steps, at , and is then optimal.
(c) The inner maximization chooses disjoint spans to maximize , a weighted interval scheduling problem solved per sentence by a dynamic program over token positions. So the optimal span set thresholds the marginals at , subject to disjointness. When the candidate spans are pairwise disjoint it is exactly .
Proof.
(a) For every , is equivalent to , since the denominator is positive, and . So exactly when , with equality at because the maximizing set attains it. This is the characterization of a ratio’s maximum that Dinkelbach (1967) restates from Jagannathan and turns into an algorithm.
(b) , using . If , the set has , which says : a strict increase. If , then by (a). There are finitely many span sets, so the values cannot rise forever.
(c) For fixed the objective adds one weight per chosen span, and the only constraint is that spans in one sentence be disjoint. Let be the best total over spans inside the first tokens. Then , and with its back-pointers is the optimum. A span with weight at most never helps, so only spans with are chosen. With disjoint candidates there is no conflict, and the set is all of them.
∎Lipton, Elkan, and Naryanaswamy (2014, Corollary 1) proved the threshold for binary decisions with calibrated probabilities, F1 taken as a ratio of population rates; spans add the disjointness constraint, which the interval program handles. One more fact makes the program cheap.
Remark (Above one half, no conflicts).
Two overlapping spans cannot both belong to one span set, so they are disjoint events and . Two candidates above a threshold therefore never overlap, and thresholding at needs no dynamic program at all.
Two caveats belong next to the theorem. is the F1 of expected counts, not the expected F1, and the two agree only asymptotically, on large test sets (Ye et al. 2012, Theorem 8). And the theorem optimizes the model’s own beliefs. If the marginals are calibrated, in formalML’s sense of uncertainty-quantification (its Definition 3), is a forecast of the realized F1 and the right threshold; if not, neither is. The calibration topic is about measuring exactly that gap, and the held-out names below open one. The harness checks Theorem 3 against brute force over every combination of disjoint span subsets on random multi-sentence instances, and the threshold form against all subsets of ten disjoint candidates.
Holding names out by design
Strict F1 per type (bars). The word CRF predicts no organization anywhere in the held-out test. Relax the match to see boundary errors: a tagger that finds a name but misplaces its edges gains under “type” and “partial” (Proposition 3). 35 of the 49 held-out name words were in the encoder’s pretraining vocabulary.
Where this sits
The sequence labeling topic built the chain; this page fixed its output language (Proposition 1, Theorem 1), its features (a frozen contextual encoder), its metric (Proposition 3, Theorem 3), and its training data (name dropout, Proposition 5). The spans it returns are the input of the next topic, relation extraction, which asks which pairs of recognized entities a sentence relates. Its distant supervision reads labels off the corpus’s knowledge base, so its errors start where this page’s end: a missed organization is a relation that can never be found.
Honest caveats
Implementation
The companion notebook (notebookPath) imports named_entity_recognition.py, which owns every number on this page. It imports the sequence labeling topic’s module for the chain recursions, the CRF objective and its L-BFGS fit, the finance BIO sentences, and the word features, and the fine-tuning topic’s module for the masked-LM encoder, its fast forward pass, and its backward pass. Its harness checks:
- Proposition 1’s bijection, masks, and count, and Proposition 2’s relabeling and counterexample, by enumeration;
- Theorem 1’s three parts and Proposition 4’s span marginals against brute force on random chains in both schemes;
- Theorem 2 by complex step on chains and by central differences through the encoder, and Corollary 1’s unique optimum from two starts;
- Theorem 3 against every combination of disjoint span subsets, and the threshold on disjoint candidates;
- Proposition 3 on random span sets and on every tagger, and Proposition 5 on a toy fit and on the substitution-trained tagger;
- every comparison this page states, on both test sets and both encoder readings.
The laboratory reads its data from a JSON file that one of the harness’s tests requires to equal the module’s viz_payload() exactly, so the figure cannot drift from the code.
Connections
- Theorem 3's F1-optimal decoder is optimal only for calibrated span marginals; the held-out names miscalibrate them, and the calibration topic's reliability analysis is how one would measure that. language-model-calibration
- Its encoder backward pass is the one Theorem 2 feeds with gold indicators minus marginals; this page keeps the encoder frozen, and fine-tuning it through the CRF is the experiment it leaves open. fine-tuning-low-rank-adaptation
References & Further Reading
- paper Introduction to the CoNLL-2003 Shared Task: Language-Independent Named Entity Recognition — Erik F. Tjong Kim Sang and Fien De Meulder (2003) Proceedings of CoNLL-2003, 142–47. The benchmark and its scoring: a phrase counts only if it matches a gold phrase exactly (strict span F1)
- paper Design Challenges and Misconceptions in Named Entity Recognition — Lev Ratinov and Dan Roth (2009) Proceedings of CoNLL-2009, 147–55. Section 4 and Table 2 compare BIO with BILOU (BIOES): BILOU wins on three of four evaluation sets
- paper Neural Architectures for Named Entity Recognition — Guillaume Lample, Miguel Ballesteros, Sandeep Subramanian, Kazuya Kawakami, and Chris Dyer (2016) Proceedings of NAACL-HLT 2016, 260–70. A BiLSTM with a CRF output layer; §4.1 replaces singletons with an unknown-word embedding with probability 0.5 in training; §2.4 finds IOBES no significant improvement over IOB
- paper Natural Language Processing (Almost) from Scratch — Ronan Collobert, Jason Weston, Léon Bottou, Michael Karlen, Koray Kavukcuoglu, and Pavel Kuksa (2011) Journal of Machine Learning Research 12: 2493–537. Section 3.4.2, the sentence-level log-likelihood: transition scores added to a network's per-token scores, normalized over all tag paths, trained end to end
- paper BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding — Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova (2019) Proceedings of NAACL-HLT 2019, 4171–86. Section 5.3: CoNLL-2003 NER as tagging, the first sub-token's representation fed to a token-level classifier with no CRF layer
- paper Generalisation in Named Entity Recognition: A Quantitative Analysis — Isabelle Augenstein, Leon Derczynski, and Kalina Bontcheva (2017) Computer Speech & Language 44: 61–83. Leading recognizers rely on surface forms seen in training, and score markedly lower on unseen entities, mostly through recall
- paper SemEval-2013 Task 9: Extraction of Drug-Drug Interactions from Biomedical Texts (DDIExtraction 2013) — Isabel Segura-Bedmar, Paloma Martínez, and María Herrero-Zazo (2013) Proceedings of SemEval 2013, 341–50. Section 3.1: the four entity-matching criteria, strict, exact boundary, partial boundary, and type
- paper On Nonlinear Fractional Programming — Werner Dinkelbach (1967) Management Science 13, no. 7: 492–98. Restates and proves Jagannathan's (1966) theorem relating fractional and parametric programs, and gives the iteration; cited from the abstract (the full text was not accessible)
- paper Optimal Thresholding of Classifiers to Maximize F1 Measure — Zachary C. Lipton, Charles Elkan, and Balakrishnan Naryanaswamy (2014) Machine Learning and Knowledge Discovery in Databases: ECML PKDD 2014, Part II, Lecture Notes in Computer Science 8725, 225–39. Corollary 1: with calibrated probabilities, the F1-optimal threshold is half the optimal F1 (population F1, a ratio of rates)
- paper Optimizing F-Measures: A Tale of Two Approaches — Nan Ye, Kian Ming A. Chai, Wee Sun Lee, and Hai Leong Chieu (2012) Proceedings of the 29th ICML. Theorem 8: empirical utility maximization and the decision-theoretic approach to F-measures are asymptotically equivalent on large i.i.d. test sets