ไลบรารีงาน BertQuestionAnswerer
API โหลดโมเดล Bert และตอบคำถามตามเนื้อหาของข้อความที่กำหนด สำหรับข้อมูลเพิ่มเติม โปรดดูเอกสารประกอบสำหรับแบบจำลองคำถาม-คำตอบ ที่นี่
คุณสมบัติหลักของ BertQuestionAnswerer API
ใช้การป้อนข้อความสองข้อความเป็นคำถามและบริบท แล้วแสดงรายการคำตอบที่เป็นไปได้
ดำเนินการโทเค็น Wordpiece หรือ Sentencepiece ที่ไม่อยู่ในกราฟบนข้อความที่ป้อน
รองรับ BertQuestionAnswerer รุ่น
รุ่นต่อไปนี้เข้ากันได้กับ BertNLClassifier
API
โมเดลที่สร้างโดย TensorFlow Lite Model Maker สำหรับคำตอบของ BERT
โมเดลแบบกำหนดเองที่ตรงตาม ข้อกำหนดความเข้ากันได้ ของโมเดล
เรียกใช้การอนุมานใน 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.3.0'
}
ขั้นตอนที่ 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.2.0'
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 model
บริบท: "ป่าฝนอเมซอนหรืออีกทางหนึ่งคือ ป่าอเมซอน หรือที่รู้จักกันในชื่อภาษาอังกฤษว่าอเมซอนเป็นป่าฝนเขตร้อนที่มีใบกว้างชื้นในชีวนิเวศของอเมซอนซึ่งครอบคลุมพื้นที่ส่วนใหญ่ของแอมะซอนในอเมริกาใต้ แอ่งนี้ครอบคลุมพื้นที่ 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
สำหรับ Wordpiece/Sentencepiece Tokenizer3 เทนเซอร์อินพุตที่มีชื่อ "ids", "mask" และ "segment_ids" สำหรับเอาต์พุตของ tokenizer
2 เทนเซอร์เอาต์พุตที่มีชื่อ "end_logits" และ "start_logits" เพื่อระบุตำแหน่งสัมพัทธ์ของคำตอบในบริบท