Few-Shot Text Classification

Machine Learning
Type
NLP text-classification task (low-resource)

What does it do?

Few-shot text classification assigns documents to classes using only a handful of labelled examples per class.

Gao et al. (2021) study prompt-based fine-tuning of pretrained language models as few-shot learners, and Tunstall et al. (2022) introduce SetFit, a prompt-free approach that fine-tunes a pretrained Sentence Transformer on text pairs and trains a classification head on the resulting embeddings. Other few-shot strategies, such as prototypical networks and MAML, are also used for this task; they are summarised briefly here, and the detailed treatment on this page concerns the SetFit approach.

The intuition behind the method

Most classifiers need a large labelled training set, but labelling text is expensive and sometimes only a few examples per class exist. Few-shot text classification works around this by starting from a model that already understands language, a large pretrained model, and nudging it toward your classes with just a few examples each. Instead of learning every class from scratch, it compares new documents to a small set of labelled prototypes or lightly adapts the pretrained model, so a few examples per class can guide the decision.

History

Few-shot text classification grew out of few-shot and meta-learning research applied to language. Gao et al. (2021) studied how pretrained language models can be made better few-shot learners using prompt-based fine-tuning. Tunstall et al. (2022) introduced SetFit, an efficient prompt-free framework for few-shot fine-tuning of Sentence Transformers, which made the approach practical when only small labelled sets are available.

When to use it

Use few-shot text classification when you need to classify text but have only a few labelled examples per class, far fewer than a conventional supervised classifier would require, and when a suitable pretrained language model is available. The method assumes that every class you want to predict has at least one labelled example; classes without any examples cannot be predicted. Do not use it when you already have a large, balanced labelled set, since a standard supervised text classifier is then simpler and is the more natural choice.

Limitations and considerations

  • Requires at least one labelled example for every class; entirely unseen classes cannot be predicted.
  • Performance depends on the pretrained backbone: a weak or poorly matched model cannot be compensated for by the few labels, because those labels carry little general language knowledge.
  • With very few examples, predictions can be unstable and sensitive to which examples happen to be chosen for each class.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Can learn from very few labelled examples per class, in a regime where a conventional classifier has too few labels to train on reliably.
  • Draws on the language knowledge already inside a pretrained model, so the amount of task-specific labelled data needed is small.
  • The SetFit approach needs no hand-written prompts or verbalizers; the cited work reports results comparable to prompt-based PEFT and PET techniques while being faster to train.

Applications

  • Classifying text when labelling is costly and only a few examples per category are available.
  • Bootstrapping a classifier for a new domain or label set before large-scale labelling is done.
  • Rapid prototyping of text categorisation in research and operational settings with scarce labels.
  • Extending an existing scheme with new, sparsely-labelled classes.

How it works

How it works

1

Assemble a small labelled set with at least one example per class, often only a few per class. This support set is all the method learns from, so the fewer examples available, the more the result depends on what the pretrained backbone already knows.

2

Choose a pretrained language model as the backbone. With so few labels there is little room to teach general language use, so the quality of the pretrained model sets much of what can be achieved. Pick a backbone suited to the language of your texts; the cited SetFit work notes that its Sentence Transformer body can be switched for multilingual settings.

3

Adapt the backbone with a few-shot strategy. Prototypical networks compute a prototype for each class from its examples, MAML meta-learns initial parameters intended to adapt quickly, and fine-tuning updates the pretrained model on the small set. In the SetFit approach cited here, the pretrained Sentence Transformer is first fine-tuned on text pairs with a contrastive Siamese objective, without prompts or verbalizers.

4

Embed each new document with the adapted model and assign a label: by nearest prototype for prototypical networks, or with the classification head trained on the embeddings in the SetFit approach. The output is one predicted class label per document.

Common pitfalls

  • Treating a class with zero labelled examples as learnable — every class must have at least one example.
  • Pairing the method with a small or poorly matched pretrained model, which removes the prior knowledge the few-shot setting depends on.
  • Over-reading accuracy from a tiny support set. Estimates are noisy, so evaluate on a labelled test set kept separate from the support examples, repeat the experiment over several random draws of the support set and report the mean and spread, and check per-class results so that minority classes are not hidden by overall accuracy.

Frequently asked questions

How few examples per class does it really need?

At least one labelled example per class is required, and the method is designed for only a few per class. How well it performs at very low counts depends heavily on the pretrained backbone and on which examples are chosen, so the fewer examples you have, the more the result rests on the model's prior knowledge.

Which few-shot strategy should I use?

Prototypical networks build a prototype per class from the few examples and classify by nearest prototype; MAML meta-learns parameters intended to adapt quickly to new tasks; fine-tuning updates a pretrained model on the small set. The SetFit approach from Tunstall et al. (2022) is one prompt-free option that fine-tunes a Sentence Transformer and trains a classification head on its embeddings.

Why does the pretrained model matter so much?

The backbone strongly shapes few-shot results, because with only a few labelled examples much of the knowledge has to come from the pretrained model. Choose a backbone suited to the language of your texts. The cited SetFit work reports high accuracy with orders of magnitude fewer parameters than PEFT and PET techniques, which typically require billion-parameter language models to achieve high accuracy.

When should I prefer a normal supervised classifier instead?

When you have abundant labelled data. Few-shot methods are designed for the low-label regime; with a large, balanced labelled set, a standard supervised text classifier is simpler and usually the stronger choice.

Related methods

Sources

  1. Gao, T., Fisch, A. & Chen, D. (2021). Making Pre-trained Language Models Better Few-shot Learners. ACL.

    Open DOI reference
  2. Tunstall, L., Reimers, N., Jo, U.E.S., Bates, L., Korat, D., Wasserblat, M. & Pereg, O. (2022). Efficient Few-Shot Learning Without Prompts. arXiv.

    Open DOI reference