🚀 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)。