Skip to content

About

This project implements and extends SetFit for few-shot text classification, along with advanced improvements such as hard negative mining and open-set recognition.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ SetFit + Hard Negative Mining for Few-Shot Text Classification

Python PyTorch HuggingFace Dataset

πŸ“š Table of Contents


πŸ“Œ Overview

This project implements and extends SetFit (Sentence Transformer Fine-tuning) for few-shot text classification, along with advanced improvements such as hard negative mining and open-set recognition.

We reproduce the methodology proposed in L. Tunstall et al., "Efficient Few-Shot Learning Without Prompts", 2022 and build upon it with practical enhancements.


🧠 Problem Statement

Traditional NLP models require large labeled datasets, which are:

  • Expensive
  • Time-consuming
  • Domain-specific

πŸ‘‰ Our goal:

Build a high-performance text classifier using only a few labeled examples (e.g., 8 per class).


πŸ“š About SetFit (Paper Summary)

The SetFit paper proposes a prompt-free few-shot learning framework that avoids the limitations of prompt-based methods.

πŸ” Key Ideas

  • Uses Sentence Transformers instead of large LLMs
  • Performs contrastive learning using sentence pairs
  • Trains a lightweight classification head on embeddings
  • Requires no prompts or verbalizers

πŸ‘‰ The method:

  1. Fine-tune sentence embeddings using positive/negative pairs
  2. Train classifier on embeddings

⚑ Why it matters

  • Comparable performance to large models
  • Orders of magnitude faster training
  • Works with very small datasets (few-shot)
  • No prompt engineering needed

πŸ—οΈ Project Architecture

setfit-project/
β”‚
β”œβ”€β”€ data/                  # Saved dataset samples (.txt)
β”œβ”€β”€ results/               # Experiment outputs
β”‚   β”œβ”€β”€ baseline/
β”‚   └── hard_negative/
β”‚
β”œβ”€β”€ configs/
β”‚   └── default.yaml       # Centralized project settings
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ data_loader.py     # Dataset + few-shot sampling
β”‚   β”œβ”€β”€ pair_builder.py    # Pair construction
β”‚   β”œβ”€β”€ hard_negative.py   # Hard negative mining
β”‚   β”œβ”€β”€ train.py           # Training pipeline
β”‚   β”œβ”€β”€ evaluate.py        # Metrics + plots
β”‚   β”œβ”€β”€ demo.py            # Interactive inference
β”‚   └── config_utils.py    # YAML config loader
β”‚
β”œβ”€β”€ requirements.txt       # Project dependencies
β”œβ”€β”€ LICENSE                # Apache license
└── README.md              # This file

βš™οΈ Methodology

🧩 Baseline (SetFit)

  • Sample k examples per class
  • Create positive + random negative pairs
  • Train Sentence Transformer with CosineSimilarityLoss
  • Train Logistic Regression classifier

πŸ”₯ Our Contributions

We extend SetFit with:


1️⃣ Hard Negative Mining

Instead of random negatives:

  • Select semantically similar but incorrect samples
  • Forces model to learn fine-grained boundaries

πŸ‘‰ Improves robustness and generalization


2️⃣ Improved Pair Construction

  • Balanced positive/negative sampling
  • Hard + easy negatives mix

3️⃣ Open-Set Classification

We introduce "OTHER" class using confidence thresholding

If model confidence < threshold:

β†’ Reject prediction (unknown class)

4️⃣ Real Dataset (BBC News)

We use:

  • Business
  • Entertainment
  • Politics
  • Sports
  • Technology

πŸ‘‰ More realistic than AG News


πŸ“Š Pipeline

Dataset β†’ Few-shot Sampling β†’ Pair Generation
        β†’ Sentence Transformer Fine-tuning
        β†’ Embedding Extraction
        β†’ Classifier Training
        β†’ Evaluation + Demo

πŸ§ͺ Experiments

We evaluate:

  • Baseline SetFit
  • Hard Negative SetFit

Across:

  • Multiple seeds
  • Few-shot setting (k=8)
  • Using multiple classification metrics beyond accuracy for a more comprehensive evaluation.

πŸ“ˆ Metrics

We evaluate using standard classification metrics:

  • Accuracy
  • Precision (macro)
  • Recall (macro)
  • F1-score (macro)
  • Mean Β± Standard Deviation across seeds

πŸš€ How to Run

πŸ”Ή 0. Configure experiment settings

All defaults are now stored in:

configs/default.yaml

You can edit dataset/model/threshold/seeds and other values there. All scripts support --config and still allow CLI overrides for key options.


πŸ”Ή 1. Install dependencies

pip install -r requirements.txt

πŸ”Ή 2. Generate dataset

python src/data_loader.py --config configs/default.yaml

Outputs:

data/train.txt
data/test.txt

πŸ”Ή 3. Train baseline

python src/train.py --config configs/default.yaml --mode baseline

πŸ”Ή 4. Train hard negative model

python src/train.py --config configs/default.yaml --mode hard_negative

πŸ”Ή 5. Evaluate

python src/evaluate.py --config configs/default.yaml --results_dir results/baseline --output_dir results/baseline
python src/evaluate.py --config configs/default.yaml --results_dir results/hard_negative --output_dir results/hard_negative

πŸ”Ή 6. Run demo

python src/demo.py --config configs/default.yaml --model_dir results/hard_negative/seed_0 --interactive

πŸ”¬ Research Experiments (Hypotheses + Ablations)

Use the dedicated research config and matrix runner:

python src/run_research_experiments.py --config configs/research.yaml --dry_run
python src/run_research_experiments.py --config configs/research.yaml

Run a pilot subset before full execution:

python src/run_research_experiments.py --config configs/research.yaml --datasets bbc_news,ag_news --k_values 8 --pair_strategies random,hard --seeds 0,1,2

Pair strategy options:

  • random
  • easy
  • hard
  • mixed

Threshold analysis for uncertainty-aware behavior:

python src/threshold_analysis.py --config configs/research.yaml --model_dir results/research/bbc_news/k_8/hard/seed_0

πŸ’‘ Example Output

Input: "Stock markets crash globally"
Prediction: BUSINESS (0.82)

Input: "Aliens discovered on Mars"
Prediction: OTHER

🧠 Key Insights

  • Few-shot learning is highly unstable across seeds

  • Hard negatives improve:

    • decision boundaries
    • semantic understanding
  • Confidence thresholding enables real-world deployment


πŸ“Š Results

πŸ”Ή Baseline (SetFit)

Seed Accuracy
0 0.716
1 0.699
2 0.710

Mean Accuracy: ~0.708


πŸ”Ή Hard Negative (Ours)

Seed Accuracy
0 0.955
1 0.935
42 0.939

Mean Accuracy: ~0.943


πŸš€ Improvement

  • Absolute gain: +23.5%
  • Hard negative mining significantly improves:
    • semantic discrimination
    • decision boundary sharpness

πŸ‘‰ This demonstrates that pair quality > model complexity in few-shot learning.


πŸ“Œ Future Work

  • Adaptive hard negative mining
  • Label semantic injection
  • Contrastive loss variants
  • Calibration & uncertainty estimation

πŸŽ“ Authors

  • Mozeel Pradip Vanwani
  • Mayank Seth
  • Krishnkant Sahu
  • Burri Vivek Vardhan Verma

About

This project implements and extends SetFit for few-shot text classification, along with advanced improvements such as hard negative mining and open-set recognition.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages