Task Library BertQuestionAnswerer
API 會載入 Bert 模型,並根據指定的段落內容回答問題。如需更多資訊,請參閱此處的問答模型文件。
BertQuestionAnswerer API 的主要功能
接受兩個文字輸入作為問題和情境,並輸出可能的答案清單。
對輸入文字執行圖形外的 Wordpiece 或 Sentencepiece 權杖化。
支援的 BertQuestionAnswerer 模型
下列模型與 BertNLClassifier
API 相容。
在 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 (NNAPI is included)
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 模型中繼資料。
中繼資料應符合下列需求
適用於 Wordpiece/Sentencepiece 權杖產生器的
input_process_units
3 個輸入張量,名稱為「ids」、「mask」和「segment_ids」,用於權杖產生器的輸出
2 個輸出張量,名稱為「end_logits」和「start_logits」,以指示答案在情境中的相對位置