This tutorial will demonstrate how to train, evaluate, and test three types of models for Question-Answering.
Overview
Deprecation Notice
This tutorial is deprecated as of r1.23.0 and will be removed in the next release.
This tutorial will demonstrate how to train, evaluate, and test three types of models for Question-Answering -
- BERT-like models for Extractive Question-Answering
- Sequence-to-Sequence (S2S) models for Generative Question-Answering (ex. T5/BART-like)
- GPT-like models for Generative Question-Answering
Task Description
- Given a context and a natural language query, we want to generate an answer for the query
- Depending on how the answer is generated, the task can be broadly divided into two types:
- Extractive Question Answering
- Generative Question Answering
Extractive Question-Answering with BERT-like models
Given a question and a context, both in natural language, predict the span within the context with a start and end position which indicates the answer to the question. For every word in our training dataset we’re going to predict:
- likelihood this word is the start of the span
- likelihood this word is the end of the span
We are using a BERT encoder with 2 span prediction heads for predicting start and end position of the answer. The span predictions are token classifiers consisting of a single linear layer.
Generative Question-Answering with S2S and GPT-like models
Given a question and a context, both in natural language, generate an answer for the question. Unlike the BERT-like models, there is no constraint that the answer should be a span within the context.
Installing NeMo
You can run either this notebook locally (if you have all the dependencies and a GPU) or on Google Colab.
Instructions for setting up Colab are as follows:
- Open a new Python 3 notebook.
- Import this notebook from GitHub (File -> Upload Notebook -> "GITHUB" tab -> copy/paste GitHub URL)
- Connect to an instance with a GPU (Runtime -> Change runtime type -> select "GPU" for hardware accelerator)
- Run the cell below to set up dependencies.
Imports and constants
Configuration
The model is defined in a config file which declares multiple important sections:
- model: All arguments that will relate to the Model - language model, span prediction, optimizer and schedulers, datasets and any other related information
- trainer: Any argument to be passed to PyTorch Lightning
- exp_manager: All arguments used for setting up the experiment manager - target directory, name, logger information
We will download the default config file provided at NeMo/examples/nlp/question_answering/conf/qa_conf.yaml and edit necessary values for training different models
Training and testing models on SQuAD v2.0
Dataset
For this example, we are going to download the SQuAD dataset to showcase how to do training and inference. There are two datasets, SQuAD1.0 and SQuAD2.0. SQuAD 1.1, the previous version of the SQuAD dataset, contains 100,000+ question-answer pairs on 500+ articles. SQuAD2.0 dataset combines the 100,000 questions in SQuAD1.1 with over 50,000 unanswerable questions written adversarially by crowdworkers to look similar to answerable ones.
To download both datasets, we use NeMo/examples/nlp/question_answering/get_squad.py
After execution of the above cell, your data folder will contain a subfolder "squad" the following four files for training and evaluation
squad
│
└───v1.1
│ │ - train-v1.1.json
│ │ - dev-v1.1.json
│
└───v2.0
│ - train-v2.0.json
│ - dev-v2.0.json
Set dataset config values
Set trainer config values
Set experiment manager config values
BERT model for SQuAD v2.0
Set model config values
Create trainer and initialize model
Train, test, and save the model
Load the saved model and run inference
S2S BART model for SQuAD v2.0
Set model config values
Create trainer and initialize model
Train, test, and save the model
Load the saved model and run inference
GPT2 model for SQuAD v2.0
Set model config values
Create trainer and initialize model
Train, test, and save the model
Load the saved model and run inference
Training and testing models on MS-MARCO
Dataset
Downloading the data
MS-MARCO(Microsoft Machine Reading Comprehension) is a large scale dataset focused on machine reading comprehension, question answering, and passage ranking. MS-MARCO consists of 1,010,916 queries generated from real, anonymized Bing user queries. The contexts are extracted from real web documents and the answers are generated by humans.
Please agree to the Terms of Use at https://microsoft.github.io/msmarco/ before downloading the data
The data can be downloaded at:
Converting to SQuAD format
The script for converting MS-MARCO dataset to SQuAD can be found at NeMo/examples/nlp/question_answering/convert_msmarco_to_squad_format.py