Task Library BertQuestionAnswerer API は、Bert モデルを読み込み、特定の文章の内容に基づいて質問に回答します。詳細については、質問応答モデルの例をご覧ください。
BertQuestionAnswerer API の主な機能
質問とコンテキストの 2 つのテキスト入力を受け取り、考えられる回答のリストを出力します。
入力テキストに対してグラフ外の Wordpiece または Sentencepiece トークン化を実行します。
サポートされている BertQuestionAnswerer モデル
次のモデルは BertNLClassifier API と互換性があります。
TensorFlow Lite Model Maker for BERT Question Answer で作成されたモデル。
モデルの互換性要件を満たすカスタムモデル。
Java で推論を実行する
ステップ 1: Gradle の依存関係とその他の設定をインポートする
.tflite モデルファイルを、モデルが実行される Android モジュールの assets ディレクトリにコピーします。ファイルを圧縮しないように指定し、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 ポッドを追加する
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 を構成するその他のオプションについては、ソースコードをご覧ください。
検索結果の例
ALBERT モデルの回答結果の例を次に示します。
コンテキスト: 「アマゾン熱帯雨林は、アマゾン ジャングルとも呼ばれ、英語ではアマゾニアとも呼ばれます。アマゾン バイオームの湿った広葉樹の熱帯雨林で、南米のアマゾン盆地の大部分を占めています。この流域は 700 万 km2 で、そのうち 550 万 km2 が熱帯雨林に覆われています。この地域には、9 つの国に属する地域が含まれます。」
質問: 「アマゾン熱帯雨林はどこにありますか?」
Answers:
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 モデルを想定しています。
メタデータは次の要件を満たしている必要があります。
Wordpiece/Sentencepiece Tokenizer の
input_process_unitsトークナイザーの出力用の名前「ids」、「mask」、「segment_ids」を持つ 3 つの入力テンソル
コンテキスト内の回答の相対位置を示す「end_logits」と「start_logits」という名前の 2 つの出力テンソル