Named Entity Recognition
Named Entity Recognition (NER) is an NLP task that locates and classifies named entities in text — such as people, organizations, locations, and dates — into predefined categories. It matters because it is a foundational building block for information extraction pipelines, powering everything from search engines to clinical decision-support systems. A counterintuitive fact: a model can achieve 90%+ accuracy on standard benchmarks while completely failing on a single domain shift, such as moving from news articles to medical records, because entity boundaries and categories are deeply context-dependent.
NER is fundamentally a sequence labeling problem: instead of assigning one label to an entire sentence, the model assigns a label to every single token (word or sub-word). The "aha moment" is that adjacent token labels are not independent — whether a token is the continuation of an entity depends heavily on what the previous token was labeled. This is why almost every effective NER approach models the sequence as a whole rather than token by token in isolation.
How It Works
The BIO Tagging Scheme
The standard way to turn span detection into a sequence labeling task is the BIO scheme (sometimes called IOB2):
| Tag | Meaning |
|---|---|
| `B-TYPE` | Beginning of an entity of this type |
| `I-TYPE` | Inside (continuation) of an entity of this type |
| `O` | Outside — not part of any entity |
For example: ``` Sentence: Barack Obama visited Berlin yesterday Tags: B-PER I-PER O B-LOC O ``` This encodes that "Barack Obama" is a single PERSON entity and "Berlin" is a LOCATION entity.
Scoring: The CRF Layer
A Conditional Random Field (CRF) layer is placed on top of a neural encoder to model the probability of an entire tag sequence given the input tokens. The key equation is:
Explanation: The probability of the entire label sequence y given the input x is proportional to the exponentiated sum of "compatibility scores" at each position, normalized by Z(x), the sum over all possible label sequences.
Symbols:
- — the predicted tag sequence (e.g., `[B-PER, I-PER, O, B-LOC, O]`)
- — the input token sequence
- — total number of tokens
- — a score for transitioning from label to label at position
- — the partition function (normalization constant ensuring probabilities sum to 1)
The CRF learns that transitions like `I-PER → B-LOC` are valid, while illegal transitions like `O → I-PER` (starting inside an entity without a B- tag) get very low scores.
Feature Extraction: From Characters to Contextual Embeddings
Explanation: The hidden state for token t is produced by running all tokens through an encoder simultaneously, so every token's representation can "see" the full surrounding context. In practice the encoder is a BiLSTM, BERT, or similar model. The hidden state feeds into the CRF's scoring function.
Training Loss
NER models are trained by minimizing the negative log-likelihood of the correct tag sequence:
Explanation: We want to maximize the probability of the correct label sequence y, which is equivalent to minimizing its negative log probability.*
A Minimal Python Example
```python
# Using the popular `transformers` + `datasets` libraries from HuggingFace from datasets import load_dataset from transformers import ( AutoTokenizer, AutoModelForTokenClassification, pipeline )
# 1. Load a pre-trained NER model (fine-tuned BERT on CoNLL-2003) model_name = "dslim/bert-base-NER" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForTokenClassification.from_pretrained(model_name)
# 2. Create an NER pipeline ner = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
# 3. Run inference text = "Elon Musk founded SpaceX in Hawthorne, California in 2002." results = ner(text) for entity in results: print(f"{entity['word']:20s} → {entity['entity_group']:6s} " f"(score: {entity['score']:.2f})")
# Output:
# Elon Musk → PER (score: 0.99)
# SpaceX → ORG (score: 0.98)
# Hawthorne → LOC (score: 0.97)
# California → LOC (score: 0.99) ``` ```python
# Training your own NER model from scratch (sketch) from transformers import Trainer, TrainingArguments training_args = TrainingArguments( output_dir="./ner-model", num_train_epochs=3, per_device_train_batch_size=16, evaluation_strategy="epoch", learning_rate=2e-5, # standard fine-tuning LR for BERT )
# `model` is AutoModelForTokenClassification initialized with your label set
# `tokenized_dataset` maps raw text + BIO tags to token IDs trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_dataset["train"], eval_dataset=tokenized_dataset["validation"], ) trainer.train() ```
Erik Tjong Kim Sang is a Dutch computational linguist whose most lasting contribution to NER was co-organizing the CoNLL-2002 and CoNLL-2003 shared tasks, which produced the benchmark datasets still used to evaluate NER systems today. He introduced the standardized evaluation metric of entity-level F1 score — requiring both the span boundaries and the entity type to be correct — raising the bar from simple token accuracy. Interestingly, the CoNLL-2003 English dataset was built from Reuters newswire, a narrow domain that later research showed causes surprisingly large drops when models are applied to other genres.
Intuition
Imagine you are a detective reading a crime report and highlighting people, places, and organizations with different color markers. You don't read one word at a time in isolation — you scan the full sentence, use context clues ("Mr." suggests a person is coming, "Inc." suggests an organization just ended), and apply consistent rules about spans (you always highlight the full name "New York City", not just "New"). NER works exactly the same way: the BiLSTM gives every token a "full sentence awareness" by reading left-to-right and right-to-left, and the CRF layer enforces the consistency rules (you can't suddenly be "inside" a person span without first "beginning" one). The BIO tags are your different colored markers, and the model learns which color to use based on context — not just the word itself.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
Sequence Labeling · Part-of-Speech Tagging · Relation Extraction · Transformer Models · Text Classification