模型概述
模型特點
模型能力
使用案例
🚀 roberta-large-zeroshot-v2.0-c
本項目的 roberta-large-zeroshot-v2.0-c
模型系列專為高效零樣本分類設計,可藉助 Hugging Face 管道運行,無需訓練數據,且支持 GPU 和 CPU。該系列模型能將任何分類任務轉化為自然語言推理任務,通用性強。
🚀 快速開始
本系列模型專為通過 Hugging Face 管道進行高效零樣本分類而設計。這些模型無需訓練數據即可進行分類,並且可以在 GPU 和 CPU 上運行。最新零樣本分類器的概述可在我的 零樣本分類器集合 中查看。
這個 zeroshot-v2.0
系列模型的主要更新是,有幾個模型是在完全符合商業使用的數據上進行訓練的,適用於有嚴格許可要求的用戶。
這些模型可以完成一個通用的分類任務:根據給定的文本判斷一個假設是 “真” 還是 “不真”(entailment
與 not_entailment
)。這個任務格式基於自然語言推理任務(NLI)。這個任務非常通用,任何分類任務都可以通過 Hugging Face 管道重新表述為這個任務。
✨ 主要特性
- 零樣本分類:無需訓練數據即可完成分類任務。
- 跨平臺運行:支持在 GPU 和 CPU 上運行。
- 通用性強:可將任何分類任務轉化為自然語言推理任務。
- 商業友好:部分模型使用完全符合商業使用的數據進行訓練。
📦 安裝指南
你可以使用以下命令安裝所需的庫:
!pip install transformers[sentencepiece]
💻 使用示例
基礎用法
#!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)
高級用法
你可以通過更改零樣本管道的 hypothesis_template
來制定自己的假設。類似於大語言模型的 “提示工程”,你可以測試不同的 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)
📚 詳細文檔
訓練數據
名稱中帶有 “-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
的數據集。
評估指標
這些模型在 28 個不同的文本分類任務上使用 f1_macro 指標進行了評估。主要參考模型是 facebook/bart-large-mnli
,在撰寫本文時(2024 年 4 月 3 日),它是最常用的符合商業使用的零樣本分類器。
屬性 | 詳情 |
---|---|
模型類型 | 用於零樣本分類的 RoBERTa 大模型 |
訓練數據 | 1. 使用 Mixtral-8x7B-Instruct-v0.1 生成的合成數據。 2. 兩個符合商業使用的 NLI 數據集:(MNLI,FEVER-NLI)。 |
不同模型的使用場景
- 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 模型。 - 有關新模型的最新更新,請始終參考 零樣本分類器集合。
模型復現
復現代碼可在以下目錄中找到:https://github.com/MoritzLaurer/zeroshot-classifier/tree/main
侷限性和偏差
該模型只能完成文本分類任務。偏差可能來自底層基礎模型、人工 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 聯繫我,或在 領英 上與我交流。
靈活使用和 “提示”
你可以通過更改零樣本管道的 hypothesis_template
來制定自己的假設。類似於大語言模型的 “提示工程”,你可以測試不同的 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)








