テキスト検索を使用すると、コーパス内で意味的に類似したテキストを検索できます。これは、クエリの意味論的な意味を表す高次元ベクトルに検索クエリを埋め込み、その後、 ScaNN (Scalable Nearest Neighbors) を使用して事前定義されたカスタム インデックスで類似性検索を行うことによって機能します。
テキスト分類 (例: Bert 自然言語分類子) とは対照的に、認識できる項目の数を増やす場合、モデル全体を再トレーニングする必要はありません。新しい項目はインデックスを再構築するだけで追加できます。これにより、より大規模な (100,000 項目以上の) コーパスの操作も可能になります。
タスク ライブラリTextSearcher
API を使用して、カスタム テキスト サーチャーをモバイル アプリにデプロイします。
TextSearcher API の主な機能
単一の文字列を入力として受け取り、インデックス内の埋め込み抽出と最近傍検索を実行します。
前提条件
TextSearcher
API を使用する前に、検索対象のテキストのカスタム コーパスに基づいてインデックスを構築する必要があります。これは、 Model Maker Searcher APIを使用してチュートリアルに従い、適応させることで実現できます。
このためには以下が必要になります。
- Universal Sentence Encoder などの TFLite テキスト エンベッダー モデル。例えば、
- テキストのコーパス。
この手順を完了すると、スタンドアロンの 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 (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);
TextSearcher
構成するためのその他のオプションについては、ソース コードと javadoc を参照してください。
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 デモ ツールを試してください。