Sub topic 7
Subword Tokenization and Its Effect on NER
This chapter assumes that you know basic Python, have seen tokens and BIO tags, and understand that a neural network classifier predicts labels from input features.
Why transformers split words
A tokenizer converts text into the units a transformer encoder can process. Older systems often use one token per word. Transformer models commonly use subword tokenization instead. A long or uncommon word is split into smaller pieces that the model has seen in other words.
For example, a WordPiece tokenizer might represent these words as follows:
| Input word | Possible subword tokens |
|---|---|
| playing | play, ##ing |
| Microsoft | Microsoft |
| unrecognized | un, ##recogn, ##ized |
The exact result depends on the model's vocabulary. A prefix such as ## usually means that the piece continues the previous token. Some tokenizers use different markers, such as a leading space marker.
Subword vocabularies help a model handle new words. The model may not know the complete word unrecognized, but it can still use representations for un, recogn, and ized. This is useful for names, product identifiers, and technical terms.
What changes for NER
Named entity recognition assigns a label to an entity span. A transformer token-classification model instead produces one label for each tokenizer output position. Therefore, one original word can produce several predictions.
Consider this sentence:
The company hired Maria González.A tokenizer might produce:
The company hired Maria Gon ##zález .The person name occupies two subword positions. The training data, however, may contain one word-level label:
The/O company/O hired/O Maria/B-PER González/I-PERThe training process must decide how to transfer the word-level label to the subwords. A common policy assigns the label only to the first subword and ignores later subwords when calculating the loss:
The/O company/O hired/O Maria/B-PER Gon/I-PER ##zález/-100 ./OThe value -100 is commonly used by PyTorch loss functions to mean “ignore this position.” Another policy repeats the label across every subword. Either policy can work, but training and evaluation must use the same convention.
Inspecting a tokenizer
The Hugging Face tokenizer exposes the connection between subwords and original words through word IDs. Special tokens, such as the beginning and ending markers used by some models, have no word ID.
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
words = ["Maria", "González", "joined", "Acme"]
encoded = tokenizer(
words,
is_split_into_words=True,
return_attention_mask=False,
)
print(tokenizer.convert_ids_to_tokens(encoded["input_ids"]))
print(encoded.word_ids())A possible result includes a list like:
['[CLS]', 'Maria', 'Gonz', '##ález', 'joined', 'Ac', '##me', '[SEP]']
[None, 0, 1, 1, 2, 3, 3, None]The repeated word ID shows that Gonz and ##ález came from one original word. The result can differ between tokenizer versions, so inspect the actual output rather than assuming a fixed split.
Common NER errors
Subword tokenization creates several failure modes:
- A model may label only part of a name correctly.
- A boundary may fall inside a product code or organization name.
- A word split into many pieces receives less consistent context.
- Evaluation can count a partially correct entity as completely wrong.
- Character offsets from the original text can be confused with token indexes.
Keep the original text, word indexes, and tokenizer offsets separate. When converting predictions back to entities, merge adjacent subword predictions according to the model's aggregation rule and then map the result to character spans.
Exercise
Why might the labels for the subwords of one word use B-PER, I-PER, and -100 instead of repeating B-PER?
Show answer
B-PER marks the beginning of the person entity, I-PER continues it, and -100 ignores later subwords during loss calculation. The exact policy must be used consistently during training and evaluation.