{"id":8634,"library":"setfit","title":"SetFit","description":"SetFit is a Python library for efficient few-shot learning using Sentence Transformers. It enables training accurate text classifiers with minimal labeled data by finetuning pre-trained Sentence Transformer models. The library is prompt-free, fast to train, and offers multilingual support. The current version is 1.1.3, and the project maintains an active release cadence with frequent patch updates addressing compatibility and minor fixes, alongside larger feature releases.","status":"active","version":"1.1.3","language":"en","source_language":"en","source_url":"https://github.com/huggingface/setfit","tags":["machine learning","nlp","few-shot learning","sentence embeddings","huggingface","text classification"],"install":[{"cmd":"pip install setfit","lang":"bash","label":"Basic Installation"},{"cmd":"pip install torch --index-url https://download.pytorch.org/whl/cu118 && pip install setfit","lang":"bash","label":"Installation with CUDA support (recommended for GPU)"}],"dependencies":[{"reason":"SetFit is tested on Python 3.9+.","package":"python","optional":false},{"reason":"Core dependency for data handling, frequent compatibility updates required.","package":"datasets","optional":false},{"reason":"Core backend for embedding models and finetuning. SetFit defers embedding model finetuning to its Trainer.","package":"sentence-transformers","optional":false},{"reason":"Used for `TrainingArguments` and `TrainerCallback` instances; compatibility updates are frequent.","package":"transformers","optional":false},{"reason":"Required for multi-GPU training and is a dependency of `sentence-transformers`.","package":"accelerate","optional":false},{"reason":"Default classification head used by SetFit.","package":"scikit-learn","optional":true},{"reason":"Recommended for GPU acceleration.","package":"torch","optional":true}],"imports":[{"symbol":"SetFitModel","correct":"from setfit import SetFitModel"},{"symbol":"SetFitTrainer","correct":"from setfit import SetFitTrainer"},{"symbol":"TrainingArguments","correct":"from setfit import TrainingArguments"},{"note":"Path changed in v1.0.0","wrong":"from setfit.modeling import SupConLoss","symbol":"SupConLoss","correct":"from setfit import SupConLoss"}],"quickstart":{"code":"from datasets import load_dataset\nfrom setfit import SetFitModel, SetFitTrainer, TrainingArguments, sample_dataset\nfrom sentence_transformers.losses import CosineSimilarityLoss\n\n# 1. Initialize a SetFit model\nmodel = SetFitModel.from_pretrained(\"BAAI/bge-small-en-v1.5\")\n\n# 2. Load and prepare a dataset (e.g., sst2 for sentiment classification)\ndataset = load_dataset(\"SetFit/sst2\")\n\n# Simulate few-shot regime: 8 examples per class\ntrain_dataset = sample_dataset(dataset[\"train\"], label_column=\"label\", num_samples=8)\neval_dataset = dataset[\"validation\"]\n\n# Optional: Map dataset columns if they are not 'text' and 'label'\ncolumn_mapping = {\"sentence\": \"text\", \"label\": \"label\"}\n\n# 3. Define TrainingArguments\ntraining_args = TrainingArguments(\n    batch_size=16,\n    num_iterations=20, # Number of text pairs to generate for contrastive learning\n    num_epochs=1,      # Number of epochs to use for contrastive learning\n    learning_rate=2e-5,\n    seed=42,\n    evaluation_strategy=\"epoch\",\n    save_strategy=\"epoch\"\n)\n\n# 4. Create SetFitTrainer\ntrainer = SetFitTrainer(\n    model=model,\n    args=training_args,\n    train_dataset=train_dataset,\n    eval_dataset=eval_dataset,\n    loss_class=CosineSimilarityLoss, # Loss function for contrastive learning\n    metric=\"accuracy\",\n    column_mapping=column_mapping\n)\n\n# 5. Train the model\ntrainer.train()\n\n# 6. Evaluate the model\nmetrics = trainer.evaluate()\nprint(f\"Evaluation Metrics: {metrics}\")\n\n# 7. Make predictions\nsentences = [\"The movie was great!\", \"I didn't like the food.\"]\npredictions = model.predict(sentences)\nprint(f\"Predictions: {predictions}\")\n\n# 8. Push model to Hugging Face Hub (requires `huggingface_hub` login)\n# trainer.push_to_hub(\"my-awesome-setfit-model\")","lang":"python","description":"This quickstart demonstrates the typical workflow for training a SetFit model for text classification. It covers initializing a `SetFitModel` from the Hugging Face Hub, preparing a dataset (including simulating a few-shot scenario), configuring training parameters via `TrainingArguments`, creating and training a `SetFitTrainer`, evaluating the model, and making predictions. The example uses a small BGE model and the SST-2 dataset, sampling 8 examples per class for training."},"warnings":[{"fix":"Migrate `model.fit()` calls to use `SetFitTrainer` and pass training hyperparameters via a `TrainingArguments` instance. Refer to the v1.0.0 migration guide for details.","message":"SetFit v1.0.0 introduced significant API changes, particularly for the `SetFitTrainer` and how training arguments are handled. The old `model.fit()` method was removed, and training parameters were moved into a `TrainingArguments` dataclass.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Upgrade to Python 3.9 or newer. SetFit is tested on Python 3.9+.","message":"Python 3.7 support was deprecated starting from SetFit v1.1.0.","severity":"deprecated","affected_versions":">=1.1.0"},{"fix":"Ensure you are using a compatible set of dependency versions. If encountering errors, try upgrading SetFit to the latest patch release or consult the official documentation/GitHub issues for known compatibility notes.","message":"Frequent compatibility issues can arise with newer versions of core dependencies like `datasets`, `transformers`, and `sentence-transformers`. Patch releases of SetFit (`v1.1.3`, `v1.1.2`) often address these specific issues.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `transformers.trainer_utils.set_seed()` is called before `SetFitModel.from_pretrained()`. Be aware that minor differences in sampling logic between major versions might still lead to slightly different models.","message":"Reproducibility of trained models across different SetFit versions (e.g., v0.6.0 vs v1.0.3) can be challenging due to changes in random seed handling for the model's head initialization and different sentence pair sampling methods.","severity":"gotcha","affected_versions":"<1.0.3"},{"fix":"This issue was fixed in v1.1.1. Upgrade to SetFit v1.1.1 or later to ensure `report_to=\"none\"` is respected.","message":"The `report_to=\"none\"` argument in `TrainingArguments` was sometimes ignored, leading to unexpected logging behavior if Weights & Biases or Tensorboard were installed.","severity":"gotcha","affected_versions":"1.0.0 - 1.1.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"The training API was refactored. Use `SetFitTrainer` with a `TrainingArguments` instance and call `trainer.train()` instead.","cause":"Attempting to use the `model.fit()` method, which was removed in SetFit v1.0.0.","error":"AttributeError: 'SetFitModel' object has no attribute 'fit'"},{"fix":"Create an instance of `TrainingArguments` and pass these parameters to it, then provide the `TrainingArguments` instance to the `SetFitTrainer` via the `args` parameter.","cause":"Training hyperparameters (e.g., `num_epochs`, `batch_size`, `learning_rate`) were passed directly to `SetFitTrainer`'s constructor, but in v1.0.0 and later, they must be encapsulated within a `TrainingArguments` object.","error":"TypeError: SetFitTrainer.__init__() got an unexpected keyword argument 'num_epochs' (or 'batch_size', 'learning_rate', etc.)"},{"fix":"Try clearing the PyTorch Sentence Transformers cache (`~/.cache/torch/sentence_transformers/`) and ensure your system has enough free disk space.","cause":"This error during `trainer.train()` often indicates a corrupted PyTorch cache or insufficient disk space.","error":"RuntimeError: [enforce fail at inline_container.cc:471] PytorchStreamWriter failed writing file data/..."},{"fix":"Inspect class distribution of your dataset and balance if necessary. Reduce `num_epochs` or `num_iterations`, adjust `learning_rate`, or use techniques like early stopping. Evaluate with metrics appropriate for imbalanced data (e.g., F1-score, precision, recall).","cause":"This can indicate overfitting to the majority class, severely imbalanced datasets, or 'collapsing' of the embedding model where it fails to distinguish between classes.","error":"Model outputs the exact same label regardless of input or very low accuracy (e.g., 60-63%) on a classification task."}]}