🚀 多語言MiniLMv2-L12-mnli-xnli
本多語言模型可對100多種語言進行自然語言推理(NLI),因此也適用於多語言零樣本分類。它能有效解決不同語言文本分類數據稀缺的問題,為多語言處理提供高效的解決方案。
🚀 快速開始
本多語言模型可對100多種語言進行自然語言推理(NLI),因此也適用於多語言零樣本分類。
✨ 主要特性
- 多語言支持:能夠在100 + 種語言上執行自然語言推理(NLI),適用於多語言零樣本分類。
- 模型蒸餾優勢:相較於XLM - RoBERTa - large,蒸餾後的模型體積更小,推理速度更快,內存需求更低。
- 訓練數據優質:使用XNLI開發集的專業翻譯文本和原始英文MNLI訓練集進行訓練,避免了機器翻譯質量問題,降低過擬合風險,減少訓練成本。
📦 安裝指南
文檔未提及安裝步驟,故跳過此章節。
💻 使用示例
基礎用法
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="MoritzLaurer/multilingual-MiniLMv2-L12-mnli-xnli")
sequence_to_classify = "Angela Merkel ist eine Politikerin in Deutschland und Vorsitzende der CDU"
candidate_labels = ["politics", "economy", "entertainment", "environment"]
output = classifier(sequence_to_classify, candidate_labels, multi_label=False)
print(output)
高級用法
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model_name = "MoritzLaurer/multilingual-MiniLMv2-L12-mnli-xnli"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
premise = "Angela Merkel ist eine Politikerin in Deutschland und Vorsitzende der CDU"
hypothesis = "Emmanuel Macron is the President of France"
input = tokenizer(premise, hypothesis, truncation=True, return_tensors="pt")
output = model(input["input_ids"].to(device))
prediction = torch.softmax(output["logits"][0], -1).tolist()
label_names = ["entailment", "neutral", "contradiction"]
prediction = {name: round(float(pred) * 100, 1) for pred, name in zip(prediction, label_names)}
print(prediction)
📚 詳細文檔
訓練數據
該模型在XNLI開發數據集和MNLI訓練數據集上進行訓練。XNLI開發集包含2490條從英語專業翻譯為其他14種語言的文本(總共37350條文本)(詳見此論文)。需要注意的是,XNLI包含15種語言的MNLI數據集的15個機器翻譯版本的訓練集,但由於這些機器翻譯存在質量問題,該模型僅在XNLI開發集的專業翻譯文本和原始英文MNLI訓練集(392702條文本)上進行訓練。不使用機器翻譯文本可以避免模型對15種語言過擬合,避免對預訓練的其他語言產生災難性遺忘,並顯著降低訓練成本。
訓練過程
該模型使用Hugging Face訓練器進行訓練,採用以下超參數。確切的基礎模型是mMiniLMv2 - L12 - H384 - distilled - from - XLMR - Large。
training_args = TrainingArguments(
num_train_epochs=3, # total number of training epochs
learning_rate=4e-05,
per_device_train_batch_size=64, # batch size per device during training
per_device_eval_batch_size=120, # batch size for evaluation
warmup_ratio=0.06, # number of warmup steps for learning rate scheduler
weight_decay=0.01, # strength of weight decay
)
評估結果
該模型在XNLI測試集的15種語言上進行評估(每種語言5010條文本,總共75150條)。需要注意的是,多語言NLI模型能夠在不接收特定語言的NLI訓練數據的情況下對NLI文本進行分類(跨語言遷移)。這意味著該模型也能夠對其訓練過的其他語言進行NLI,但性能可能低於XNLI中可用的語言。
論文中報告的multilingual - MiniLM - L12的平均XNLI性能為0.711(見表11)。此重新實現的平均性能為0.75。性能的提升可能得益於在訓練數據中加入了MNLI,並且該模型是從XLM - RoBERTa - large而不是 - base(multilingual - MiniLM - L12 - v2)蒸餾而來。
數據集 |
平均XNLI |
阿拉伯語(ar) |
保加利亞語(bg) |
德語(de) |
希臘語(el) |
英語(en) |
西班牙語(es) |
法語(fr) |
印地語(hi) |
俄語(ru) |
斯瓦希里語(sw) |
泰語(th) |
土耳其語(tr) |
烏爾都語(ur) |
越南語(vi) |
中文(zh) |
準確率 |
0.75 |
0.73 |
0.78 |
0.762 |
0.754 |
0.821 |
0.779 |
0.775 |
0.724 |
0.76 |
0.689 |
0.738 |
0.732 |
0.7 |
0.762 |
0.751 |
速度(文本/秒,A100 GPU,評估批次 = 120) |
4535.0 |
4629.0 |
4417.0 |
4500.0 |
3938.0 |
4959.0 |
4634.0 |
4152.0 |
4190.0 |
4368.0 |
4630.0 |
4698.0 |
4929.0 |
4291.0 |
4420.0 |
5275.0 |
數據集 |
mnli_m |
mnli_mm |
準確率 |
0.818 |
0.831 |
速度(文本/秒,A100 GPU,評估批次 = 120) |
2912.0 |
2902.0 |
🔧 技術細節
本模型基於微軟創建的multilingual - MiniLM - L12模型,該模型是從XLM - RoBERTa - large蒸餾而來。通過在XNLI開發集和MNLI訓練集上進行微調,使其具備多語言自然語言推理和零樣本分類的能力。蒸餾模型雖然在性能上較原模型有所損失,但在速度和內存使用上有顯著優勢。
📄 許可證
本項目採用MIT許可證。
⚠️ 重要提示
請查閱關於不同NLI數據集的原始論文和文獻,以瞭解潛在的偏差。
💡 使用建議
📖 引用
如果您使用此模型,請引用:Laurer, Moritz, Wouter van Atteveldt, Andreu Salleras Casas, and Kasper Welbers. 2022. ‘Less Annotating, More Classifying – Addressing the Data Scarcity Issue of Supervised Machine Learning with Deep Transfer Learning and BERT - NLI’. Preprint, June. Open Science Framework. https://osf.io/74b8k.
💬 合作建議或疑問
如果您有任何問題或合作想法,請通過m{dot}laurer{at}vu{dot}nl聯繫我,或在領英上與我交流。