Zero-shot classification
Zero-shot classification is the ability for a transformer to predict a label from a new set of classes which it wasn't originally trained to identify. This is possible through its transfer learning capabilities. It can be an extremely valuable tool.
Hugging Face pipeline()
also has a zero-shot-classification
task. These pipelines require both an input text and candidate labels.
Build a zero-shot classifier to predict the label for the input text
, a news headline that has been loaded for you.
pipelines
from the transformers
library is already loaded for you.
Note that we are using our own version of the pipeline function to enable you to learn how to use these functions without having to download the model.
This is a part of the course
“Working with Hugging Face”
Exercise instructions
- Build the pipeline for a zero-shot-classification task and save as
classifier
. - Create a list of the labels - "politics", "science", "sports" - and save as
candidate_labels
. - Predict the label of
text
using the classifier and candidate labels.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Build the zero-shot classifier
____ = pipeline(____="zero-shot-classification", ____="facebook/bart-large-mnli")
# Create the list
candidate_labels = ["politics", "____", "____"]
# Predict the output
output = ____(____, ____)
print(f"Top Label: {output['labels'][0]} with score: {output['scores'][0]}")