SetFit: Efficient Few-Shot Learning Without Prompts
SetFit is a prompt-free few-shot text classification framework that first fine-tunes a Sentence Transformer contrastively on in-class/out-of-class sentence pairs built from a handful of labeled examples, then trains a classifier head on the resulting embeddings. With 8 examples per class it matches RoBERTa Large fine-tuned on 3k examples on Customer Reviews, beats GPT-3 and PET on the RAFT benchmark with a 355M-parameter model, and rivals T-Few 3B while being 27x smaller. Training takes about 30 seconds on a V100 ($0.025) versus 11 minutes on an A100 for T-Few, and the setfit library trains a model in a few lines of Python; swapping in a multilingual checkpoint extends it to other languages.
Related posts
- SetFitABSA: Few-Shot Aspect Based Sentiment Analysis using SetFit
Hugging Face mediumAdeptSetFitABSA performs few-shot aspect-based sentiment analysis in three stages: spaCy extracts noun and noun-compound aspect candidates, one SetFit model classifies each candidate as aspect or non-aspect, and a second SetFit model assigns a sentiment polarity to each aspect. Both classifiers turn token-level labeling into sentence classification by prefixing the candidate to the full sentence ("aspect_candidate:sentence"), so no prompts are needed. On SemEval-2014 Laptop and Restaurant data, the two 110M-parameter MPNet models beat fine-tuned T5 and GPT2 when training data is scarce and match or beat 7B Llama-2-chat with in-context learning. A full model trains in about 10 minutes on a free Colab T4 through the setfit library's AbsaTrainer and AbsaModel APIs.
- Few-shot learning in practice: GPT-Neo and the 🤗 Accelerated Inference API
Hugging Face mediumAdeptFew-shot learning lets a large language model perform a new NLP task from a handful of examples supplied in the prompt (task description, examples, and a prompt to complete), avoiding the labeled data needed for fine-tuning. GPT-Neo 2.7B, about 60x smaller than GPT-3, generalizes poorly zero-shot but works well with 3-4 examples. Generation parameters such as end sequence, max new tokens and temperature strongly affect whether a task succeeds, and a Python snippet shows how to call the model through the Hugging Face Accelerated Inference API. Few-shot predictions also inherit pretraining biases, as when a sentiment prompt labels "I'm a disabled happy person" negative. The recommended safeguards are transparency, opt-out, user feedback and override, and human review of automated decisions.
- Training and Finetuning Embedding Models with Sentence Transformers
Hugging Face mediumAdeptSentence Transformers v3 trains and finetunes embedding models through a SentenceTransformerTrainer that combines Hugging Face datasets, a loss function, optional training arguments and evaluators. Finetuning matters because each task needs its own notion of similarity. Dataset column order must match the chosen loss's inputs, with an optional "label" or "score" column, and extraneous columns must be removed. A worked example finetunes MPNet on AllNLI triplets with MultipleNegativesRankingLoss and a no-duplicates batch sampler, and evaluates with TripletEvaluator and EmbeddingSimilarityEvaluator on STSb.
- How Hugging Face Accelerated Development of Witty Works Writing Assistant
Hugging Face easyNoviceWitty Works' inclusive-language writing assistant began with spaCy-based linguistic feature matching, which handled about 85% of its ~2,300-word vocabulary but failed on context-dependent words like "fossil" or "flexible." On Hugging Face experts' advice, the team switched from contextualized word embeddings to Sentence Transformers sentence embeddings and used SetFit few-shot fine-tuning, feeding mpnet-base-v2 embeddings into logistic regression and KNN classifiers. Labeled data needs dropped from 100-200 to 15-20 sentences per word, and the model reached 0.92 accuracy. It was deployed on Azure without further optimization because latency was already acceptable.
- Train and Fine-Tune Sentence Transformers Models
Hugging Face mediumAdeptSentence Transformers models map text to fixed-size embeddings by running a pre-trained Transformer and pooling its token embeddings, and training them depends on matching the dataset format to the right loss. Four common formats map to specific losses: labeled pairs (ContrastiveLoss, SoftmaxLoss, CosineSimilarityLoss), unlabeled positive pairs (MultipleNegativesRankingLoss), class-labeled sentences (batch triplet losses), and unlabeled triplets (TripletLoss). A worked example builds a distilroberta-base model, trains it on QQP triplets with TripletLoss via the legacy `fit` API, and pushes it to the Hugging Face Hub. The guide is marked outdated: since v3.0, SentenceTransformerTrainer replaces this API.