模型概述
模型特點
模型能力
使用案例
🚀 roberta-base-zeroshot-v2.0-c
roberta-base-zeroshot-v2.0-c是一個專為零樣本分類設計的模型,可在無訓練數據的情況下進行文本分類,適用於GPU和CPU,且部分版本採用完全商業友好的數據進行訓練。
🚀 快速開始
本模型屬於zeroshot-v2.0系列,專為使用Hugging Face管道進行高效零樣本分類而設計。這些模型無需訓練數據即可進行分類,並且可以在GPU和CPU上運行。最新零樣本分類器的概述可在我的 零樣本分類器集合 中查看。
✨ 主要特性
- 零樣本分類能力:無需訓練數據即可完成分類任務。
- 多平臺支持:可在GPU和CPU上運行。
- 商業友好數據訓練:部分模型使用完全商業友好的數據進行訓練。
- 通用分類任務:可以將任何分類任務重新表述為判斷假設是否為“真”或“非真”的任務。
📦 安裝指南
在使用模型前,你需要安裝transformers
庫:
#!pip install transformers[sentencepiece]
💻 使用示例
基礎用法
from transformers import pipeline
text = "Angela Merkel is a politician in Germany and leader of the CDU"
hypothesis_template = "This text is about {}"
classes_verbalized = ["politics", "economy", "entertainment", "environment"]
zeroshot_classifier = pipeline("zero-shot-classification", model="MoritzLaurer/deberta-v3-large-zeroshot-v2.0") # change the model identifier here
output = zeroshot_classifier(text, classes_verbalized, hypothesis_template=hypothesis_template, multi_label=False)
print(output)
multi_label=False
強制模型只選擇一個類別,multi_label=True
允許模型選擇多個類別。
高級用法
你可以通過更改零樣本管道的 hypothesis_template
來制定自己的假設,類似於大語言模型的“提示工程”,你可以測試不同的假設模板和類別表述來提高性能。
from transformers import pipeline
text = "Angela Merkel is a politician in Germany and leader of the CDU"
# 表述1
hypothesis_template = "This text is about {}"
classes_verbalized = ["politics", "economy", "entertainment", "environment"]
# 表述2,根據你的用例而定
hypothesis_template = "The topic of this text is {}"
classes_verbalized = ["political activities", "economic policy", "entertainment or music", "environmental protection"]
# 測試不同的表述
zeroshot_classifier = pipeline("zero-shot-classification", model="MoritzLaurer/deberta-v3-large-zeroshot-v2.0") # 更改此處的模型標識符
output = zeroshot_classifier(text, classes_verbalized, hypothesis_template=hypothesis_template, multi_label=False)
print(output)
📚 詳細文檔
模型描述
zeroshot-v2.0系列模型
該系列模型的主要更新是,部分模型使用完全商業友好的數據進行訓練,以滿足有嚴格許可要求的用戶。這些模型可以完成一個通用的分類任務:給定一段文本,判斷一個假設是“真”還是“非真”(entailment
與 not_entailment
)。此任務格式基於自然語言推理任務(NLI),任何分類任務都可以通過Hugging Face管道重新表述為該任務。
訓練數據
名稱中帶有 “-c
” 的模型使用兩種完全商業友好的數據進行訓練:
- 合成數據:使用 Mixtral-8x7B-Instruct-v0.1 生成。首先與Mistral-large對話,為25種職業創建了500多個不同的文本分類任務列表,並手動整理數據。然後使用這些數據作為種子,用Mixtral-8x7B-Instruct-v0.1生成了數十萬個文本。最終使用的數據集可在 synthetic_zeroshot_mixtral_v0.1 數據集中的
mixtral_written_text_for_tasks_v4
子集中找到。數據整理經過多次迭代,未來還會進一步改進。 - 兩個商業友好的NLI數據集:MNLI 和 FEVER-NLI,添加這些數據集是為了提高模型的泛化能力。
- 名稱中沒有 “
-c
” 的模型還使用了更廣泛的訓練數據,包括ANLI、WANLI、LingNLI以及 此列表 中used_in_v1.1==True
的所有數據集。
何時使用哪個模型
- deberta-v3-零樣本與roberta-零樣本:deberta-v3的性能明顯優於roberta,但速度稍慢。roberta與Hugging Face的生產推理TEI容器和閃存注意力直接兼容,適合生產用例。簡而言之,為了追求準確性,使用deberta-v3模型;如果關注生產推理速度,可以考慮使用roberta模型(例如在TEI容器和 HF推理端點 中)。
- 商業用例:名稱中帶有 “
-c
” 的模型保證僅使用商業友好的數據進行訓練。沒有 “-c
” 的模型使用了更多數據,性能更好,但包含了非商業許可的數據。對於有嚴格法律要求的用戶,建議使用名稱中帶有 “-c
” 的模型。 - 多語言/非英語用例:使用 bge-m3-zeroshot-v2.0 或 bge-m3-zeroshot-v2.0-c。注意,多語言模型的性能不如僅適用於英語的模型。因此,你也可以先使用 EasyNMT 等庫將文本機器翻譯為英語,然後將任何僅適用於英語的模型應用於翻譯後的數據。機器翻譯還便於在團隊不精通數據中所有語言的情況下進行驗證。
- 上下文窗口:
bge-m3
模型可以處理多達8192個標記,其他模型可以處理多達512個標記。注意,較長的文本輸入會使模型變慢並降低性能,因此如果你只處理最多400個單詞/1頁的文本,可以使用deberta模型以獲得更好的性能。 - 新模型的最新更新始終可以在 零樣本分類器集合 中查看。
復現
復現代碼可在 此處 的 v2_synthetic_data
目錄中找到。
侷限性和偏差
該模型只能完成文本分類任務。偏差可能來自基礎模型、人類NLI訓練數據和Mixtral生成的合成數據。
許可證
基礎模型根據MIT許可證發佈,訓練數據的許可證因模型而異,請參閱上文。
引用
該模型是 這篇論文 中描述的研究的擴展。如果你在學術上使用該模型,請引用:
@misc{laurer_building_2023,
title = {Building {Efficient} {Universal} {Classifiers} with {Natural} {Language} {Inference}},
url = {http://arxiv.org/abs/2312.17543},
doi = {10.48550/arXiv.2312.17543},
abstract = {Generative Large Language Models (LLMs) have become the mainstream choice for fewshot and zeroshot learning thanks to the universality of text generation. Many users, however, do not need the broad capabilities of generative LLMs when they only want to automate a classification task. Smaller BERT-like models can also learn universal tasks, which allow them to do any text classification task without requiring fine-tuning (zeroshot classification) or to learn new tasks with only a few examples (fewshot), while being significantly more efficient than generative LLMs. This paper (1) explains how Natural Language Inference (NLI) can be used as a universal classification task that follows similar principles as instruction fine-tuning of generative LLMs, (2) provides a step-by-step guide with reusable Jupyter notebooks for building a universal classifier, and (3) shares the resulting universal classifier that is trained on 33 datasets with 389 diverse classes. Parts of the code we share has been used to train our older zeroshot classifiers that have been downloaded more than 55 million times via the Hugging Face Hub as of December 2023. Our new classifier improves zeroshot performance by 9.4\%.},
urldate = {2024-01-05},
publisher = {arXiv},
author = {Laurer, Moritz and van Atteveldt, Wouter and Casas, Andreu and Welbers, Kasper},
month = dec,
year = {2023},
note = {arXiv:2312.17543 [cs]},
keywords = {Computer Science - Artificial Intelligence, Computer Science - Computation and Language},
}
合作建議或問題諮詢
如果你有問題或合作想法,請通過moritz{at}huggingface{dot}co聯繫我,或在 LinkedIn 上與我交流。
🔧 技術細節
評估指標
模型在28個不同的文本分類任務上使用 f1_macro 指標進行評估。主要參考點是 facebook/bart-large-mnli
,在撰寫本文時(2024年4月3日),它是最常用的商業友好零樣本分類器。
facebook/bart-large-mnli | roberta-base-zeroshot-v2.0-c | roberta-large-zeroshot-v2.0-c | deberta-v3-base-zeroshot-v2.0-c | deberta-v3-base-zeroshot-v2.0 (fewshot) | deberta-v3-large-zeroshot-v2.0-c | deberta-v3-large-zeroshot-v2.0 (fewshot) | bge-m3-zeroshot-v2.0-c | bge-m3-zeroshot-v2.0 (fewshot) | |
---|---|---|---|---|---|---|---|---|---|
all datasets mean | 0.497 | 0.587 | 0.622 | 0.619 | 0.643 (0.834) | 0.676 | 0.673 (0.846) | 0.59 | (0.803) |
amazonpolarity (2) | 0.937 | 0.924 | 0.951 | 0.937 | 0.943 (0.961) | 0.952 | 0.956 (0.968) | 0.942 | (0.951) |
imdb (2) | 0.892 | 0.871 | 0.904 | 0.893 | 0.899 (0.936) | 0.923 | 0.918 (0.958) | 0.873 | (0.917) |
appreviews (2) | 0.934 | 0.913 | 0.937 | 0.938 | 0.945 (0.948) | 0.943 | 0.949 (0.962) | 0.932 | (0.954) |
yelpreviews (2) | 0.948 | 0.953 | 0.977 | 0.979 | 0.975 (0.989) | 0.988 | 0.985 (0.994) | 0.973 | (0.978) |
rottentomatoes (2) | 0.83 | 0.802 | 0.841 | 0.84 | 0.86 (0.902) | 0.869 | 0.868 (0.908) | 0.813 | (0.866) |
emotiondair (6) | 0.455 | 0.482 | 0.486 | 0.459 | 0.495 (0.748) | 0.499 | 0.484 (0.688) | 0.453 | (0.697) |
emocontext (4) | 0.497 | 0.555 | 0.63 | 0.59 | 0.592 (0.799) | 0.699 | 0.676 (0.81) | 0.61 | (0.798) |
empathetic (32) | 0.371 | 0.374 | 0.404 | 0.378 | 0.405 (0.53) | 0.447 | 0.478 (0.555) | 0.387 | (0.455) |
financialphrasebank (3) | 0.465 | 0.562 | 0.455 | 0.714 | 0.669 (0.906) | 0.691 | 0.582 (0.913) | 0.504 | (0.895) |
banking77 (72) | 0.312 | 0.124 | 0.29 | 0.421 | 0.446 (0.751) | 0.513 | 0.567 (0.766) | 0.387 | (0.715) |
massive (59) | 0.43 | 0.428 | 0.543 | 0.512 | 0.52 (0.755) | 0.526 | 0.518 (0.789) | 0.414 | (0.692) |
wikitoxic_toxicaggreg (2) | 0.547 | 0.751 | 0.766 | 0.751 | 0.769 (0.904) | 0.741 | 0.787 (0.911) | 0.736 | (0.9) |
wikitoxic_obscene (2) | 0.713 | 0.817 | 0.854 | 0.853 | 0.869 (0.922) | 0.883 | 0.893 (0.933) | 0.783 | (0.914) |
wikitoxic_threat (2) | 0.295 | 0.71 | 0.817 | 0.813 | 0.87 (0.946) | 0.827 | 0.879 (0.952) | 0.68 | (0.947) |
wikitoxic_insult (2) | 0.372 | 0.724 | 0.798 | 0.759 | 0.811 (0.912) | 0.77 | 0.779 (0.924) | 0.783 | (0.915) |
wikitoxic_identityhate (2) | 0.473 | 0.774 | 0.798 | 0.774 | 0.765 (0.938) | 0.797 | 0.806 (0.948) | 0.761 | (0.931) |
hateoffensive (3) | 0.161 | 0.352 | 0.29 | 0.315 | 0.371 (0.862) | 0.47 | 0.461 (0.847) | 0.291 | (0.823) |
hatexplain (3) | 0.239 | 0.396 | 0.314 | 0.376 | 0.369 (0.765) | 0.378 | 0.389 (0.764) | 0.29 | (0.729) |
biasframes_offensive (2) | 0.336 | 0.571 | 0.583 | 0.544 | 0.601 (0.867) | 0.644 | 0.656 (0.883) | 0.541 | (0.855) |
biasframes_sex (2) | 0.263 | 0.617 | 0.835 | 0.741 | 0.809 (0.922) | 0.846 | 0.815 (0.946) | 0.748 | (0.905) |
biasframes_intent (2) | 0.616 | 0.531 | 0.635 | 0.554 | 0.61 (0.881) | 0.696 | 0.687 (0.891) | 0.467 | (0.868) |
agnews (4) | 0.703 | 0.758 | 0.745 | 0.68 | 0.742 (0.898) | 0.819 | 0.771 (0.898) | 0.687 | (0.892) |
yahootopics (10) | 0.299 | 0.543 | 0.62 | 0.578 | 0.564 (0.722) | 0.621 | 0.613 (0.738) | 0.587 | (0.711) |
trueteacher (2) | 0.491 | 0.469 | 0.402 | 0.431 | 0.479 (0.82) | 0.459 | 0.538 (0.846) | 0.471 | (0.518) |
spam (2) | 0.505 | 0.528 | 0.504 | 0.507 | 0.464 (0.973) | 0.74 | 0.597 (0.983) | 0.441 | (0.978) |
wellformedquery (2) | 0.407 | 0.333 | 0.333 | 0.335 | 0.491 (0.769) | 0.334 | 0.429 (0.815) | 0.361 | (0.718) |
manifesto (56) | 0.084 | 0.102 | 0.182 | 0.17 | 0.187 (0.376) | 0.258 | 0.256 (0.408) | 0.147 | (0.331) |
capsotu (21) | 0.34 | 0.479 | 0.523 | 0.502 | 0.477 (0.664) | 0.603 | 0.502 (0.686) | 0.472 | (0.644) |
這些數字表示零樣本性能,因為訓練數據中未添加這些數據集的數據。請注意,標題中沒有 “-c
” 的模型進行了兩次評估:一次不使用這28個數據集中的任何數據,以測試純零樣本性能(相應列中的第一個數字);最後一次包括每個數據集每個類別最多500個訓練數據點(列中括號內的第二個數字,“fewshot”)。沒有模型在測試數據上進行訓練。
不同數據集的詳細信息可在此處查看:https://github.com/MoritzLaurer/zeroshot-classifier/blob/main/v1_human_data/datasets_overview.csv
📄 許可證
基礎模型根據MIT許可證發佈。訓練數據的許可證因模型而異,請參閱上文。
信息表格
屬性 | 詳情 |
---|---|
模型類型 | 用於零樣本分類的roberta-base模型 |
訓練數據 | 名稱中帶有 “-c ” 的模型使用合成數據(使用Mixtral-8x7B-Instruct-v0.1生成)和兩個商業友好的NLI數據集(MNLI和FEVER-NLI)進行訓練;名稱中沒有 “-c ” 的模型還使用了更廣泛的訓練數據,包括ANLI、WANLI、LingNLI等。 |
常用提示信息
⚠️ 重要提示
沒有模型在測試數據上進行訓練。
💡 使用建議
為了追求準確性,使用deberta-v3模型;如果關注生產推理速度,可以考慮使用roberta模型(例如在TEI容器和 HF推理端點 中)。對於有嚴格法律要求的用戶,建議使用名稱中帶有 “
-c
” 的模型。如果處理多語言/非英語文本,可以先使用 EasyNMT 等庫將文本機器翻譯為英語,然後將任何僅適用於英語的模型應用於翻譯後的數據。








