Sub topic 8
Fine-Tuned Token Classifiers and BIO Tags
This chapter assumes that you know basic Python, JSON, language-model concepts, and introductory precision, recall, and F1. It focuses on token classification: training a model to assign a label to each token in a text.
What a token classifier does
A token-classification model reads text as a sequence of tokens. A token is usually a word, part of a word, punctuation mark, or special symbol. The model assigns one label to each token.
For named-entity recognition (NER), labels might include PERSON, ORG, and LOCATION. For invoice extraction, labels might include VENDOR, TOTAL, and DATE.
The model does not directly return a JSON object or a list of complete entities. It first predicts token labels. A post-processing step combines those labels into spans, which are character or token ranges representing extracted values.
BIO tagging
BIO is a common labeling scheme for marking spans:
B-Xmeans the beginning of an entity of typeX.I-Xmeans a continuation token inside an entity of typeX.Omeans the token is outside every entity.
For example, the sentence Angela Merkel visited Paris can be labeled as follows:
| Token | Label |
|---|---|
| Angela | B-PER |
| Merkel | I-PER |
| visited | O |
| Paris | B-LOC |
The labels show that Angela Merkel is one PER span and Paris is one LOC span. A new B- label starts a new entity, even when it follows an I- label for another type.
Some datasets use BILOU or BMEWO instead. These schemes distinguish single-token and final tokens more explicitly. Use the scheme expected by the model and evaluation code. Do not mix label conventions across training and scoring.
Decoding and scoring spans
A decoder walks through the labels and creates a span when it sees B-X. It continues while the following labels are compatible I-X tags. Inconsistent sequences need a policy. For example, an I-PER without a preceding B-PER can be treated as a new PER span or ignored.
Exact-match span evaluation requires both the boundaries and the label to match. If the gold span is New York and the model predicts only New, the prediction is incorrect even though it identifies the right general location.
def bio_to_spans(tags):
spans = []
start = None
label = None
for i, tag in enumerate(tags + ["O"]):
continuing = (
start is not None
and tag.startswith("I-")
and tag[2:] == label
)
if start is not None and not continuing:
spans.append((start, i, label))
start = None
label = None
if tag.startswith("B-"):
start = i
label = tag[2:]
elif tag.startswith("I-") and start is None:
# Repair an orphan I-tag as a new span.
start = i
label = tag[2:]
return spans
tags = ["B-PER", "I-PER", "O", "B-LOC"]
print(bio_to_spans(tags))This prints:
[(0, 2, 'PER'), (3, 4, 'LOC')]The end index is exclusive, so (0, 2, "PER") covers tokens at positions 0 and 1.
Fine-tuning workflow
Fine-tuning starts with documents whose tokens have gold BIO labels. The model’s original language representation is adjusted using these examples. During training, it learns patterns such as capitalization, surrounding words, token order, and domain-specific terminology.
A typical workflow is:
- Define the entity labels and BIO rules.
- Tokenize each document.
- Align word-level labels with the tokenizer’s subword tokens.
- Train on the labeled examples.
- Decode predicted tags into spans.
- Evaluate on documents kept out of training.
Subword alignment is a common source of bugs. If Acme becomes one token, alignment is simple. A rare word might split into several pieces. Usually the first piece receives the B- or I- label, later pieces receive compatible I- labels, and special tokens receive an ignored label during loss calculation.
Exercise
The gold labels are ["B-ORG", "I-ORG", "O", "B-LOC"], but the model predicts ["B-ORG", "O", "O", "B-LOC"]. Which entity is wrong under exact-match span scoring?
Show answer
The ORG entity is wrong because the model predicted only the first token. The LOC entity is correct.