BERT 質問応答を統合する

タスク ライブラリの BertQuestionAnswerer API が BERT モデルと回答を読み込む 生成する質問を作成できます。詳しくは、 質問応答モデルのサンプル。

BErtQuestionAnswerer API の主な機能

  • 2 つのテキスト入力を質問とコンテキストとして受け取り、候補となるリストを出力します。 答えます。

  • 入力に対してグラフ外のワードピースまたは文のトークン化を行う あります。

サポートされている BertQuestionAnswerer モデル

次のモデルは BertNLClassifier API と互換性があります。

Java で推論を実行する

ステップ 1: Gradle の依存関係とその他の設定をインポートする

.tflite モデルファイルを Android モジュールのアセット ディレクトリにコピーします。 モデルを実行する場所を指定しますファイルを圧縮しないように指定する。 TensorFlow Lite ライブラリをモジュールの build.gradle ファイルに追加します。

android {
    // Other settings

    // Specify tflite file should not be compressed for the app apk
    aaptOptions {
        noCompress "tflite"
    }

}

dependencies {
    // Other dependencies

    // Import the Task Text Library dependency
    implementation 'org.tensorflow:tensorflow-lite-task-text:0.4.4'
}

ステップ 2: API を使用して推論を実行する

// Initialization
BertQuestionAnswererOptions options =
    BertQuestionAnswererOptions.builder()
        .setBaseOptions(BaseOptions.builder().setNumThreads(4).build())
        .build();
BertQuestionAnswerer answerer =
    BertQuestionAnswerer.createFromFileAndOptions(
        androidContext, modelFile, options);

// Run inference
List<QaAnswer> answers = answerer.answer(contextOfTheQuestion, questionToAsk);

詳しくは、 ソースコード をご覧ください。

Swift で推論を実行する

ステップ 1: CocoaPods をインポートする

Podfile に TensorFlowLiteTaskText Pod を追加する

target 'MySwiftAppWithTaskAPI' do
  use_frameworks!
  pod 'TensorFlowLiteTaskText', '~> 0.4.4'
end

ステップ 2: API を使用して推論を実行する

// Initialization
let mobileBertAnswerer = TFLBertQuestionAnswerer.questionAnswerer(
      modelPath: mobileBertModelPath)

// Run inference
let answers = mobileBertAnswerer.answer(
      context: context, question: question)

詳しくは、 ソースコード をご覧ください。

C++ で推論を実行する

// Initialization
BertQuestionAnswererOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<BertQuestionAnswerer> answerer = BertQuestionAnswerer::CreateFromOptions(options).value();

// Run inference with your inputs, `context_of_question` and `question_to_ask`.
std::vector<QaAnswer> positive_results = answerer->Answer(context_of_question, question_to_ask);

詳しくは、 ソースコード をご覧ください。

Python で推論を実行する

ステップ 1: pip パッケージをインストールする

pip install tflite-support

ステップ 2: モデルを使用する

# Imports
from tflite_support.task import text

# Initialization
answerer = text.BertQuestionAnswerer.create_from_file(model_path)

# Run inference
bert_qa_result = answerer.answer(context, question)

詳しくは、 ソースコード BertQuestionAnswerer を構成するその他のオプションをご覧ください。

検索結果の例

こちらは、Google Cloud で提供されている ALBERT モデル

コンテキスト: 「アマゾン熱帯雨林、あるいはアマゾン ジャングル、 英語(アマゾニア)は、アマゾンの湿った広葉熱帯熱帯雨林です この生物群系は、南米のアマゾン盆地の大部分を占めています。この洗面ボウル 7,000,000 km2(2,700,000 平方マイル)を含み、そのうちの 5,500,000 km2(2,100,000 平方マイル)が熱帯雨林に覆われています。この地域 9 か国に属する領土が含まれています

質問: 「アマゾン熱帯雨林はどこにありますか?」

解答:

answer[0]:  'South America.'
logit: 1.84847, start_index: 39, end_index: 40
answer[1]:  'most of the Amazon basin of South America.'
logit: 1.2921, start_index: 34, end_index: 40
answer[2]:  'the Amazon basin of South America.'
logit: -0.0959535, start_index: 36, end_index: 40
answer[3]:  'the Amazon biome that covers most of the Amazon basin of South America.'
logit: -0.498558, start_index: 28, end_index: 40
answer[4]:  'Amazon basin of South America.'
logit: -0.774266, start_index: 37, end_index: 40

シンプルな BertQuestionAnswerer の CLI デモツール 独自のモデルとテストデータで トレーニングできます

モデルの互換性要件

BertQuestionAnswerer API は、必須の TFLite モデルを想定しています。 TFLite モデル メタデータ

メタデータは次の要件を満たす必要があります。

  • input_process_units: Wordpiece/Sentencepiece Tokenizer

  • 名前が「ids」、「mask」の 3 つの入力テンソル「segment_ids」出力を トークナイザ

  • 「end_logits」という名前の 2 つの出力テンソル「start_logits」アラートの取り込み時に コンテキストにおける回答の相対的な位置