Task Library BertQuestionAnswerer API 會載入 Bert 模型,並根據特定段落的內容回答問題。詳情請參閱問答模型範例。
BertQuestionAnswerer API 的主要功能
以兩段文字做為問題和背景資訊,並輸出可能的答案清單。
對輸入文字執行圖外 Wordpiece 或 Sentencepiece 權杖化。
支援的 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 的其他選項,請參閱原始碼。
搜尋結果範例
以下是 ALBERT 模型的答案結果範例。
背景:「亞馬遜雨林又稱亞馬遜叢林,在英文中也稱為亞馬遜尼亞,是亞馬遜生物群系中潮濕的闊葉熱帶雨林,涵蓋南美洲亞馬遜盆地的大部分地區。這個盆地面積達 7,000,000 平方公里 (2,700,000 平方英里),其中 5,500,000 平方公里 (2,100,000 平方英里) 覆蓋著雨林。這個地區包含九個國家的領土。」
問題:「亞馬遜雨林在哪裡?」
答案:
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 分詞器3 個輸入張量,名稱分別為「ids」、「mask」和「segment_ids」,用於標記器輸出內容
2 個輸出張量,名稱分別為「end_logits」和「start_logits」,用來指出答案在內容中的相對位置