Question Answering is the task of being able to answer questions pertaining to some known context. For example, given a context about some historical figure, any question pertaininig to the context should be answerable. In our case the article would be our input context and question, and the answer would be the output sequence from the model.
Note
We currently only support Extractive Question Answering, like the task performed using the SQUAD like datasets.
Let's look at an example. We'll use the SQUAD 2.0 dataset, which contains train-v2.0.json
and dev-v2.0.json
. Each JSON file looks like this:
{
"answers": {
"answer_start": [94, 87, 94, 94],
"text": ["10th and 11th centuries", "in the 10th and 11th centuries", "10th and 11th centuries", "10th and 11th centuries"]
},
"context": "\"The Normans (Norman: Nourmands; French: Normands; Latin: Normanni) were the people who in the 10th and 11th centuries gave thei...",
"id": "56ddde6b9a695914005b9629",
"question": "When were the Normans in Normandy?",
"title": "Normans"
}
...
In the above, the context
key represents the context used for the question and answer, the question
key represents the question being asked with respect to the context, the answer
key stores the answer(s) for the question. id
and title
are used for unique identification and grouping concepts together respectively. Once we've downloaded the data using ~flash.core.data.download_data
, we create the ~flash.text.question_answering.data.QuestionAnsweringData
. We select a pre-trained backbone to use for our ~flash.text.question_answering.model.QuestionAnsweringTask
and finetune on the SQUAD 2.0 data. The backbone can be any Question Answering model from HuggingFace/transformers.
Note
When changing the backbone, make sure you pass in the same backbone to the ~flash.text.question_answering.data.QuestionAnsweringData
and the ~flash.text.question_answering.model.QuestionAnsweringTask
!
Next, we use the trained ~flash.text.question_answering.model.QuestionAnsweringTask
for inference. Finally, we save the model. Here's the full example:
../../../flash_examples/question_answering.py
To learn how to view the available backbones / heads for this task, see backbones_heads
.
Torch ORT converts your model into an optimized ONNX graph, speeding up training & inference when using NVIDIA or AMD GPUs. Enabling Torch ORT requires a single flag passed to the QuestionAnsweringTask
once installed. See installation instructions here.
Note
Not all Transformer models are supported. See this table for supported models + branches containing fixes for certain models.
...
model = QuestionAnsweringTask(backbone="distilbert-base-uncased", max_answer_length=30, enable_ort=True)