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 }
}