テキスト検索では、コーパス内の意味的に類似したテキストを検索できます。これは、検索クエリをクエリのセマンティックな意味を表す高次元ベクトルに埋め込み、次に ScaNN(Scalable Nearest Neighbors)を使用して、事前定義されたカスタム インデックスで類似性検索を行うことで機能します。
テキスト分類(Bert 自然言語分類子など)とは異なり、認識できるアイテムの数を増やすためにモデル全体を再トレーニングする必要はありません。新しいアイテムは、インデックスを再構築するだけで追加できます。これにより、大規模な(10 万個以上のアイテムを含む)コーパスを扱うこともできます。
Task Library TextSearcher API を使用して、カスタム テキスト検索ツールをモバイルアプリにデプロイします。
TextSearcher API の主な機能
1 つの文字列を入力として受け取り、エンベディング抽出とインデックス内の最近傍検索を実行します。
入力テキストの処理。入力テキストに対するグラフ内またはグラフ外の Wordpiece または Sentencepiece トークン化など。
前提条件
TextSearcher API を使用する前に、検索対象のテキストのカスタム コーパスに基づいてインデックスを構築する必要があります。これは、チュートリアルに沿って Model Maker Searcher API を使用して実現できます。
これには、次のものが必要です。
- ユニバーサル センテンス エンコーダなどの TFLite テキスト エンベッダー モデル。例:
- テキスト コーパス。
このステップを完了すると、スタンドアロンの 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
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 デモツールを試してください。