集成 BERT 问答器

Task 库 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
    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 模型的回答结果示例。

Context: "The Amazon rainforest, alternatively, the Amazon Jungle, also known in English as Amazonia, is a moist broadleaf tropical rainforest in the Amazon biome that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 km2 (2,700,000 sq mi), of which 5,500,000 km2 (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations."

问题:“亚马逊雨林在哪里?”

答案:

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”,用于指示答案在上下文中的相对位置