{"id":13754258,"url":"https://github.com/huggingface/setfit","last_synced_at":"2025-10-08T19:00:08.305Z","repository":{"id":59830117,"uuid":"508972059","full_name":"huggingface/setfit","owner":"huggingface","description":"Efficient few-shot learning with Sentence Transformers","archived":false,"fork":false,"pushed_at":"2025-08-05T11:46:08.000Z","size":1920264,"stargazers_count":2561,"open_issues_count":147,"forks_count":250,"subscribers_count":20,"default_branch":"main","last_synced_at":"2025-09-06T10:13:16.529Z","etag":null,"topics":["few-shot-learning","nlp","sentence-transformers"],"latest_commit_sha":null,"homepage":"https://hf.co/docs/setfit","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/huggingface.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-06-30T07:10:15.000Z","updated_at":"2025-09-05T06:47:59.000Z","dependencies_parsed_at":"2022-09-22T18:11:42.030Z","dependency_job_id":"770577d3-96cf-4c6f-a7ad-26712cd9fb59","html_url":"https://github.com/huggingface/setfit","commit_stats":null,"previous_names":["setfit/setfit"],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/huggingface/setfit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huggingface%2Fsetfit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huggingface%2Fsetfit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huggingface%2Fsetfit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huggingface%2Fsetfit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/huggingface","download_url":"https://codeload.github.com/huggingface/setfit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huggingface%2Fsetfit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000516,"owners_count":26082785,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["few-shot-learning","nlp","sentence-transformers"],"created_at":"2024-08-03T09:01:52.371Z","updated_at":"2025-10-08T19:00:05.690Z","avatar_url":"https://github.com/huggingface.png","language":"Jupyter Notebook","readme":"\u003cimg src=\"https://raw.githubusercontent.com/huggingface/setfit/main/assets/setfit.png\"\u003e\n\n\u003cp align=\"center\"\u003e\n    🤗 \u003ca href=\"https://huggingface.co/models?library=setfit\" target=\"_blank\"\u003eModels\u003c/a\u003e | 📊 \u003ca href=\"https://huggingface.co/setfit\" target=\"_blank\"\u003eDatasets\u003c/a\u003e | 📕 \u003ca href=\"https://huggingface.co/docs/setfit\" target=\"_blank\"\u003eDocumentation\u003c/a\u003e | 📖 \u003ca href=\"https://huggingface.co/blog/setfit\" target=\"_blank\"\u003eBlog\u003c/a\u003e | 📃 \u003ca href=\"https://arxiv.org/abs/2209.11055\" target=\"_blank\"\u003ePaper\u003c/a\u003e\n\u003c/p\u003e\n\n# SetFit - Efficient Few-shot Learning with Sentence Transformers\n\nSetFit is an efficient and prompt-free framework for few-shot fine-tuning of [Sentence Transformers](https://sbert.net/). It achieves high accuracy with little labeled data - for instance, with only 8 labeled examples per class on the Customer Reviews sentiment dataset, SetFit is competitive with fine-tuning RoBERTa Large on the full training set of 3k examples 🤯!\n\nCompared to other few-shot learning methods, SetFit has several unique features:\n\n* 🗣 **No prompts or verbalizers:** Current techniques for few-shot fine-tuning require handcrafted prompts or verbalizers to convert examples into a format suitable for the underlying language model. SetFit dispenses with prompts altogether by generating rich embeddings directly from text examples.\n* 🏎 **Fast to train:** SetFit doesn't require large-scale models like T0 or GPT-3 to achieve high accuracy. As a result, it is typically an order of magnitude (or more) faster to train and run inference with.\n* 🌎 **Multilingual support**: SetFit can be used with any [Sentence Transformer](https://huggingface.co/models?library=sentence-transformers\u0026sort=downloads) on the Hub, which means you can classify text in multiple languages by simply fine-tuning a multilingual checkpoint.\n\nCheck out the [SetFit Documentation](https://huggingface.co/docs/setfit) for more information!\n\n## Installation\n\nDownload and install `setfit` by running:\n\n```bash\npip install setfit\n```\n\nIf you want the bleeding-edge version instead, install from source by running:\n\n```bash\npip install git+https://github.com/huggingface/setfit.git\n```\n\n## Usage\n\nThe [quickstart](https://huggingface.co/docs/setfit/quickstart) is a good place to learn about training, saving, loading, and performing inference with SetFit models. \n\nFor more examples, check out the [`notebooks`](https://github.com/huggingface/setfit/tree/main/notebooks) directory, the [tutorials](https://huggingface.co/docs/setfit/tutorials/overview), or the [how-to guides](https://huggingface.co/docs/setfit/how_to/overview).\n\n\n### Training a SetFit model\n\n`setfit` is integrated with the [Hugging Face Hub](https://huggingface.co/) and provides two main classes:\n\n* [`SetFitModel`](https://huggingface.co/docs/setfit/reference/main#setfit.SetFitModel): a wrapper that combines a pretrained body from `sentence_transformers` and a classification head from either [`scikit-learn`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html) or [`SetFitHead`](https://huggingface.co/docs/setfit/reference/main#setfit.SetFitHead) (a differentiable head built upon `PyTorch` with similar APIs to `sentence_transformers`).\n* [`Trainer`](https://huggingface.co/docs/setfit/reference/trainer#setfit.Trainer): a helper class that wraps the fine-tuning process of SetFit.\n\nHere is a simple end-to-end training example using the default classification head from `scikit-learn`:\n\n\n```python\nfrom datasets import load_dataset\nfrom setfit import SetFitModel, Trainer, TrainingArguments, sample_dataset\n\n\n# Load a dataset from the Hugging Face Hub\ndataset = load_dataset(\"sst2\")\n\n# Simulate the few-shot regime by sampling 8 examples per class\ntrain_dataset = sample_dataset(dataset[\"train\"], label_column=\"label\", num_samples=8)\neval_dataset = dataset[\"validation\"].select(range(100))\ntest_dataset = dataset[\"validation\"].select(range(100, len(dataset[\"validation\"])))\n\n# Load a SetFit model from Hub\nmodel = SetFitModel.from_pretrained(\n    \"sentence-transformers/paraphrase-mpnet-base-v2\",\n    labels=[\"negative\", \"positive\"],\n)\n\nargs = TrainingArguments(\n    batch_size=16,\n    num_epochs=4,\n    eval_strategy=\"epoch\",\n    save_strategy=\"epoch\",\n    load_best_model_at_end=True,\n)\n\ntrainer = Trainer(\n    model=model,\n    args=args,\n    train_dataset=train_dataset,\n    eval_dataset=eval_dataset,\n    metric=\"accuracy\",\n    column_mapping={\"sentence\": \"text\", \"label\": \"label\"}  # Map dataset columns to text/label expected by trainer\n)\n\n# Train and evaluate\ntrainer.train()\nmetrics = trainer.evaluate(test_dataset)\nprint(metrics)\n# {'accuracy': 0.8691709844559585}\n\n# Push model to the Hub\ntrainer.push_to_hub(\"tomaarsen/setfit-paraphrase-mpnet-base-v2-sst2\")\n\n# Download from Hub\nmodel = SetFitModel.from_pretrained(\"tomaarsen/setfit-paraphrase-mpnet-base-v2-sst2\")\n# Run inference\npreds = model.predict([\"i loved the spiderman movie!\", \"pineapple on pizza is the worst 🤮\"])\nprint(preds)\n# [\"positive\", \"negative\"]\n```\n\n\n## Reproducing the results from the paper\n\nWe provide scripts to reproduce the results for SetFit and various baselines presented in Table 2 of our paper. Check out the setup and training instructions in the [`scripts/`](scripts/) directory.\n\n## Developer installation\n\nTo run the code in this project, first create a Python virtual environment using e.g. Conda:\n\n```bash\nconda create -n setfit python=3.9 \u0026\u0026 conda activate setfit\n```\n\nThen install the base requirements with:\n\n```bash\npip install -e '.[dev]'\n```\n\nThis will install mandatory packages for SetFit like `datasets` as well as development packages like `black` and `isort` that we use to ensure consistent code formatting.\n\n### Formatting your code\n\nWe use `black` and `isort` to ensure consistent code formatting. After following the installation steps, you can check your code locally by running:\n\n```\nmake style \u0026\u0026 make quality\n```\n\n## Project structure\n\n```\n├── LICENSE\n├── Makefile        \u003c- Makefile with commands like `make style` or `make tests`\n├── README.md       \u003c- The top-level README for developers using this project.\n├── docs            \u003c- Documentation source\n├── notebooks       \u003c- Jupyter notebooks.\n├── final_results   \u003c- Model predictions from the paper\n├── scripts         \u003c- Scripts for training and inference\n├── setup.cfg       \u003c- Configuration file to define package metadata\n├── setup.py        \u003c- Make this project pip installable with `pip install -e`\n├── src             \u003c- Source code for SetFit\n└── tests           \u003c- Unit tests\n```\n\n## Related work\n\n* [https://github.com/pmbaumgartner/setfit](https://github.com/pmbaumgartner/setfit) - A scikit-learn API version of SetFit.\n* [jxpress/setfit-pytorch-lightning](https://github.com/jxpress/setfit-pytorch-lightning) - A PyTorch Lightning implementation of SetFit.\n* [davidberenstein1957/spacy-setfit](https://github.com/davidberenstein1957/spacy-setfit) - An easy and intuitive approach to use SetFit in combination with spaCy. \n\n## Citation\n\n```bibtex\n@misc{https://doi.org/10.48550/arxiv.2209.11055,\n  doi = {10.48550/ARXIV.2209.11055},\n  url = {https://arxiv.org/abs/2209.11055},\n  author = {Tunstall, Lewis and Reimers, Nils and Jo, Unso Eun Seo and Bates, Luke and Korat, Daniel and Wasserblat, Moshe and Pereg, Oren},\n  keywords = {Computation and Language (cs.CL), FOS: Computer and information sciences, FOS: Computer and information sciences},\n  title = {Efficient Few-Shot Learning Without Prompts},\n  publisher = {arXiv},\n  year = {2022},\n  copyright = {Creative Commons Attribution 4.0 International}\n}\n```\n","funding_links":[],"categories":["文本匹配 文本检索 文本相似度","Computation and Communication Optimisation","Jupyter Notebook"],"sub_categories":["其他_文本生成、文本对话"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuggingface%2Fsetfit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhuggingface%2Fsetfit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuggingface%2Fsetfit/lists"}