Zero-shot-klassificering och QNLI

Natural Language Processing (NLP) i Python

Fouad Trad

Machine Learning Engineer

Zero-shot-klassificering

  • Modellen kan tilldela text till etiketter den inte sett tidigare
  • Använder naturlig språkprediktion för utdata
  • Användbart för:
    • Innehållsmärkning
    • Kundsupport
    • Filtrering av nyhetsartiklar

Bild som visar texten "The national football team won the cup yesterday" som ska klassificeras i tre kategorier: sport, teknik och hälsa.

Natural Language Processing (NLP) i Python

Zero-shot-klassificeringspipeline

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) i Python

Inferens för frågor med naturligt språk (QNLI)

  • Avgör om svaret på en fråga finns i ett textavsnitt
  • Användbart för:
    • Dokumentsökning
    • Chattbotar
    • Informationshämtning

Bild som visar att QNLI tar emot ett textavsnitt och en fråga och returnerar ett poängvärde.

Natural Language Processing (NLP) i Python

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

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

Nu kör vi en övning!

Natural Language Processing (NLP) i Python

Preparing Video For Download...