🚀 xlm-roberta-large-xnli
本模型可用于零样本的文本分类任务,通过在多语言自然语言推理(NLI)数据上微调,能在多种语言中实现高效准确的文本分类。
🚀 快速开始
本模型基于 [xlm - roberta - large](https://huggingface.co/xlm - roberta - large),在包含15种语言的NLI数据组合上进行微调。它适用于零样本的文本分类任务,例如可与Hugging Face的 ZeroShotClassificationPipeline 一起使用。
✨ 主要特性
- 多语言支持:该模型经过在XNLI(多语言NLI数据集)上的微调,可用于XNLI语料库中的任意语言,包括英语、法语、西班牙语、德语等15种语言。由于基础模型在100种不同语言上进行了预训练,因此在上述列出语言之外的其他语言中也表现出了一定的有效性。
- 零样本分类:特别适用于非英语语言的零样本文本分类任务。
💻 使用示例
基础用法
使用Hugging Face的 zero - shot - classification
管道加载模型:
from transformers import pipeline
classifier = pipeline("zero-shot-classification",
model="joeddav/xlm-roberta-large-xnli")
然后可以对任意上述支持的语言进行分类,甚至可以使用一种语言的标签对另一种语言的文本进行分类:
sequence_to_classify = "За кого вы голосуете в 2020 году?"
candidate_labels = ["Europe", "public health", "politics"]
classifier(sequence_to_classify, candidate_labels)
默认的假设模板是英语的 This text is {}
。如果只在一种语言环境下工作,将其翻译成相应语言可能会更有效:
sequence_to_classify = "¿A quién vas a votar en 2020?"
candidate_labels = ["Europa", "salud pública", "política"]
hypothesis_template = "Este ejemplo es {}."
classifier(sequence_to_classify, candidate_labels, hypothesis_template=hypothesis_template)
高级用法
手动使用PyTorch进行操作:
from transformers import AutoModelForSequenceClassification, AutoTokenizer
nli_model = AutoModelForSequenceClassification.from_pretrained('joeddav/xlm-roberta-large-xnli')
tokenizer = AutoTokenizer.from_pretrained('joeddav/xlm-roberta-large-xnli')
premise = sequence
hypothesis = f'This example is {label}.'
x = tokenizer.encode(premise, hypothesis, return_tensors='pt',
truncation_strategy='only_first')
logits = nli_model(x.to(device))[0]
entail_contradiction_logits = logits[:,[0,2]]
probs = entail_contradiction_logits.softmax(dim=1)
prob_label_is_true = probs[:,1]
🔧 技术细节
该模型首先在100种语言的数据集上进行预训练,具体可参考 原始论文。然后在MNLI训练集以及XNLI验证集和测试集的组合上针对NLI任务进行微调。最后,在XNLI数据上额外训练一个epoch,其中前提和假设的翻译被打乱,使得每个示例的前提和假设来自同一个原始英语示例,但前提和假设使用不同的语言。
📄 许可证
本模型使用的许可证为MIT。
属性 |
详情 |
模型类型 |
基于xlm - roberta - large微调的零样本分类模型 |
训练数据 |
100种语言的预训练数据,MNLI训练集、XNLI验证集和测试集,打乱前提和假设语言的XNLI数据 |
💡 使用建议
对于仅英语的分类任务,建议使用 [bart - large - mnli](https://huggingface.co/facebook/bart - large - mnli) 或 [蒸馏的bart MNLI模型](https://huggingface.co/models?filter=pipeline_tag%3Azero - shot - classification&search=valhalla)。