多言語ユニバーサルセンテンスエンコーダーの Q&A と取得

TensorFlow.org で実行 Google Colabで実行 GitHub でソースを表示 ノートブックをダウンロード TF Hub モデルを参照

これは、テキストの質問と回答の取得に使用する多言語対応のユニバーサルセンテンスエンコーダー Q&A モジュールを使用して、モデルの question_encoderresponse_encoder を説明する実演です。デモ用データセットとして SQuAD 段落の文章を使用します。各文章とそのコンテキスト(文章の周辺にあるテキスト)は、response_encoder を使って高次元埋め込みにエンコードされており、質問と回答の取得に使用できるよう、埋め込みは simpleneighbors ライブラリを使用して構築されたインデックスに保存されています。

取得時、SQuAD データセットからランダムな質問が選択されて question_encoder で高次元埋め込みにエンコードされ、simpleneighbors インデックスをクエリすると、セマンティック空間の最近傍のリストが返されます。

その他のモデル

現在ホストされているテキスト埋め込みモデルはこちらを、SQuAD でもトレーニングされたすべてのモデルはこちらをご覧ください。

セットアップ

Setup Environment

Setup common imports and functions

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

次のコードブロックを実行して、SQuAD データセットを次のように抽出します。

  • sentences: (text, context) のタプル式のリストです。SQuAD データセットの各段落は nltk ライブラリを使って文章ごとに分割され、その文章と段落のテキストによって (text, context) タプル式を形成します。
  • questions: (question, answer) タプル式のリストです。

注意: 以下の squad_url を選択すると、この実演を使用して、SQuAD の train データセットまたはより小規模な dev データセット(1.1 または 2.0)のインデックスを作成できます。

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:

('Such experiments demonstrate the crucial properties that forces are additive '
 'vector quantities: they have magnitude and direction.')

context:

('Historically, forces were first quantitatively investigated in conditions of '
 'static equilibrium where several forces canceled each other out. Such '
 'experiments demonstrate the crucial properties that forces are additive '
 'vector quantities: they have magnitude and direction. When two forces act on '
 'a point particle, the resulting force, the resultant (also called the net '
 'force), can be determined by following the parallelogram rule of vector '
 'addition: the addition of two vectors represented by sides of a '
 'parallelogram, gives an equivalent resultant vector that is equal in '
 'magnitude and direction to the transversal of the parallelogram. The '
 'magnitude of the resultant varies from the difference of the magnitudes of '
 'the two forces to their sum, depending on the angle between their lines of '
 'action. However, if the forces are acting on an extended body, their '
 'respective lines of application must also be specified in order to account '
 'for their effects on the motion of the body.')

次のコードブロックは、Univeral Encoder Multilingual Q&A モデルquestion_encoderresponse_encoder シグネチャを使用して、TensorFlow グラフ gsession をセットアップします。

Load model from tensorflow hub

次のコードブロックは、response_encoder を使用して、すべてのテキストの埋め込みを計算し、タプルのコンテキストを形成し、simpleneighbors インデックスに格納します。

Compute embeddings and build simpleneighbors index

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

取得時、質問は question_encoder でエンコードされ、質問の埋め込みを使って simpleneighbors インデックスがクエリされます。

Retrieve nearest neighbors for a random question from SQuAD