Sub topic 10
Questions with No Answer in the Text
Prerequisites and the null answer
This chapter assumes that you understand tokenization, question-context pair encoding, and start and end logits from a transformer question-answering model.
Extractive question answering should be able to say that a question has no answer in the supplied context. This case is called unanswerable question answering. A model must distinguish between:
- An answer span that appears in the context.
- A question whose answer is absent from the context.
This differs from generative question answering. A generative model might invent an answer or use information learned elsewhere. An extractive model should return no span when the passage does not support an answer.
SQuAD 2.0 adds unanswerable questions to the original SQuAD format. For these examples, the answers.text list is empty, and there is no answer character position.
Representing “no answer”
A common convention uses the special [CLS] token as the null answer. The model already produces a start and end logit for every token, so the null score is:
null_score = start_logit[CLS] + end_logit[CLS]The model also finds the best valid context span. The decoder compares that span with the null option:
margin = best_span_score - null_scoreIf the margin is greater than a chosen threshold, the decoder returns the span. Otherwise, it returns an empty answer.
| Decision | Meaning |
|---|---|
| `best_span_score - null_score > threshold` | Return the best context span |
| `best_span_score - null_score <= threshold` | Return no answer |
The threshold controls the trade-off. A lower threshold makes the system more willing to answer. A higher threshold makes it more willing to abstain.
Running a SQuAD 2.0 model
Use a checkpoint fine-tuned on data containing unanswerable questions. A model trained only on answerable SQuAD examples may always select some text, even when the passage is irrelevant.
from transformers import pipeline
qa = pipeline(
"question-answering",
model="deepset/roberta-base-squad2",
)
context = (
"The Pacific Ocean is the largest ocean on Earth. "
"It covers more than 165 million square kilometres."
)
result = qa(
question="What is the capital of France?",
context=context,
)
print(result)
print(result["answer"] or "[no answer found]")For an impossible question, a SQuAD 2.0 checkpoint should favor the null option. The exact output depends on the library version and model, so inspect the answer and score rather than assuming that a particular numeric score means “certain.”
Thresholds and failure modes
The score returned by a pipeline is not necessarily a calibrated probability. It is safer to evaluate many examples and choose a threshold on a validation set. You can measure whether the system abstains on impossible questions while still answering supported questions.
Common errors include:
- Hallucinated spans: The model selects plausible words that do not answer the question.
- Over-abstention: The model returns no answer even when the context contains one.
- Near-miss evidence: The context discusses the topic but does not state the requested fact.
- Window errors: In a long context, the answer may occur in another overflow window.
- Bad threshold selection: A threshold chosen on training data may not transfer to new documents.
A useful error review records the question, predicted span, null score, best span score, and reference label. This shows whether mistakes come from the encoder, span decoding, or the abstention threshold.
Exercise
A model gives a best span score of 12.4 and a null score of 13.1. With a threshold of 0.0, should the decoder return the span or no answer?
Show answer
It should return no answer. The margin is 12.4 - 13.1 = -0.7, which is not greater than the threshold.