Independent token choices can form an impossible tag sequence; decoding over allowed transitions chooses a consistent path from local evidence.
Constrained sequence decoding and valid label paths
State the transition rules
In BIO tagging, I-DATE cannot begin a sequence or follow B-POLICY_ID. It must continue a DATE span. A local classifier may still assign I-DATE the highest score to the first token. Editing invalid predictions afterward can change boundaries unpredictably. Explicit transition constraints make the contract visible before scoring.
Compare local and global choices
A linear-chain decoder adds an emission score for each token label and a transition score between neighboring labels. Viterbi-style recurrence retains the best predecessor for every possible current label. The final path can differ from per-token argmax because a slightly weaker local label may permit a much stronger valid sequence. The compact code below enumerates paths for only three tokens; production decoding uses a recurrence.
Keep restrictions tied to the task
Hard constraints prevent illegal BIO transitions, but they cannot verify that a detected date is real or that a policy ID exists in the account system. An overly narrow label grammar can suppress valid unusual formats. Test unusual note templates and multilingual punctuation against the span contract.
Separate decoding from probability claims
The score of the best path is not automatically a calibrated probability. If a reviewer needs confidence, calculate a defined sequence or span uncertainty measure and validate it on later notes. Selective review handles ambiguous extractions.
Measure latency by sequence length
With T tokens and K labels, a dense first-order recurrence costs O(TK²) time. Long notes or many field types can make constrained decoding material even when encoder inference dominates on short notes. Profile complete intake latency rather than quoting only neural forward time.
Implementation
from itertools import product
labels = ("O", "B-POLICY", "I-POLICY")
emissions = [
{"O": 0.1, "B-POLICY": 0.8, "I-POLICY": 0.9},
{"O": 0.1, "B-POLICY": 0.2, "I-POLICY": 0.9},
{"O": 0.8, "B-POLICY": 0.1, "I-POLICY": 0.2},
]
def valid_path(path):
return all(tag != "I-POLICY" or
(position > 0 and path[position - 1] in {"B-POLICY", "I-POLICY"})
for position, tag in enumerate(path))
def decode_short_sequence(scores):
candidates = (path for path in product(labels, repeat=len(scores))
if valid_path(path))
return max(candidates,
key=lambda path: sum(scores[position][tag]
for position, tag in enumerate(path)))
assert decode_short_sequence(emissions) == ("B-POLICY", "I-POLICY", "O")Performance and operating cost
The toy enumeration is O(K^T) and exists only to expose the constraint. A Viterbi recurrence is O(TK²) time, with O(TK) backpointer memory; sparse allowed transitions can lower practical work. Compute these costs in addition to tokenization and encoder latency.
Common Mistakes
- Do not deploy exhaustive path enumeration for real notes.
- Do not call the highest-scoring path probability calibrated.
- Do not assume legal BIO syntax means the extracted field is factually valid.
