การค้นหาข้อความช่วยให้ค้นหาข้อความที่คล้ายกันในเชิงความหมายในคลังข้อมูลได้ โดยจะทำงาน ด้วยการฝังคำค้นหาลงในเวกเตอร์ที่มีมิติสูงซึ่งแสดงถึง ความหมายเชิงความหมายของคำค้นหา ตามด้วยการค้นหาความคล้ายคลึงในดัชนีที่กำหนดไว้ล่วงหน้า ที่กำหนดเองโดยใช้ ScaNN (Scalable Nearest Neighbors)
การขยายจำนวนรายการที่ระบบจดจำได้ไม่จำเป็นต้องฝึกโมเดลทั้งหมดอีกครั้ง ซึ่งแตกต่างจากการจัดประเภทข้อความ (เช่น ตัวแยกประเภทภาษาธรรมชาติของ Bert) คุณเพิ่มรายการใหม่ได้โดย เพียงแค่สร้างดัชนีใหม่ นอกจากนี้ยังช่วยให้ทำงานกับคลังข้อมูลขนาดใหญ่ (100,000 รายการขึ้นไป) ได้ด้วย
ใช้ 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 searcher ไปยังไดเรกทอรีชิ้นงานของโมดูล 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
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: ติดตั้งแพ็กเกจ Pypi สำหรับการรองรับ TensorFlow Lite
คุณสามารถติดตั้งแพ็กเกจ 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 กับโมเดลและข้อมูลทดสอบของคุณเอง