การค้นหาข้อความช่วยให้ค้นหาข้อความที่มีความหมายคล้ายกันในคลังข้อมูล มันทำงานโดยฝังคำค้นหาลงในเวกเตอร์มิติสูงที่แสดงถึงความหมายเชิงความหมายของข้อความค้นหา ตามด้วยการค้นหาความคล้ายคลึงกันในดัชนีที่กำหนดเองที่กำหนดไว้ล่วงหน้าโดยใช้ ScaNN (เพื่อนบ้านที่ใกล้ที่สุดที่ปรับขนาดได้)
ตรงข้ามกับการจัดประเภทข้อความ (เช่น Bert natural language classifier ) การขยายจำนวนรายการที่สามารถจดจำได้ ไม่จำเป็นต้องฝึกซ้ำทั้งโมเดล สามารถเพิ่มรายการใหม่ได้โดยการสร้างดัชนีใหม่ สิ่งนี้ยังช่วยให้สามารถทำงานกับศพที่ใหญ่กว่า (100k+ รายการ) ได้
ใช้ Task Library TextSearcher
API เพื่อปรับใช้โปรแกรมค้นหาข้อความที่กำหนดเองในแอพมือถือของคุณ
คุณสมบัติหลักของ TextSearcher API
รับสตริงเดียวเป็นอินพุต ทำการแยกการฝังและค้นหาเพื่อนบ้านที่ใกล้ที่สุดในดัชนี
การประมวลผลข้อความป้อนเข้า ซึ่งรวมถึงการ สร้างโทเค็น Wordpiece หรือ Sentencepiece ในกราฟหรือนอกกราฟบนข้อความที่ป้อน
ข้อกำหนดเบื้องต้น
ก่อนใช้ TextSearcher
API จะต้องสร้างดัชนีตามคลังข้อความที่กำหนดเองเพื่อค้นหา ซึ่งสามารถทำได้โดยใช้ Model Maker Searcher API โดยทำตามและปรับบทช่วย สอน
สำหรับสิ่งนี้คุณจะต้อง:
- โมเดลตัวฝังข้อความ TFLite เช่น Universal Sentence Encoder ตัวอย่างเช่น,
- คลังข้อความของคุณ
หลังจากขั้นตอนนี้ คุณควรมีโมเดลตัวค้นหา 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.0'
// Import the GPU delegate plugin Library for GPU inference
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin:0.4.0'
}
ขั้นตอนที่ 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
ลองใช้ เครื่องมือสาธิต CLI อย่างง่ายสำหรับ TextSearcher ด้วยแบบจำลองและข้อมูลทดสอบของคุณเอง