整合文字搜尋器

文字搜尋可讓您在語料庫中搜尋語意相似的文字。其運作方式是將搜尋查詢嵌入高維度向量 (代表查詢的語意),然後使用 ScaNN (可擴充最近鄰演算法) 在預先定義的自訂索引中進行相似度搜尋。

與文字分類 (例如Bert 自然語言分類器) 相反,擴充可辨識項目的數量不需要重新訓練整個模型。只需重新建構索引即可新增項目。這也能處理較大的語料庫 (超過 10 萬個項目)。

使用 Task Library TextSearcher API,將您的自訂文字搜尋器部署到您的行動應用程式中。

TextSearcher API 的主要功能

  • 採用單一字串做為輸入,在索引中執行嵌入擷取和最近鄰搜尋。

  • 輸入文字處理,包括圖形內或圖形外的輸入文字 WordpieceSentencepiece 符號化。

先決條件

使用 TextSearcher API 之前,需要根據要搜尋的自訂文字語料庫建立索引。這可以使用 Model Maker Searcher API,並遵循及調整教學課程來達成。

為此,您需要

  • TFLite 文字嵌入器模型,例如 Universal Sentence Encoder。例如:
    • 這個Colab 中重新訓練的模型,已針對裝置端推論最佳化。在 Pixel 6 上查詢文字字串僅需 6 毫秒。
    • 量化模型,比上述模型小,但每次嵌入需要 38 毫秒。
  • 您的文字語料庫。

在此步驟之後,您應該會有獨立的 TFLite 搜尋器模型 (例如 mobilenet_v3_searcher.tflite),這是原始文字嵌入器模型,索引已附加到 TFLite 模型中繼資料

在 Java 中執行推論

步驟 1:匯入 Gradle 依附元件和其他設定

.tflite 搜尋器模型檔案複製到將執行模型的 Android 模組的 assets 目錄。指定不應壓縮檔案,並將 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 (NNAPI is included)
    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 Support Pypi 套件。

您可以使用下列指令安裝 TensorFlow Lite Support 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 示範工具