Answering Questions with Transformers

[This article was first published on Python – Predictive Hacks, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

In a previous post, we showed how we could do text summarization with transformers. Here, we will provide you an example, of how we can use transformers for question answering. We will work with the huggingface library. We will work with Google Colab, so the example is reproducible. First, we need to install the libraries:

!pip install transformers
!pip install torch

Now, we are ready to work with the transformers.

Example of Answering Questions

For an AI algorithm to be able to answer questions from an input text, it means that it is able to “understand”, that is why we call it Natural Language Understanding (NLU) and to be able to respond by generating a text, that is why we call it Natural Language Generation (NLG). The process that we will follow for question answering is described in hugging face documentation:

  • Start the BERT model
  • Provide the input text and the required questions
  • Iterate over the questions and build a sequence from the text and the current question, with the correct model-specific separators token type ids and attention masks
  • Pass this sequence through the model. This outputs a range of scores across the entire sequence tokens (question and text), for both the start and end positions.
  • Compute the softmax of the result to get probabilities over the tokens
  • Fetch the tokens from the identified start and stop values, convert those tokens to a string.
  • Print the results

Note that I took the code from the Hugging Face documentation, and I made a change because there was a bug. I changed the:

answer_start_scores, answer_end_scores = model(**inputs)

with:

answer_start_scores, answer_end_scores = model(**inputs)[0], model(**inputs)[1]

In our example, I provide the following input text:

My name is George Pipis and I work as a Data Scientist at Persado. My personal blog is called Predictive Hacks which provides tutorials mainly in R and Python

And I will make the following questions:

  • What is my name?
  • What is George Pipis job?
  • What is the name of the blog?
  • What is the blog about?
  • Where does George work?

Let’s see how well can transformers answer those questions above.

from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch

tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")

text = r"""
My name is George Pipis and I work as a Data Scientist at Persado. 
My personal blog is called Predictive Hacks which provides tutorials mainly in R and Python.

"""

questions = [
    "What is my name?",
    "What is George Pipis job?",
    "What is the name of the blog?",
    "What is the blog about?",
    "Where does George work?"
]

for question in questions:
    inputs = tokenizer.encode_plus(question, text, add_special_tokens=True, return_tensors="pt")
    input_ids = inputs["input_ids"].tolist()[0]

    text_tokens = tokenizer.convert_ids_to_tokens(input_ids)
    answer_start_scores, answer_end_scores = model(**inputs)[0], model(**inputs)[1]

    answer_start = torch.argmax(answer_start_scores)  # Get the most likely beginning of answer with the argmax of the score
    answer_end = torch.argmax(answer_end_scores) + 1  # Get the most likely end of answer with the argmax of the score

    answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))

    print(f"Question: {question}")
    print(f"Answer: {answer}\n")
 

Output:

Question: What is my name?
Answer: george pipis

Question: What is George Pipis job?
Answer: data scientist

Question: What is the name of the blog?
Answer: predictive hacks

Question: What is the blog about?
Answer: tutorials

Question: Where does George work?
Answer: persado

Not bad at all! The model was able to provide good answers 🤗

To leave a comment for the author, please follow the link and comment on their blog: Python – Predictive Hacks.

Want to share your content on python-bloggers? click here.