ゼロショット分類とQNLI

Pythonで学ぶ自然言語処理(NLP)

Fouad Trad

Machine Learning Engineer

ゼロショット分類

  • 未学習のラベルへテキストを割り当て可能
  • 自然言語の推論で出力を得る
  • 用途:
    • コンテンツのタグ付け
    • カスタマーサポート
    • ニュース記事のフィルタリング

テキスト「The national football team won the cup yesterday」を、sports・technology・healthの3カテゴリで分類する図。

Pythonで学ぶ自然言語処理(NLP)

ゼロショット分類パイプライン

from transformers import pipeline

zero_shot_classifier = pipeline(
task="zero-shot-classification",
model="MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli" )
text = "The national football team won the cup yesterday."
candidate_labels = ["sports", "technology", "health"]
result = zero_shot_classifier(text, candidate_labels)
print(result)
{'sequence': 'The national football team won the cup yesterday.',
 'labels': ['sports', 'technology', 'health'], 
 'scores': [0.9948731064796448, 0.0029330444522202015, 0.002193822991102934]}
Pythonで学ぶ自然言語処理(NLP)

質問自然言語推論(QNLI)

  • 質問の答えが本文にあるかを判定
  • 用途:
    • 文書検索
    • チャットボット
    • 情報検索

QNLIは本文と質問を受け取り、スコアを返す図。

Pythonで学ぶ自然言語処理(NLP)

QNLIパイプライン

from transformers import pipeline

qnli_pipeline = pipeline( task="text-classification", model="cross-encoder/qnli-electra-base" )
passage = "Penguins are found primarily in the Southern Hemisphere."
question = "Where do penguins live?"
result = qnli_pipeline({"text": question, "text_pair": passage})
print(result)
{'label': 'LABEL_0', 'score': 0.9951545000076294}
Pythonで学ぶ自然言語処理(NLP)

QNLIパイプライン

from transformers import pipeline
qnli_pipeline = pipeline(
    task="text-classification", 
    model="cross-encoder/qnli-electra-base"
    )
passage = "Penguins are found primarily in the Southern Hemisphere."
question = "What is the capital of Paris?"
result = qnli_pipeline({"text": question, "text_pair": passage})
print(result)
{'label': 'LABEL_0', 'score': 0.008907231502234936}
Pythonで学ぶ自然言語処理(NLP)

Passons à la pratique !

Pythonで学ぶ自然言語処理(NLP)

Preparing Video For Download...