Question Answering (Part 6): Using pre-trained BERT (Bidirectional Encoder Representations from Transformers) Model To Build a Question Answering system

Atul Singh, PhD
2 min readFeb 24, 2021

In this article let us look at the architecture of a Question Answering system, and we can build one using a pre-trained Question Answer model based on BERT. But before that let us do a quick recap of what we have learned in this series so far. We have looked the business use case for building a Question Answering system, the different ways in which we can build Question Answering systems, the data sets that can be used for supervised machine learning based Question Answer models, explanation of RNN and Transformers, and a BERT based architecture for building Question Answer models.

Question Answering System Architecture

The architecture of a Question Answering system. Apart from a Question Answer model we need a ranker that searches the corpus to return the relevant passages on which to apply the Question Answer model.

To build a Question Answering system we need a ranker component. The ranker component takes as its input the natural language query, and the corpus. The ranker uses information retrieval techniques to return a set of passages from the corpus which could potentially contain the answer to the question. Each question along with the passage is passed through the Question Answer model to get the answers. The answers are ranked on the basis of the match to the query and the model’s confidence in the answer selected from the passage.

Implementing the Ranker

You can use an existing tools with search capabilities such as ElasticSearch, or Lucene to build the ranker. For the purpose of this article we have created a cosine similarity based ranker. It computes the cosine distance of the documents in the corpus with the input question.

The code to compute the cosine distance of the input question to the documents in the corpus is shown below:

The code to get the top_n documents that are closest to the input question based on the cosine distance of the corpus documents from the input question is show below:

Using Pre-trained BERT Question Answer models

Building a BERT based question answer model from scratch is time consuming. There are libraries available which provide easy to use wrappers over pre trained BERT models for question answering tasks. DeepPavlov and HuggingFace are two popular libraries that provide support for pre-trained BERT models.

DeepPavlov and HuggingFaces provide easy to use wrappers over BERT that can be used for question answering systems.

The code to use DeepPavlov to find answer to an input question from a passage is show below:

The code to use HuggingFace to find answer to an input question is available in the ipython notebook which contains the complete demo code.

Hope you find the code sample, and the detailed explanation on building Question Answering system useful.

--

--

Atul Singh, PhD

Data scientist, with extensive experience of design, development, and industrialization of AI/ML based solutions for finance, telecom, retail and healthcare.