Skip to content
Afa' Afa'

Zero-Shot classification with Transformers

Classify text into arbitrary labels with a zero-shot classification pipeline.

1 min read Updated

Zero-shot classification lets you label text with categories the model was never explicitly trained on. This script loads a multilingual NLI model and classifies text against arbitrary labels. The script is below.

zero-shot-classification.py

python
from transformers import pipeline

classifier = pipeline("zero-shot-classification", 
                      model="BaptisteDoyen/camembert-base-xnli")

sequence = "L'équipe de France joue aujourd'hui au Parc des Princes"
candidate_labels = ["sport","politique","science"]
hypothesis_template = "Ce texte parle de {}."    

result = classifier(sequence, candidate_labels, hypothesis_template=hypothesis_template)     

print(result)
Something wrong or want to discuss this article? Get in touch

Search articles and projects

Type to filter articles and projects. Use the arrow keys to move through results and Enter to open one. Press Escape to close.