- Introduced by
- Devlin et al. (BERT); Rajpurkar et al. (SQuAD benchmark)
- Year
- 2016–2019
- Type
- Transfer learning / fine-tuning for extractive or generative QA
- Family
- Deep learning / NLP / CV
What does it do?
Fine-tuned question answering takes a large pre-trained language model, such as BERT or RoBERTa, and continues training it on labeled question-context-answer examples so that it can answer questions about a given passage.
In the extractive form, the model predicts the start and end positions of an answer span inside the passage; a generative form produces the answer as free text. The pre-trained weights supply general language knowledge, and fine-tuning teaches the question-answering task.
The intuition behind the method
A pre-trained language model has already absorbed a great deal of grammar and word usage from large amounts of text, but it has not been taught to answer questions. Fine-tuning gives it labeled examples of questions, passages and correct answers, so it learns where in a passage an answer is likely to be and how to mark it. Because the model starts from pre-trained weights rather than from random initialisation, it needs far fewer labeled examples than a model trained for this task alone.
History
The fine-tuned QA setting was defined by the SQuAD dataset (Rajpurkar et al., 2016), which contains more than 100,000 questions written by crowdworkers on Wikipedia articles, with each answer a span of the reading passage. BERT (Devlin et al., 2019) showed that a pre-trained bidirectional transformer with a simple span-prediction layer added on top could be fine-tuned for this task. Later pre-trained transformer models were adapted to the same task format, and the approach has since been extended to other question-answering settings.
When to use it
Use fine-tuned QA when you have a labeled set of question-context-answer triples, ideally in the thousands, and need answers drawn from a known body of documents such as manuals, policies, clinical notes or research articles. It is best suited to extractive reading comprehension, where the answer is stated somewhere in the passage and can be marked as a span. Do not use it when labeled examples are scarce and no in-domain pre-trained backbone is available, when the answer depends on combining facts across several documents without a retrieval step, or when the answer is not stated in any text you can supply.
Limitations and considerations
- Training requires labeled question-context-answer triples, and creating them is costly; the SQuAD dataset, for example, was built from more than 100,000 crowdworker-written questions.
- An extractive model can only return text that appears verbatim in the passage, so it cannot answer a question whose answer is absent or only implied.
- Accuracy drops on questions that require multi-hop reasoning, commonsense inference or arithmetic unless the model is given additional training or architectural support.
- Large transformer backbones are computationally heavy at inference, so deployment may require quantisation or distillation into a smaller model.
- When a passage contains several similar candidate answers, the model can assign high scores to a span that answers a related but different question; generative variants can also produce answers that the passage does not support.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- On extractive benchmarks such as SQuAD, pre-trained transformers gain substantially over earlier feature-based models; the original SQuAD paper reports F1 of 51.0% for its logistic regression model against human F1 of 86.8%.
- Pre-trained representations allow the model to learn the answer-locating task from far fewer labeled examples than a model trained from scratch would need.
- Adapting to a specialised field is largely a matter of choosing a backbone pre-trained on text from that field, without changing the task head.
- The start and end scores provide a confidence signal that can support abstention, though these raw scores should be calibrated on held-out data before a threshold is set.
- The same training pipeline can serve extractive span prediction and generative answering, with the output head or decoder changed to match the task.
Applications
- Policy and manual lookup: extracting the clause or step that answers an employee's or customer's question from a fixed set of documents.
- Clinical and biomedical text: locating a stated dosage, finding or diagnosis within notes or abstracts, when the backbone has been pre-trained on matching text.
- Contract and regulatory review: marking the passage that states a specific obligation, date or definition in a document.
- Scientific literature: identifying the sentence in an article that reports a particular result, sample or method.
- Course material: answering students' factual questions from a textbook chapter whose content is fixed.
How it works
How it works
A pre-trained transformer, such as BERT-base, RoBERTa-large or DeBERTa, is chosen as the backbone. Its weights encode contextual representations learned from large text corpora with objectives such as masked language modelling. When the target text differs strongly from general web or encyclopaedic text, a backbone pre-trained on similar material (for example, a biomedical model for clinical questions) is usually the better starting point.
Each training example is a tuple of context passage, question and answer. For extractive QA the answer is given as a character or token span within the passage; for generative QA it is free text. The question and passage are joined with special separator tokens so that the model can attend across both at once.
The whole pre-trained model is updated on the QA dataset by minimising cross-entropy over the gold start and end positions of the answer (extractive) or over the answer tokens (generative). For extractive QA, a task head with a start vector S and an end vector E scores each token of the passage. A small learning rate is normally used so that the pre-trained representations are adjusted gradually rather than overwritten.
At inference, the model computes start and end probabilities for every passage token using the start and end scores described in the formula. The span with the highest combined start and end score, subject to a maximum length and to ordering constraints, is returned as the answer. For generative models, beam search or greedy decoding produces the answer sequence instead.
Formula
P(\text{start}=i) = \text{softmax}(S \cdot T_i), \quad P(\text{end}=j) = \text{softmax}(E \cdot T_j)Performance is reported with Exact Match (EM), which counts a prediction as correct only if it equals the gold answer string, and with token-level F1, which gives partial credit for overlapping words. The two metrics can diverge substantially, so both should be reported. For open-domain or retrieval-based QA, ranking metrics such as mean reciprocal rank may also be relevant.
Common pitfalls
- Evaluating only on Exact Match without F1, missing partial-credit answers that are semantically correct.
- Ignoring the context length limit of the backbone — long documents must be chunked with overlapping strides, or answers near chunk boundaries will be missed.
- Using a learning rate too high during fine-tuning, causing catastrophic forgetting of pre-trained representations.
- Treating dev-set EM/F1 as final without testing on held-out questions that differ in topic or phrasing from training.
- Fine-tuning on a general QA dataset (e.g., SQuAD) and expecting strong performance on a specialised domain without domain adaptation.
Frequently asked questions
How much labeled data do I need?
As a rough guide, a few hundred annotated examples can give useful results for extractive QA on text similar to the pre-training data. For specialised or low-resource domains, a larger set (around 1,000 to 5,000 examples) is generally advisable. If fewer examples are available, consider data augmentation or few-shot prompting with a generative model instead.
What is the difference between extractive and generative QA?
Extractive QA returns a span copied verbatim from the provided context; the model only predicts start and end positions. Generative QA produces free-form text with an encoder-decoder or decoder-only model, which can paraphrase or combine information but may produce statements that the context does not support, so generated answers need checking against the source.
My context documents are longer than the model's maximum token length. What should I do?
Split long documents into overlapping windows so that an answer near a chunk boundary appears whole in at least one window. The window length is bounded by the backbone (for example, 512 tokens for BERT), and the overlap is a setting to tune. At inference, combine predictions across chunks by keeping the span with the highest combined start and end score. Retrieval-augmented architectures that first select the relevant passage are an alternative for very large corpora.
How do I know when the model should abstain rather than guess?
Train on a dataset that includes questions with no answer in the passage, so that the model learns to output a null span for those cases. The threshold applied to the null-span score sets the abstention rate, and it should be chosen on held-out data rather than on the training set.
Can I use fine-tuned QA without a GPU?
Inference with smaller or distilled backbones (for example, DistilBERT or ALBERT-base) can run on a CPU at moderate throughput, but fine-tuning large models such as BERT-large or RoBERTa-large without a GPU is impractically slow. Quantised model exports or hosted API endpoints are practical alternatives for resource-constrained deployments.
Related methods
Sources
Devlin, J., Chang, M.-W., Lee, K., & Toutanova, K. (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. Proceedings of NAACL-HLT 2019, 4171–4186.
Open DOI referenceRajpurkar, P., Zhang, J., Lopyrev, K., & Liang, P. (2016). SQuAD: 100,000+ Questions for Machine Comprehension of Text. Proceedings of EMNLP 2016, 2383–2392.
Open DOI reference