{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "h2q27gKz1H20" }, "source": [ "##### Copyright 2020 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "TUfAcER1oUS6" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "Gb7qyhNL1yWt" }, "source": [ "# TensorFlow Lite Model Maker による BERT 質疑応答" ] }, { "cell_type": "markdown", "metadata": { "id": "Fw5Y7snSuG51" }, "source": [ "
![]() | \n",
" ![]() | \n",
" ![]() | \n",
" ![]() | \n",
"
\n", " 回答は文章に含まれている(画像提供: SQuAD ブログ)\n", "
\n", "\n", "質疑応答タスクのモデルでは、入力は、すでに前処理されている文章と質問のペアで、出力は、文章の各トークンの開始ロジットと終了ロジットです。入力のサイズは設定可能で、文章と質問の長さに応じて調整することができます。" ] }, { "cell_type": "markdown", "metadata": { "id": "gb7P4WQta8Ub" }, "source": [ "## エンドツーエンドの概要\n" ] }, { "cell_type": "markdown", "metadata": { "id": "w7cIHjIfbDlG" }, "source": [ "次のコードスニペットでは、数行のコード内でモデルを取得する方法を示します。全体的なプロセスには、(1)モデルの選択、(2)データの読み込み、(3)モデルの再トレーニング、(4)評価、(5)TensorFlow Lite 形式へのエクスポート、という 5 つのステップが含まれます。" ] }, { "cell_type": "markdown", "metadata": { "id": "xQPdlxZBYuZG" }, "source": [ "```python\n", "# Chooses a model specification that represents the model.\n", "spec = model_spec.get('mobilebert_qa')\n", "\n", "# Gets the training data and validation data.\n", "train_data = DataLoader.from_squad(train_data_path, spec, is_training=True)\n", "validation_data = DataLoader.from_squad(validation_data_path, spec, is_training=False)\n", "\n", "# Fine-tunes the model.\n", "model = question_answer.create(train_data, model_spec=spec)\n", "\n", "# Gets the evaluation result.\n", "metric = model.evaluate(validation_data)\n", "\n", "# Exports the model to the TensorFlow Lite format with metadata in the export directory.\n", "model.export(export_dir)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "exScAdvBbNEi" }, "source": [ "上記のコードについて、次のセクションでより詳しく説明します。" ] }, { "cell_type": "markdown", "metadata": { "id": "bcLF2PKkSbV3" }, "source": [ "## 前提条件\n", "\n", "この例を実行するには、[GitHub リポジトリ](https://github.com/tensorflow/examples/tree/master/tensorflow_examples/lite/model_maker) から、Model Maker パッケージを含む必要なパッケージをインストールする必要があります。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "qhl8lqVamEty" }, "outputs": [], "source": [ "!sudo apt -y install libportaudio2\n", "!pip install -q tflite-model-maker-nightly" ] }, { "cell_type": "markdown", "metadata": { "id": "l6lRhVK9Q_0U" }, "source": [ "必要なパッケージをインポートします。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "XtxiUeZEiXpt" }, "outputs": [], "source": [ "import numpy as np\n", "import os\n", "\n", "import tensorflow as tf\n", "assert tf.__version__.startswith('2')\n", "\n", "from tflite_model_maker import model_spec\n", "from tflite_model_maker import question_answer\n", "from tflite_model_maker.config import ExportFormat\n", "from tflite_model_maker.question_answer import DataLoader" ] }, { "cell_type": "markdown", "metadata": { "id": "l65ctmtW7_FF" }, "source": [ "「エンドツーエンドの概要」では、簡単なエンドツーエンドの例を実演しています。次のセクションでは、順を追ってより詳しく例を説明します。" ] }, { "cell_type": "markdown", "metadata": { "id": "kJ_B8fMDOhMR" }, "source": [ "## 質疑応答のモデルを表現する model_spec を選択する\n", "\n", "各 `model_spec` オブジェクトは、質疑応答用の特定のモデルを表現します。Model Maker は現在、MobileBERT と BERT ベースモデルをサポートしています。\n", "\n", "サポートされているモデル | model_spec の名前 | モデルの説明\n", "--- | --- | ---\n", "[MobileBERT](https://arxiv.org/pdf/2004.02984.pdf) | 'mobilebert_qa' | BERT ベースより 4.3 倍小さく、5.5 倍高速ですが、オンデバイスシナリオに適した、優位性のある結果を達成します。\n", "[MobileBERT-SQuAD](https://arxiv.org/pdf/2004.02984.pdf) | 'mobilebert_qa_squad' | MobileBERT モデルと同じモデルアーキテクチャを持ち、最初のモデルは [SQuAD1.1](https://rajpurkar.github.io/SQuAD-explorer/) で再トレーニング済みです。\n", "[BERT-Base](https://arxiv.org/pdf/1810.04805.pdf) | 'bert_qa' | NLP タスクで広く使用される標準的な BERT モデルです。\n", "\n", "このチュートリアルでは、例として [MobileBERT-SQuAD](https://arxiv.org/pdf/2004.02984.pdf) を使用します。モデルは [SQuAD1.1](https://rajpurkar.github.io/SQuAD-explorer/) で再トレーニング済みであるため、質疑応答タスクではより高速に収束する可能性があります。\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "vEAWuZQ1PFiX" }, "outputs": [], "source": [ "spec = model_spec.get('mobilebert_qa_squad')" ] }, { "cell_type": "markdown", "metadata": { "id": "ygEncJxtl-nQ" }, "source": [ "## オンデバイス ML アプリ固有の入力データを読み込み、データを前処理する\n", "\n", "[TriviaQA](https://nlp.cs.washington.edu/triviaqa/) は、読解問題のデータセットで、質問、回答、エビデンスの 3 つを 1 組とした 65 万個を超えるデータが含まれます。このチュートリアルでは、このデータセットのサブセットを使用して、Model Maker ライブラリの使用方法を学習します。\n", "\n", "データを読み込むには、`--sample_size=8000` とした[コンバータ用 Python スクリプト](https://github.com/mandarjoshi90/triviaqa#miscellaneous)と一連の `web` データを実行して、TriviaQA データセットを [SQuAD1.1](https://rajpurkar.github.io/SQuAD-explorer/) 形式に変換します。次のようにして、変換コードを少し変更してください。\n", "\n", "- 文脈ドキュメントで回答が見つからなかったサンプルを省略します。\n", "- 大文字や小文字を無視し、文脈の元の解答を取得します。\n", "\n", "変換済みデータセットのアーカイブバージョンをダウンロードします。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "7tOfUr2KlgpU" }, "outputs": [], "source": [ "train_data_path = tf.keras.utils.get_file(\n", " fname='triviaqa-web-train-8000.json',\n", " origin='https://storage.googleapis.com/download.tensorflow.org/models/tflite/dataset/triviaqa-web-train-8000.json')\n", "validation_data_path = tf.keras.utils.get_file(\n", " fname='triviaqa-verified-web-dev.json',\n", " origin='https://storage.googleapis.com/download.tensorflow.org/models/tflite/dataset/triviaqa-verified-web-dev.json')" ] }, { "cell_type": "markdown", "metadata": { "id": "UfZk8GNr_1nc" }, "source": [ "また、独自のデータセットを使用しても、MobileBERT モデルをトレーニングできます。Colab でこのノートブックを実行している場合は、左のサイドバーを使ってデータをアップロードしてください。\n", "\n", "