集成文本搜索器

借助文本搜索功能,您可以在语料库中搜索语义相似的文本。行之有效 通过将搜索查询嵌入表示 查询的语义含义,然后在预定义查询中使用相似度搜索, 自定义索引 ScaNN (可扩缩的最近邻)。

这与文本分类(例如 Bert 自然语言分类器), 增加可以识别的训练项数量无需重新训练 整个模型。只需重新构建索引即可添加新项。这也 支持使用更大的(超过 10 万项)语料库。

使用 Task Library TextSearcher API 将自定义文本搜索器部署到 您的移动应用

TextSearcher API 的主要功能

  • 接受单个字符串作为输入,执行嵌入提取, 搜索索引的最近邻搜索。

  • 输入文本处理,包括图内或图外 词汇句子 对输入文本进行词元化处理。

前提条件

在使用 TextSearcher API 之前,需要根据 要搜索的自定义文本语料库。这可以通过使用 Model Maker Searcher API 通过遵循和调整 教程

为此,您需要:

  • TFLite 文本嵌入器模型,例如 Universal Sentence Encoder。对于 示例, <ph type="x-smartling-placeholder">
      </ph>
    • 这一模型 Colab、 针对设备端推断进行了优化。只需 6 毫秒即可查询 文本字符串。
    • 量化 一个,这比上面要小,但每次嵌入需要 38 毫秒。
  • 生成文本。

完成此步骤后,您应该会获得一个独立的 TFLite 搜索器模型(例如 mobilenet_v3_searcher.tflite),即原始文本嵌入器模型, 附加到 TFLite 模型元数据

使用 Java 运行推理

第 1 步:导入 Gradle 依赖项和其他设置

.tflite 搜索器模型文件复制到 Android 的资源目录 将在其中运行模型的模块。指定不应将文件 然后将 TensorFlow Lite 库添加到模块的 build.gradle 中, 文件:

android {
    // Other settings

    // Specify tflite index file should not be compressed for the app apk
    aaptOptions {
        noCompress "tflite"
    }

}

dependencies {
    // Other dependencies

    // Import the Task Vision Library dependency
    implementation 'org.tensorflow:tensorflow-lite-task-vision:0.4.4'
    // Import the GPU delegate plugin Library for GPU inference
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin:0.4.4'
}

第 2 步:使用模型

// Initialization
TextSearcherOptions options =
    TextSearcherOptions.builder()
        .setBaseOptions(BaseOptions.builder().useGpu().build())
        .setSearcherOptions(
            SearcherOptions.builder().setL2Normalize(true).build())
        .build();
TextSearcher textSearcher =
    textSearcher.createFromFileAndOptions(context, modelFile, options);

// Run inference
List<NearestNeighbor> results = textSearcher.search(text);

请参阅 源代码和 javadoc 了解用于配置 TextSearcher 的更多选项。

使用 C++ 运行推理

// Initialization
TextSearcherOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
options.mutable_embedding_options()->set_l2_normalize(true);
std::unique_ptr<TextSearcher> text_searcher = TextSearcher::CreateFromOptions(options).value();

// Run inference with your input, `input_text`.
const SearchResult result = text_searcher->Search(input_text).value();

请参阅 源代码 了解用于配置 TextSearcher 的更多选项。

在 Python 中运行推理

第 1 步:安装 TensorFlow Lite 支持 Pypi 软件包。

您可以使用以下工具来安装 TensorFlow Lite 支持 Pypi 软件包 命令:

pip install tflite-support

第 2 步:使用模型

from tflite_support.task import text

# Initialization
text_searcher = text.TextSearcher.create_from_file(model_path)

# Run inference
result = text_searcher.search(text)

请参阅 源代码 了解用于配置 TextSearcher 的更多选项。

示例结果

Results:
 Rank#0:
  metadata: The sun was shining on that day.
  distance: 0.04618
 Rank#1:
  metadata: It was a sunny day.
  distance: 0.10856
 Rank#2:
  metadata: The weather was excellent.
  distance: 0.15223
 Rank#3:
  metadata: The cat is chasing after the mouse.
  distance: 0.34271
 Rank#4:
  metadata: He was very happy with his newly bought car.
  distance: 0.37703

尝试 适用于 TextSearcher 的 CLI 演示工具 模型和测试数据。