Task Library BertQuestionAnswerer
API โหลดโมเดล Bert และตอบคำถามตามเนื้อหาของข้อความที่กำหนด สำหรับข้อมูลเพิ่มเติม โปรดดูเอกสารประกอบสำหรับโมเดลคำถาม-คำตอบ ที่นี่
คุณสมบัติที่สำคัญของ BertQuestionAnswerer API
รับอินพุตข้อความสองรายการเป็นคำถามและบริบท และแสดงรายการคำตอบที่เป็นไปได้
ดำเนินการโทเค็น Wordpiece หรือ Sentencepiece นอกกราฟในข้อความที่ป้อน
รองรับโมเดล BertQuestionAnswerer
รุ่นต่อไปนี้เข้ากันได้กับ BertNLClassifier
API
โมเดลที่สร้างโดย TensorFlow Lite Model Maker สำหรับ BERT Question Answer
โมเดลแบบกำหนดเองที่ตรงตาม ข้อกำหนดความเข้ากันได้ของโมเดล
เรียกใช้การอนุมานใน Java
ขั้นตอนที่ 1: นำเข้าการพึ่งพา Gradle และการตั้งค่าอื่นๆ
คัดลอกไฟล์โมเดล .tflite
ไปยังไดเร็กทอรีทรัพย์สินของโมดูล Android ที่จะเรียกใช้โมเดล ระบุว่าไม่ควรบีบอัดไฟล์ และเพิ่มไลบรารี TensorFlow Lite ลงในไฟล์ build.gradle
ของโมดูล:
android {
// Other settings
// Specify tflite file should not be compressed for the app apk
aaptOptions {
noCompress "tflite"
}
}
dependencies {
// Other dependencies
// Import the Task Text Library dependency (NNAPI is included)
implementation 'org.tensorflow:tensorflow-lite-task-text:0.4.4'
}
ขั้นตอนที่ 2: เรียกใช้การอนุมานโดยใช้ API
// Initialization
BertQuestionAnswererOptions options =
BertQuestionAnswererOptions.builder()
.setBaseOptions(BaseOptions.builder().setNumThreads(4).build())
.build();
BertQuestionAnswerer answerer =
BertQuestionAnswerer.createFromFileAndOptions(
androidContext, modelFile, options);
// Run inference
List<QaAnswer> answers = answerer.answer(contextOfTheQuestion, questionToAsk);
ดู ซอร์สโค้ด สำหรับรายละเอียดเพิ่มเติม
เรียกใช้การอนุมานใน Swift
ขั้นตอนที่ 1: นำเข้า CocoaPods
เพิ่มพ็อด TensorFlowLiteTaskText ใน Podfile
target 'MySwiftAppWithTaskAPI' do
use_frameworks!
pod 'TensorFlowLiteTaskText', '~> 0.4.4'
end
ขั้นตอนที่ 2: เรียกใช้การอนุมานโดยใช้ API
// Initialization
let mobileBertAnswerer = TFLBertQuestionAnswerer.questionAnswerer(
modelPath: mobileBertModelPath)
// Run inference
let answers = mobileBertAnswerer.answer(
context: context, question: question)
ดู ซอร์สโค้ด สำหรับรายละเอียดเพิ่มเติม
เรียกใช้การอนุมานใน C ++
// Initialization
BertQuestionAnswererOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<BertQuestionAnswerer> answerer = BertQuestionAnswerer::CreateFromOptions(options).value();
// Run inference with your inputs, `context_of_question` and `question_to_ask`.
std::vector<QaAnswer> positive_results = answerer->Answer(context_of_question, question_to_ask);
ดู ซอร์สโค้ด สำหรับรายละเอียดเพิ่มเติม
เรียกใช้การอนุมานใน Python
ขั้นตอนที่ 1: ติดตั้งแพ็คเกจ pip
pip install tflite-support
ขั้นตอนที่ 2: การใช้แบบจำลอง
# Imports
from tflite_support.task import text
# Initialization
answerer = text.BertQuestionAnswerer.create_from_file(model_path)
# Run inference
bert_qa_result = answerer.answer(context, question)
ดู ซอร์สโค้ด สำหรับตัวเลือกเพิ่มเติมในการกำหนดค่า BertQuestionAnswerer
ตัวอย่างผลลัพธ์
นี่คือตัวอย่างผลลัพธ์คำตอบของ โมเดล ALBERT
บริบท: "ป่าฝนอเมซอน หรืออีกทางหนึ่งคือ ป่าอเมซอน หรือที่รู้จักในภาษาอังกฤษว่า อเมซอนเนีย เป็นป่าฝนเขตร้อนชื้นใบกว้างในชีวนิเวศอเมซอน ซึ่งครอบคลุมพื้นที่ส่วนใหญ่ของแอ่งอเมซอนในอเมริกาใต้ แอ่งนี้ครอบคลุมพื้นที่ 7,000,000 ตารางกิโลเมตร (2,700,000 ตารางไมล์) ) ซึ่งมีป่าฝนปกคลุมพื้นที่ 5,500,000 ตารางกิโลเมตร (2,100,000 ตารางไมล์) ภูมิภาคนี้รวมถึงดินแดนของเก้าประเทศด้วย"
คำถาม: ป่าฝนอเมซอนอยู่ที่ไหน?
คำตอบ:
answer[0]: 'South America.'
logit: 1.84847, start_index: 39, end_index: 40
answer[1]: 'most of the Amazon basin of South America.'
logit: 1.2921, start_index: 34, end_index: 40
answer[2]: 'the Amazon basin of South America.'
logit: -0.0959535, start_index: 36, end_index: 40
answer[3]: 'the Amazon biome that covers most of the Amazon basin of South America.'
logit: -0.498558, start_index: 28, end_index: 40
answer[4]: 'Amazon basin of South America.'
logit: -0.774266, start_index: 37, end_index: 40
ลองใช้ เครื่องมือสาธิต CLI ง่ายๆ สำหรับ BertQuestionAnswerer ด้วยโมเดลและข้อมูลการทดสอบของคุณเอง
ข้อกำหนดความเข้ากันได้ของโมเดล
BertQuestionAnswerer
API คาดว่าจะมีโมเดล TFLite พร้อมด้วย ข้อมูลเมตาของโมเดล TFLite ที่บังคับ
ข้อมูลเมตาควรเป็นไปตามข้อกำหนดต่อไปนี้:
input_process_units
สำหรับ Tokenizer ของ Wordpiece/Sentencepieceเทนเซอร์อินพุต 3 ตัวที่มีชื่อ "ids", "mask" และ "segment_ids" สำหรับเอาต์พุตของโทเค็น
เทนเซอร์เอาท์พุต 2 ตัวที่มีชื่อ "end_logits" และ "start_logits" เพื่อระบุตำแหน่งสัมพัทธ์ของคำตอบในบริบท