Multilingual Universal Sentence Encoder Q&A Retrieval

View on TensorFlow.org Run in Google Colab View on GitHub Download notebook See TF Hub models

This is a demo for using Universal Encoder Multilingual Q&A model for question-answer retrieval of text, illustrating the use of question_encoder and response_encoder of the model. We use sentences from SQuAD paragraphs as the demo dataset, each sentence and its context (the text surrounding the sentence) is encoded into high dimension embeddings with the response_encoder. These embeddings are stored in an index built using the simpleneighbors library for question-answer retrieval.

On retrieval a random question is selected from the SQuAD dataset and encoded into high dimension embedding with the question_encoder and query the simpleneighbors index returning a list of approximate nearest neighbors in semantic space.

More models

You can find all currently hosted text embedding models here and all models that have been trained on SQuAD as well here.

Setup

Setup Environment

Setup common imports and functions

[nltk_data] Downloading package punkt to /home/kbuilder/nltk_data...
[nltk_data]   Unzipping tokenizers/punkt.zip.

Run the following code block to download and extract the SQuAD dataset into:

  • sentences is a list of (text, context) tuples - each paragraph from the SQuAD dataset are splitted into sentences using nltk library and the sentence and paragraph text forms the (text, context) tuple.
  • questions is a list of (question, answer) tuples.

Download and extract SQuAD data

10452 sentences, 10552 questions extracted from SQuAD https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v1.1.json

Example sentence and context:

sentence:

('Deke Slayton, the grounded Mercury astronaut who became Director of Flight '
 'Crew Operations for the Gemini and Apollo programs, selected the first '
 'Apollo crew in January 1966, with Grissom as Command Pilot, White as Senior '
 'Pilot, and rookie Donn F. Eisele as Pilot.')

context:

('Deke Slayton, the grounded Mercury astronaut who became Director of Flight '
 'Crew Operations for the Gemini and Apollo programs, selected the first '
 'Apollo crew in January 1966, with Grissom as Command Pilot, White as Senior '
 'Pilot, and rookie Donn F. Eisele as Pilot. But Eisele dislocated his '
 'shoulder twice aboard the KC135 weightlessness training aircraft, and had to '
 'undergo surgery on January 27. Slayton replaced him with Chaffee. NASA '
 'announced the final crew selection for AS-204 on March 21, 1966, with the '
 'backup crew consisting of Gemini veterans James McDivitt and David Scott, '
 'with rookie Russell L. "Rusty" Schweickart. Mercury/Gemini veteran Wally '
 'Schirra, Eisele, and rookie Walter Cunningham were announced on September 29 '
 'as the prime crew for AS-205.')

The following code block setup the tensorflow graph g and session with the Universal Encoder Multilingual Q&A model's question_encoder and response_encoder signatures.

Load model from tensorflow hub

The following code block compute the embeddings for all the text, context tuples and store them in a simpleneighbors index using the response_encoder.

Compute embeddings and build simpleneighbors index

Computing embeddings for 10452 sentences
0%|          | 0/104 [00:00<?, ?it/s]
simpleneighbors index for 10452 sentences built.

On retrieval, the question is encoded using the question_encoder and the question embedding is used to query the simpleneighbors index.

Retrieve nearest neighbors for a random question from SQuAD