Two output heads used when fine-tuning BERT (from Murphy's book) need a decoding step after the network.
Extractive QA. The model scores every token as a possible answer start () and end (). The answer is the span maximising subject to and max_len. Implement best_span(start_logits, end_logits, max_len) returning (i, j, score) with 0-based inclusive indices. On ties, prefer the smallest , then the smallest .
Tagging. For named entity recognition each token gets a BIO tag: "B-X" begins a chunk of type X, "I-X" continues a chunk of type X, "O" is outside any chunk. Implement bio_to_spans(tags) returning a list of (type, start, end) tuples (inclusive token indices) in order of their start. An "I-X" tag continues the current chunk only if the previous token was "B-X" or "I-X" with the same type; otherwise (after "O", at the start, or after a different type) it starts a new chunk of type X. A "B-X" always starts a new chunk, even right after another chunk of type X.