API Thư viện tác vụ BertQuestionAnswerer tải một mô hình Bert và trả lời các câu hỏi dựa trên nội dung của một đoạn văn bản nhất định. Để biết thêm thông tin, hãy xem ví dụ về mô hình Hỏi – Đáp.
Các tính năng chính của BertQuestionAnswerer API
Nhận 2 đầu vào văn bản dưới dạng câu hỏi và bối cảnh, đồng thời xuất ra danh sách các câu trả lời có thể.
Thực hiện việc mã hoá Wordpiece hoặc Sentencepiece ngoài biểu đồ trên văn bản đầu vào.
Các mô hình BertQuestionAnswerer được hỗ trợ
Các mô hình sau đây tương thích với API BertNLClassifier.
Các mô hình do TensorFlow Lite Model Maker tạo cho tính năng Trả lời câu hỏi của BERT.
Các mô hình tuỳ chỉnh đáp ứng yêu cầu về khả năng tương thích của mô hình.
Chạy suy luận bằng Java
Bước 1: Nhập phần phụ thuộc Gradle và các chế độ cài đặt khác
Sao chép tệp mô hình .tflite vào thư mục tài sản của mô-đun Android nơi mô hình sẽ chạy. Chỉ định rằng tệp không được nén và thêm thư viện TensorFlow Lite vào tệp build.gradle của mô-đun:
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
implementation 'org.tensorflow:tensorflow-lite-task-text:0.4.4'
}
Bước 2: Chạy suy luận bằng 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);
Hãy xem mã nguồn để biết thêm chi tiết.
Chạy suy luận trong Swift
Bước 1: Nhập CocoaPods
Thêm nhóm TensorFlowLiteTaskText vào Podfile
target 'MySwiftAppWithTaskAPI' do
use_frameworks!
pod 'TensorFlowLiteTaskText', '~> 0.4.4'
end
Bước 2: Chạy suy luận bằng API
// Initialization
let mobileBertAnswerer = TFLBertQuestionAnswerer.questionAnswerer(
modelPath: mobileBertModelPath)
// Run inference
let answers = mobileBertAnswerer.answer(
context: context, question: question)
Hãy xem mã nguồn để biết thêm chi tiết.
Chạy suy luận bằng 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);
Hãy xem mã nguồn để biết thêm chi tiết.
Chạy suy luận trong Python
Bước 1: Cài đặt gói pip
pip install tflite-support
Bước 2: Sử dụng mô hình
# 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)
Hãy xem mã nguồn để biết thêm các lựa chọn định cấu hình BertQuestionAnswerer.
Kết quả mẫu
Dưới đây là ví dụ về kết quả trả lời của mô hình ALBERT.
Bối cảnh: "Rừng mưa nhiệt đới Amazon (còn gọi là Rừng Amazon) là một khu rừng mưa nhiệt đới ẩm ướt, có lá rộng trong quần xã sinh vật Amazon, bao phủ phần lớn lưu vực sông Amazon ở Nam Mỹ. Lưu vực này có diện tích 7.000.000 km2 (2.700.000 sq mi), trong đó 5.500.000 km2 (2.100.000 sq mi) là rừng mưa. Khu vực này bao gồm lãnh thổ thuộc về 9 quốc gia."
Câu hỏi: "Rừng mưa nhiệt đới Amazon ở đâu?"
Câu trả lời:
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
Hãy dùng thử công cụ minh hoạ CLI đơn giản cho BertQuestionAnswerer bằng mô hình và dữ liệu kiểm thử của riêng bạn.
Yêu cầu về khả năng tương thích của mô hình
API BertQuestionAnswerer yêu cầu một mô hình TFLite có Siêu dữ liệu mô hình TFLite bắt buộc.
Siêu dữ liệu phải đáp ứng các yêu cầu sau:
input_process_unitscho Wordpiece/Sentencepiece Tokenizer3 tensor đầu vào có tên "ids", "mask" và "segment_ids" cho đầu ra của mã hoá từ
2 tensor đầu ra có tên "end_logits" và "start_logits" để cho biết vị trí tương đối của câu trả lời trong ngữ cảnh