Zero-shot 分類與 QNLI

Python 的 Natural Language Processing(NLP)

Fouad Trad

Machine Learning Engineer

Zero-shot 分類

  • 讓模型將文字歸類到從未見過的標籤
  • 用自然語言預測產生輸出
  • 適用於:
    • 內容標記
    • 客服支援
    • 新聞篩選

圖片顯示文字「The national football team won the cup yesterday」需在 sports、technology、health 三類中分類。

Python 的 Natural Language Processing(NLP)

Zero-shot 分類 pipeline

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 的 Natural Language Processing(NLP)

問題自然語言推論(QNLI)

  • 判斷段落中是否能找到問題的答案
  • 適用於:
    • 文件搜尋
    • 聊天機器人
    • 資訊擷取

圖片顯示 QNLI 接收一段文字與一個問題並回傳分數。

Python 的 Natural Language Processing(NLP)

QNLI pipeline

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 的 Natural Language Processing(NLP)

QNLI pipeline

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 的 Natural Language Processing(NLP)

一起來練習吧!

Python 的 Natural Language Processing(NLP)

Preparing Video For Download...