零样本分类与 QNLI

Python 中的自然语言处理(NLP)

Fouad Trad

Machine Learning Engineer

零样本分类

  • 让模型为从未见过的标签分配文本
  • 通过自然语言推断获得输出
  • 适用场景:
    • 内容打标签
    • 客服支持
    • 新闻筛选

一段文本"国家足球队昨日夺冠",需在三个类别中分类:体育、科技、健康。

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...