작업 라이브러리 BertQuestionAnswerer API는 Bert 모델을 로드하고 주어진 구절의 내용을 기반으로 질문에 답변합니다. 자세한 내용은 질문-답변 모델의 예를 참고하세요.
BertQuestionAnswerer API의 주요 기능
질문과 맥락이라는 두 가지 텍스트 입력을 받아 가능한 답변 목록을 출력합니다.
입력 텍스트에 대해 그래프 외부 Wordpiece 또는 Sentencepiece 토큰화를 실행합니다.
지원되는 BertQuestionAnswerer 모델
다음 모델은 BertNLClassifier API와 호환됩니다.
TensorFlow Lite Model Maker for BERT Question Answer로 생성된 모델
모델 호환성 요구사항을 충족하는 맞춤 모델
Java에서 추론 실행
1단계: Gradle 종속 항목 및 기타 설정 가져오기
모델이 실행될 Android 모듈의 애셋 디렉터리에 .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
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 가져오기
Podfile에 TensorFlowLiteTaskText pod 추가
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,000km2 (2,700,000sqmi)에 걸쳐 있으며, 이 중 5,500,000km2 (2,100,000sqmi)가 열대 우림으로 덮여 있습니다. 이 지역에는 9개 국가에 속한 영토가 포함됩니다.'
질문: '아마존 열대 우림은 어디에 있어?'
정답:
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 모델이 필요합니다.
메타데이터는 다음 요구사항을 충족해야 합니다.
Wordpiece/Sentencepiece 토큰화 도구용
input_process_units토큰화 도구의 출력을 위한 이름이 'ids', 'mask', 'segment_ids'인 입력 텐서 3개
컨텍스트에서 답변의 상대적 위치를 나타내는 'end_logits' 및 'start_logits'라는 이름의 출력 텐서 2개