שלב את משיב השאלות של BERT

ספריית המשימות BertQuestionAnswerer API טוענת מודל של Bert ועונה על שאלות על סמך התוכן של קטע נתון. למידע נוסף, עיין בתיעוד עבור מודל השאלות-תשובה כאן .

תכונות מפתח של BertQuestionAnswerer API

  • לוקח שני קלט טקסט כשאלה והקשר ומוציא רשימה של תשובות אפשריות.

  • מבצע אסימוני Wordpiece או Sentencepiece מחוץ לגרף בטקסט קלט.

נתמכים בדגמי BertQuestionAnswerer

הדגמים הבאים תואמים ל- BertNLClassifier API.

הפעל הסקה ב-Java

שלב 1: ייבא תלות Gradle והגדרות אחרות

העתק את קובץ מודל .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: ייבוא ​​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 .

הקשר: "יער הגשם של האמזונס, לחילופין, ג'ונגל האמזונס, הידוע גם באנגלית בשם Amazonia, הוא יער גשם טרופי רחב עלים לח בביום האמזונס, המכסה את רוב אגן האמזונס של דרום אמריקה. אגן זה מקיף 7,000,000 קמ"ר (2,700,000 sq mi) ), מתוכם 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 עם מודל ונתוני בדיקה משלך.

דרישות תאימות לדגם

ה-API BertQuestionAnswerer מצפה למודל TFLite עם מטא נתונים חובה של מודל TFLite .

המטא נתונים צריכים לעמוד בדרישות הבאות:

  • input_process_units עבור Wordpiece/Sentencepiece Tokenizer

  • 3 טנסור קלט עם שמות "ID", "mask" ו-"segment_ids" לפלט של הטוקנייזר

  • 2 טנסור פלט עם השמות "end_logits" ו-"start_logits" לציון המיקום היחסי של התשובה בהקשר