Question Answering with Pretrained Transformers Using PyTorch | by Raymond Cheng | Towards Data Science

Question answering is a task in information retrieval and Natural Language Processing (NLP) that investigates software that can answer questions asked by humans in natural language. In Extractive Question Answering, a context is provided so that the model can refer to it and make predictions on where the answer lies within the passage.
In this article, we will show you how to implement question answering using pretrained models provided by the Huggingface Transformers library. Since the implementation is really straightforward, you can get your question answering system to work fast within minutes!
Now, let’s get started!
Step 1: Install Library
Step 2: Import Library
Step 3: Build the Question Answering Pipeline
Step 4: Define the Context and Question to Ask
Step 5: Perform Question Answering
BONUS: Question Answering for any Language
We will be using the Transformers library for question answering. To install it, simply run:
pip install transformers
Note: Remember to head over to the PyTorch official website if you haven’t installed it!
After you have successfully installed Transformers, now you can import the library to a python script:
from transformers import pipeline
We are going to be using only the pipeline module, which is an abstraction layer that provides a simple API to perform various tasks.
Now, we can start building the pipeline. To build the question answering pipeline, we can simply do:
question_answering = pipeline(“question-answering”)
This will create a model pretrained on question answering as well as its tokenizer in the background. The default model used in this case is DistilBERT-base, which is fine-tuned on the SQuAD dataset. You can learn more about DistilBERT in its paper.
To use your own model and tokenizer, you can pass in them as model and tokenizer parameters to the pipeline.
Now, it’s time to create our context and question we want to ask our model. Let’s grab a quick Machine Learning definition from Wikipedia as the context:
Machine learning (ML) is the study of computer algorithms that improve automatically through experience. It is seen as a part of artificial intelligence. Machine learning algorithms build a model based on sample data, known as “training data”, in order to make predictions or decisions without being explicitly programmed to do so. Machine learning algorithms are used in a wide variety of applications, such as email filtering and computer vision, where it is difficult or unfeasible to develop conventional algorithms to perform the needed tasks.
context = """Machine learning (ML) is the study of computer algorithms that improve automatically through experience. It is seen as a part of artificial intelligence. Machine learning algorithms build a model based on sample data, known as "training data", in order to make predictions or decisions without being explicitly programmed to do so. Machine learning algorithms are used in a wide variety of applications, such as email filtering and computer vision, where it is difficult or unfeasible to develop conventional algorithms to perform the needed tasks."""
And ask it the following question:
What are machine learning models based on?
question = "What are machine learning models based on?"
Finally, it’s time to test our model to answer our question! We can simply run the question answering model by passing the context and question as parameters to the instantiated pipeline and print out the results:
result = question_answering(question=question, context=context)print("Answer:", result['answer'])print("Score:", result['score'])
The output should say:
Answer: sample data,Score: 0.8846667408943176
From the output, we should be able to see the answer as “sample data”, which is correct, and also see its confidence score, which I’d say is pretty high in this case.
Wait, but you might ask, how do we implement question answering for languages other than English? Before you go, I thought that might be something you might be wondering.
Fortunately, we have a pool of models that are being released by our beloved community that might have already been pretrained on your language for question answering. We can go to the Huggingface models website to view the models available for question answering.
Let’s say we want to do question answering in Chinese. We can use a multi-lingual model pretrained on multiple languages. For example, this multilingual BERT is trained on the Deepmind’s xQuAD dataset (a multi-lingual version of the SQuAD dataset), which supports 11 languages: Arabic, German, Greek, English, Spanish, Hindi, Russian, Thai, Turkish, Vietnamese, and Chinese.
Now, according to the model documentation, we can directly build the model in the pipeline by specifying its model and tokenizer parameters like the following:
question_answering = pipeline("question-answering", model="mrm8488/bert-multi-cased-finetuned-xquadv1",    tokenizer="mrm8488/bert-multi-cased-finetuned-xquadv1")
Then, let’s set our context as:
机器学习是人工智能的一个分支。 是个人很热门的专业。
context = """机器学习是人工智能的一个分支。 是个人很热门的专业。"""## Machine Learning is a branch of Artificial Intelligence. It is a popular major.
And question to ask:
机器学习是什么的分支?
question = "机器学习是什么的分支?"## What is Machine Learning a branch of?
After that, we can use the same code as previous to run the system:
result = question_answering(question=question, context=context)print("Answer:", result['answer'])print("Score:", result['score'])
The system will output:
Answer: 机器学习是人工智能的一个分支。Score: 0.9717233777046204
If you know Chinese, you will know that the output answer translates to “Machine Learning is a branch of Artificial Intelligence”, which is correct!
And that’s it for today! Now you should know how to implement a question answering system in any language using a pretrained model. In case you want to view the entire code all at once, I have attached a Jupyter notebook below:
from transformers import pipeline

question_answering = pipeline("question-answering")

context = """Machine learning (ML) is the study of computer algorithms that improve automatically through experience. It is seen as a part of artificial intelligence. Machine learning algorithms build a model based on sample data, known as "training data", in order to make predictions or decisions without being explicitly programmed to do so. Machine learning algorithms are used in a wide variety of applications, such as email filtering and computer vision, where it is difficult or unfeasible to develop conventional algorithms to perform the needed tasks."""

question = "What are machine learning models based on?"

result = question_answering(question=question, context=context)

print("Answer:", result['answer'])
print("Score:", result['score'])

/home/raymond/.conda/envs/movie_script_generation/lib/python3.6/site-packages/transformers/tokenization_utils_base.py:1423: FutureWarning: The `max_len` attribute has been deprecated and will be removed in a future version, use `model_max_length` instead.
  FutureWarning,

Answer: sample data,
Score: 0.8846667408943176

question_answering = pipeline("question-answering", model="mrm8488/bert-multi-cased-finetuned-xquadv1",
    tokenizer="mrm8488/bert-multi-cased-finetuned-xquadv1")

context = """机器学习是人工智能的一个分支。 是个人很热门的专业。"""
question = "机器学习是什么的分支?"

result = question_answering(question=question, context=context)

print("Answer:", result['answer'])
print("Score:", result['score'])

/home/raymond/.conda/envs/movie_script_generation/lib/python3.6/site-packages/transformers/tokenization_utils_base.py:1423: FutureWarning: The `max_len` attribute has been deprecated and will be removed in a future version, use `model_max_length` instead.
  FutureWarning,

Answer: 机器学习是人工智能的一个分支。
Score: 0.9717233777046204

If you have any questions or discussions, feel free to comment down below! Hope to see everyone soon. If you like my work, feel free to browse my other previous posts:
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
[1] Transformers Github, Huggingface
[2] Transformers Official Documentation, Huggingface
[3] Pytorch Official Website, Facebook AI Research
[4] Sanh, Victor, et al. “DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter.” arXiv preprint arXiv:1910.01108 (2019).
[5] SQuAD Official Website, Stanford
[6] XQuAD Official Github, Google Deepmind