BERT 질문 답변자 통합

Task Library BertQuestionAnswerer API는 BERT 모델을 로드하고 주어진 구절의 내용을 기반으로 한 질문을 할 수 있습니다. 자세한 내용은 질문-답변 모델을 예로 들 수 있습니다.

BertQuestionAnswerer API의 주요 기능

  • 텍스트 입력 두 개를 질문과 컨텍스트로 취해 가능한 목록을 출력합니다. 있습니다.

  • 입력 시 그래프 외부의 Word피스 또는 Sentencestone 토큰화를 수행합니다. 있습니다.

지원되는 BERtQuestionAnswerer 모델

다음 모델은 BertNLClassifier API와 호환됩니다.

Java에서 추론 실행

1단계: Gradle 종속 항목 및 기타 설정 가져오기

.tflite 모델 파일을 Android 모듈의 assets 디렉터리에 복사합니다. 지정할 수도 있습니다 파일을 압축하지 않도록 지정합니다. 모듈의 build.gradle 파일에 TensorFlow Lite 라이브러리를 추가합니다.

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 포드 추가하기

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,000km2 (2,700,000평방마일)이며 이 중 5,500,000km2 (2,100,000제곱마일)가 열대 우림으로 덮여 있습니다. 이 지역 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 모델 메타데이터.

메타데이터는 다음 요구사항을 충족해야 합니다.

  • Word피스/Sentencestone Tokenizer: input_process_units

  • 이름이 'ids', 'mask'인 입력 텐서 3개 및 'segment_ids' 출력 tokenizer

  • 이름이 'end_logits'인 출력 텐서 2개 및 'start_logits' 는 맥락에서 답변의 상대적 위치