Question answering
Question answering tasks return an answer given a question, and the extractive type extracts the answer from the given context.
Definition · At a glance · How it works
Definition
Question answering tasks return an answer given a question. Extractive question answering extracts the answer from the given context. Abstractive question answering generates an answer from the context that correctly answers the question.
At a glance
In the Hugging Face Transformers question answering guide, a DistilBERT tokenizer processes the question and context fields. In the preprocessing code of the Hugging Face Transformers question answering guide, answer_start supplies the start character position; the end character position is derived by adding the length of the answer text. The context field is background information from which the model needs to extract the answer. The question field is the question a model should answer.
How it works
In the preprocessing part of the question answering guide, the Datasets map function is used to apply the preprocessing function over the entire dataset. Some examples in a dataset may have a very long context that exceeds the maximum input length of the model. In the question answering guide, a batch of examples is created using DefaultDataCollator.
In the documentation's preprocessing code, an answer that is not fully inside the context is labeled (0, 0). Unlike other data collators in Transformers, the DefaultDataCollator does not apply additional preprocessing such as padding. The guide's inference example uses the tokenizer to turn the text into PyTorch tensors.
For inference, the inputs are passed to the model and the logits are returned. Next, the highest probability from the model output is taken for the start and end positions. Then the predicted tokens are decoded to get the answer.
Evaluation for question answering requires a significant amount of postprocessing. The question answering guide skips the evaluation step. The Trainer still calculates the evaluation loss during training, so users are not completely in the dark about the model's performance.
For readers seeking to continue into question answering evaluation, the guide points to the Question answering chapter in the Hugging Face Course as the next reading path.
Example
The question answering guide includes finetuning DistilBERT on the SQuAD dataset for extractive question answering. The guide starts by loading a smaller subset of the SQuAD dataset from the Datasets library. The train split of the dataset is split into a train and test set.
For finetuning, DistilBERT is loaded with AutoModelForQuestionAnswering. In the training part of the question answering guide, the training hyperparameters are defined in TrainingArguments. The inference example uses the question: How many programming languages does BLOOM support?
In the same training part of the question answering guide, the training arguments are passed to Trainer along with the model, dataset, tokenizer, and data collator. In this inference example, decoding the predicted tokens gives the text: 176 billion parameters and can generate text in 46 languages natural languages and 13.
Sources
This page is written from the official sources below. Open them to check the original text.
- Official documentationhttps://huggingface.co/docs/transformers/tasks/question_answeringFetched
Verified ·
This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.