setfit
v1.1.0 - Sentence Transformers as the finetuning backend; tackle deprecations of other dependencies
?模型|数据集|文档|博客| ?纸
SetFit是一个有效且及时的框架,用于对句子变形金刚进行几次微调。它几乎没有标记的数据就可以实现高精度 - 例如,在客户评论情感数据集中,每个班级只有8个标记的示例,SetFit在完整的3K示例中的完整培训集中竞争了Roberta的竞争力!
与其他几次学习方法相比,SetFit具有几个独特的功能:
查看SetFit文档以获取更多信息!
通过运行下载并安装setfit :
pip install setfit如果要使用Bleeding-Edge版本,请通过运行来安装源:
pip install git+https://github.com/huggingface/setfit.git快速启动是学习训练,节省,加载和使用SetFit模型进行推断的好地方。
有关更多示例,请查看notebooks目录,教程或操作指南。
setfit与拥抱面枢纽集成在一起,并提供两个主要类:
SetFitModel :结合了sentence_transformers的预验证身体的包装器和来自scikit-learn或SetFitHead的分类头(一个与sentence_transformers相似的PyTorch构建的可区分头(一个可区别的头)。Trainer :一个辅助课程,涵盖了SetFit的微调过程。这是一个简单的端到端培训示例,使用scikit-learn的默认分类头:
from datasets import load_dataset
from setfit import SetFitModel , Trainer , TrainingArguments , sample_dataset
# Load a dataset from the Hugging Face Hub
dataset = load_dataset ( "sst2" )
# Simulate the few-shot regime by sampling 8 examples per class
train_dataset = sample_dataset ( dataset [ "train" ], label_column = "label" , num_samples = 8 )
eval_dataset = dataset [ "validation" ]. select ( range ( 100 ))
test_dataset = dataset [ "validation" ]. select ( range ( 100 , len ( dataset [ "validation" ])))
# Load a SetFit model from Hub
model = SetFitModel . from_pretrained (
"sentence-transformers/paraphrase-mpnet-base-v2" ,
labels = [ "negative" , "positive" ],
)
args = TrainingArguments (
batch_size = 16 ,
num_epochs = 4 ,
eval_strategy = "epoch" ,
save_strategy = "epoch" ,
load_best_model_at_end = True ,
)
trainer = Trainer (
model = model ,
args = args ,
train_dataset = train_dataset ,
eval_dataset = eval_dataset ,
metric = "accuracy" ,
column_mapping = { "sentence" : "text" , "label" : "label" } # Map dataset columns to text/label expected by trainer
)
# Train and evaluate
trainer . train ()
metrics = trainer . evaluate ( test_dataset )
print ( metrics )
# {'accuracy': 0.8691709844559585}
# Push model to the Hub
trainer . push_to_hub ( "tomaarsen/setfit-paraphrase-mpnet-base-v2-sst2" )
# Download from Hub
model = SetFitModel . from_pretrained ( "tomaarsen/setfit-paraphrase-mpnet-base-v2-sst2" )
# Run inference
preds = model . predict ([ "i loved the spiderman movie!" , "pineapple on pizza is the worst ?" ])
print ( preds )
# ["positive", "negative"] 我们提供脚本来重现我们论文表2中列出的SetFit和各种基线的结果。在scripts/目录中查看设置和培训说明。
要在此项目中运行代码,请首先使用EG Conda创建Python虚拟环境:
conda create -n setfit python=3.9 && conda activate setfit然后安装基本要求:
pip install -e ' .[dev] '这将安装诸如数据集(例如datasets的强制包装以及我们使用的开发软件包,例如black和isort ,以确保一致的代码格式。
我们使用black和isort来确保一致的代码格式。遵循安装步骤后,您可以通过运行:
make style && make quality
├── LICENSE
├── Makefile <- Makefile with commands like `make style` or `make tests`
├── README.md <- The top-level README for developers using this project.
├── docs <- Documentation source
├── notebooks <- Jupyter notebooks.
├── final_results <- Model predictions from the paper
├── scripts <- Scripts for training and inference
├── setup.cfg <- Configuration file to define package metadata
├── setup.py <- Make this project pip installable with `pip install -e`
├── src <- Source code for SetFit
└── tests <- Unit tests
@misc { https://doi.org/10.48550/arxiv.2209.11055 ,
doi = { 10.48550/ARXIV.2209.11055 } ,
url = { https://arxiv.org/abs/2209.11055 } ,
author = { Tunstall, Lewis and Reimers, Nils and Jo, Unso Eun Seo and Bates, Luke and Korat, Daniel and Wasserblat, Moshe and Pereg, Oren } ,
keywords = { Computation and Language (cs.CL), FOS: Computer and information sciences, FOS: Computer and information sciences } ,
title = { Efficient Few-Shot Learning Without Prompts } ,
publisher = { arXiv } ,
year = { 2022 } ,
copyright = { Creative Commons Attribution 4.0 International }
}