模型简介
模型特点
模型能力
使用案例
🚀 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)








