Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/setfit_doc/en/zero_shot.ipynb
Views: 2929
Zero-shot Text Classification
Your class names are likely already good descriptors of the text that you're looking to classify. With 🤗 SetFit, you can use these class names with strong pretrained Sentence Transformer models to get a strong baseline model without any training samples.
This guide will show you how to perform zero-shot text classification.
Testing dataset
We'll use the dair-ai/emotion dataset to test the performance of our zero-shot model.
This dataset stores the class names within the dataset Features
, so we'll extract the classes like so:
Otherwise, we could manually set the list of classes.
Synthetic dataset
Then, we can use get_templated_dataset() to synthetically generate a dummy dataset given these class names.
Training
We can use this dataset to train a SetFit model just like normal:
Once trained, we can evaluate the model:
And run predictions:
These predictions all look right!
Baseline
To show that the zero-shot performance of SetFit works well, we'll compare it against a zero-shot classification model from transformers
.
With its 59.1% accuracy, the 0-shot SetFit heavily outperforms the recommended zero-shot model by transformers
.
Prediction latency
Beyond getting higher accuracies, SetFit is much faster too. Let's compute the latency of SetFit with BAAI/bge-small-en-v1.5
versus the latency of transformers
with facebook/bart-large-mnli
. Both tests were performed on a GPU.
So, SetFit with BAAI/bge-small-en-v1.5
is 67x faster than transformers
with facebook/bart-large-mnli
, alongside being more accurate: