Sub topic 1
Extractive Question Answering with Transformers
Prerequisites
This chapter assumes you know basic Python, can read simple PyTorch tensor code, and have a rough idea of what a pretrained language model is. You should also know that text is split into tokens before a model reads it.
What extractive question answering is
Extractive question answering takes a question and a passage, then returns a span of text copied from that passage. The answer is never written from scratch.
Three approaches are common:
- Extractive: the model points to the answer inside the passage.
- Generative: the model writes an answer word by word, which may not appear in the passage.
- Retrieval-based: a system finds relevant documents, and a separate step picks the answer.
Extractive models are easier to check, because every answer can be traced to exact characters in the source.
SQuAD-style data: questions, contexts, and answer spans
SQuAD is a widely used question answering dataset. Each example has three fields:
question: the question text.context: the passage that contains the answer.answers: the answer text and its character position, such as{"text": [...], "answer_start": [...]}.
The answer_start value is a character index into the context. Models do not read characters directly, so the data must later be converted to token positions.
Subword tokens and character offsets
A tokenizer splits text into subwords, which are pieces smaller than a word. Rare words become several tokens. A fast tokenizer can also return an offset mapping: for each token, the start and end character positions in the original string. This mapping links model outputs back to the text.
from transformers import AutoTokenizer
name = "distilbert-base-cased-distilled-squad"
tokenizer = AutoTokenizer.from_pretrained(name)
question = "What does the Amazon basin cover?"
context = "Its drainage basin covers about seven million square kilometres."
enc = tokenizer(question, context, return_offsets_mapping=True)
print(tokenizer.convert_ids_to_tokens(enc["input_ids"]))
print(enc["offset_mapping"])
print(enc.sequence_ids()) # None for special tokens, 0 for question, 1 for contextPretrained encoders as the backbone
A pretrained encoder is a transformer model trained on large amounts of text. It turns each token into a vector that reflects the token and its surrounding words. For question answering, the encoder is reused, and a small extra layer is added on top to predict answer positions. This is why a model can answer questions after only a modest amount of task-specific training.
Feeding the question and context together
The question and context are packed into one input sequence, separated by special tokens. Attention lets every token see every other token, so the question can shape how the context is read. The sequence_ids() output in the code above shows which tokens came from the question and which from the context. The answer must come from context tokens only.
Exercise
In the sequence produced by the tokenizer for a question and a context, which tokens can the answer span contain?
Show answer
Only the tokens whose sequence id is 1, which belong to the context. Question tokens and special tokens are excluded.