Classification zero-shot et QNLI

Natural Language Processing (NLP) en Python

Fouad Trad

Machine Learning Engineer

Classification zero-shot

  • Permet d'assigner un texte à des étiquettes jamais vues
  • Utilise la prédiction en langage naturel pour produire la sortie
  • Utile pour :
    • Balisage de contenu
    • Soutien à la clientèle
    • Filtrage d'articles de nouvelles

Image montrant un texte « The national football team won the cup yesterday » à classer selon trois catégories : sports, technology et health.

Natural Language Processing (NLP) en Python

Pipeline de classification zero-shot

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]}
Natural Language Processing (NLP) en Python

Question natural language inference (QNLI)

  • Détermine si la réponse à une question se trouve dans un passage
  • Utile pour :
    • Recherche de documents
    • Agents conversationnels
    • Repérage d'information

Image montrant que QNLI reçoit un passage et une question et retourne un score.

Natural Language Processing (NLP) en Python

Pipeline 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}
Natural Language Processing (NLP) en Python

Pipeline 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}
Natural Language Processing (NLP) en Python

Passons à la pratique !

Natural Language Processing (NLP) en Python

Preparing Video For Download...