텍스트 검색자 통합

텍스트 검색을 사용하면 코퍼스에서 의미론적으로 유사한 텍스트를 검색할 수 있습니다. 작동함 검색어를 고차원 벡터에 삽입하여 검색어의 의미론적 의미와 사전 정의된 커스텀 색인 사용 ScaNN (확장 가능한 최근접 이웃).

텍스트 분류 (예: 버트 자연어 분류 기준) 인식할 수 있는 항목 수를 늘리기 위해 다시 학습할 필요가 없습니다. 모델을 학습시킬 수 있습니다 색인을 다시 빌드하기만 하면 새 항목을 추가할 수 있습니다. 또한 대규모 (10만 개 이상의 항목) 코퍼스로 작업할 수 있습니다.

Task Library TextSearcher API를 사용하여 커스텀 텍스트 검색기를 배포 있습니다.

TextSearcher API의 주요 기능

  • 단일 문자열을 입력으로 받아 임베딩 추출을 수행하고 최근접 이웃 검색을 수행합니다.

  • 인 그래프 또는 그래프 외부를 포함한 입력 텍스트 처리 워드피스 또는 문장 토큰화에 대해 살펴봤습니다

기본 요건

TextSearcher API를 사용하기 전에 다음을 기반으로 색인을 빌드해야 합니다. 텍스트 코퍼스를 생성합니다. 이렇게 하려면 Model Maker Searcher API 이를 기반으로 튜토리얼에서 확인하세요.

이를 위해 다음이 필요합니다.

  • TFLite 텍스트 삽입기 모델을 예로 들 수 있습니다. 대상 예를 들어 <ph type="x-smartling-placeholder">
      </ph>
    • 하나 재교육을 받았다면 Colab, 이는 기기 내 추론에 최적화되어 있습니다. 쿼리하는 데 단 6ms밖에 걸리지 않습니다 Pixel 6의 텍스트 문자열입니다.
    • 양자화 1로, 위의 코드보다 작지만 임베딩마다 38ms가 걸립니다.
  • 텍스트 코퍼스를 생성합니다.

이 단계를 완료하면 독립형 TFLite searcher 모델 (예: mobilenet_v3_searcher.tflite)는 색인 TFLite 모델 메타데이터.

Java에서 추론 실행

1단계: Gradle 종속 항목 및 기타 설정 가져오기

.tflite 검색기 모델 파일을 Android의 assets 디렉터리에 복사합니다. 모듈을 정의합니다 파일이 모듈의 build.gradle에 TensorFlow Lite 라이브러리를 추가합니다. 파일:

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 데모 도구 모델을 학습시킬 수 있습니다