टास्क लाइब्रेरी BertQuestionAnswerer
API एक बर्ट मॉडल को लोड करता है और दिए गए पैसेज की सामग्री के आधार पर सवालों के जवाब देता है। अधिक जानकारी के लिए, प्रश्न-उत्तर मॉडल के लिए दस्तावेज़ यहाँ देखें।
BertQuestionAnswerer API की मुख्य विशेषताएं
प्रश्न और संदर्भ के रूप में दो टेक्स्ट इनपुट लेता है और संभावित उत्तरों की एक सूची आउटपुट करता है।
इनपुट टेक्स्ट पर आउट-ऑफ़-ग्राफ़ वर्डपीस या सेंटेंसपीस टोकननाइज़ेशन करता है।
समर्थित BertQuestionAnswerer मॉडल
निम्नलिखित मॉडल BertNLClassifier
API के साथ संगत हैं।
BERT प्रश्न उत्तर के लिए TensorFlow Lite Model Maker द्वारा बनाए गए मॉडल।
कस्टम मॉडल जो मॉडल संगतता आवश्यकताओं को पूरा करते हैं।
जावा में अनुमान चलाएँ
चरण 1: ग्रैडल निर्भरता और अन्य सेटिंग्स आयात करें
एंड्रॉइड मॉड्यूल की संपत्ति निर्देशिका में .tflite
मॉडल फ़ाइल की प्रतिलिपि बनाएँ जहां मॉडल चलाया जाएगा। निर्दिष्ट करें कि फ़ाइल को संपीड़ित नहीं किया जाना चाहिए, और 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);
अधिक जानकारी के लिए स्रोत कोड देखें।
स्विफ्ट में अनुमान चलाएँ
चरण 1: कोकोपोड्स आयात करें
पॉडफाइल में TensorFlowLiteTaskText पॉड जोड़ें
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)
अधिक जानकारी के लिए स्रोत कोड देखें।
सी ++ में अनुमान चलाएं
// 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);
अधिक जानकारी के लिए स्रोत कोड देखें।
पायथन में अनुमान चलाएँ
चरण 1: पाइप पैकेज स्थापित करें
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 (2,700,000 वर्ग मील) शामिल हैं। ), जिनमें से 5,500,000 किमी 2 (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
अपने स्वयं के मॉडल और परीक्षण डेटा के साथ BertQuestionAnswerer के लिए सरल CLI डेमो टूल आज़माएं ।
मॉडल संगतता आवश्यकताएँ
BertQuestionAnswerer
API अनिवार्य TFLite मॉडल मेटाडेटा के साथ TFLite मॉडल की अपेक्षा करता है।
मेटाडेटा को निम्नलिखित आवश्यकताओं को पूरा करना चाहिए:
input_process_units
/Sentencepiece Tokenizer के लिए input_process_unitsटोकननाइज़र के आउटपुट के लिए "आईडी", "मास्क" और "सेगमेंट_आईड्स" नामों के साथ 3 इनपुट टेंसर
संदर्भ में उत्तर की सापेक्ष स्थिति को इंगित करने के लिए "end_logits" और "start_logits" नामों के साथ 2 आउटपुट टेंसर