集成 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 模型

上下文:“亚马逊雨林或者亚马逊丛林, 英语称为 Amazonia,是亚马逊雨林中一片湿润的宽叶热带雨林 覆盖了南美洲大部分亚马逊盆地的生物群落。这个盆地 占地 700 万平方公里 (2700000 平方英里),其中 有 550 万平方公里(210 万平方英里)被雨林所覆盖。此区域 包括属于九个国家的领土。”

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

回答:

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”来指明 答案在上下文中的相对位置