Question answering
Last updated
Last updated
Question answering tasks return an answer given a question. If youโve ever asked a virtual assistant like Alexa, Siri or Google what the weather is, then youโve used a question answering model before. There are two common types of question answering tasks:
Extractive: extract the answer from the given context.
Abstractive: generate an answer from the context that correctly answers the question.
This guide will show you how to:
Finetune on the dataset for extractive question answering.
Use your finetuned model for inference.
The task illustrated in this tutorial is supported by the following model architectures:
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
Before you begin, make sure you have all the necessary libraries installed:
Copied
We encourage you to login to your BOINC AI account so you can upload and share your model with the community. When prompted, enter your token to login:
Copied
Start by loading a smaller subset of the SQuAD dataset from the ๐ Datasets library. Thisโll give you a chance to experiment and make sure everything works before spending more time training on the full dataset.
Copied
Copied
Then take a look at an example:
Copied
There are several important fields here:
answers
: the starting location of the answer token and the answer text.
context
: background information from which the model needs to extract the answer.
question
: the question a model should answer.
The next step is to load a DistilBERT tokenizer to process the question
and context
fields:
Copied
There are a few preprocessing steps particular to question answering tasks you should be aware of:
Some examples in a dataset may have a very long context
that exceeds the maximum input length of the model. To deal with longer sequences, truncate only the context
by setting truncation="only_second"
.
Next, map the start and end positions of the answer to the original context
by setting return_offset_mapping=True
.
With the mapping in hand, now you can find the start and end tokens of the answer. Use the sequence_ids
method to find which part of the offset corresponds to the question
and which corresponds to the context
.
Here is how you can create a function to truncate and map the start and end tokens of the answer
to the context
:
Copied
Copied
PytorchHide Pytorch contentCopied
TensorFlowHide TensorFlow contentCopied
PytorchHide Pytorch content
Copied
At this point, only three steps remain:
Copied
Copied
TensorFlowHide TensorFlow content
To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters:Copied
Copied
Copied
Copied
Copied
Copied
Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!
Great, now that youโve finetuned a model, you can use it for inference!
Come up with a question and some context youโd like the model to predict:
Copied
Copied
You can also manually replicate the results of the pipeline
if youโd like:
PytorchHide Pytorch content
Tokenize the text and return PyTorch tensors:
Copied
Pass your inputs to the model and return the logits
:
Copied
Get the highest probability from the model output for the start and end positions:
Copied
Decode the predicted tokens to get the answer:
Copied
TensorFlowHide TensorFlow content
Tokenize the text and return TensorFlow tensors:
Copied
Pass your inputs to the model and return the logits
:
Copied
Get the highest probability from the model output for the start and end positions:
Copied
Decode the predicted tokens to get the answer:
Copied
Split the datasetโs train
split into a train and test set with the method:
To apply the preprocessing function over the entire dataset, use ๐ Datasets function. You can speed up the map
function by setting batched=True
to process multiple elements of the dataset at once. Remove any columns you donโt need:
Now create a batch of examples using . Unlike other data collators in ๐ Transformers, the does not apply any additional preprocessing such as padding.
If you arenโt familiar with finetuning a model with the , take a look at the basic tutorial !
Youโre ready to start training your model now! Load DistilBERT with :
Define your training hyperparameters in . The only required parameter is output_dir
which specifies where to save your model. Youโll push this model to the Hub by setting push_to_hub=True
(you need to be signed in to BOINC AI to upload your model).
Pass the training arguments to along with the model, dataset, tokenizer, and data collator.
Call to finetune your model.
Once training is completed, share your model to the Hub with the method so everyone can use your model:
If you arenโt familiar with finetuning a model with Keras, take a look at the basic tutorial !
Then you can load DistilBERT with :
Convert your datasets to the tf.data.Dataset
format with :
Configure the model for training with :
The last thing to setup before you start training is to provide a way to push your model to the Hub. This can be done by specifying where to push your model and tokenizer in the :
Finally, youโre ready to start training your model! Call with your training and validation datasets, the number of epochs, and your callback to finetune the model:
For a more in-depth example of how to finetune a model for question answering, take a look at the corresponding or .
Evaluation for question answering requires a significant amount of postprocessing. To avoid taking up too much of your time, this guide skips the evaluation step. The still calculates the evaluation loss during training so youโre not completely in the dark about your modelโs performance.
If have more time and youโre interested in how to evaluate your model for question answering, take a look at the chapter from the ๐ BOINC AI Course!
The simplest way to try out your finetuned model for inference is to use it in a . Instantiate a pipeline
for question answering with your model, and pass your text to it: