🚀 多語言 mDeBERTa-v3-base-mnli-xnli
這個多語言模型可以對100種語言進行自然語言推理(NLI),因此也適用於多語言零樣本分類。它能幫助用戶在多種語言場景下高效地完成文本分類等任務,節省大量標註數據的成本。
📚 詳細文檔
模型描述
這個多語言模型可以對100種語言進行自然語言推理(NLI),因此也適用於多語言零樣本分類。基礎模型由微軟在CC100多語言數據集上進行預訓練。然後在XNLI數據集(包含來自15種語言的假設 - 前提對)以及英文MNLI數據集上進行微調。
截至2021年12月,mDeBERTa-base是表現最佳的多語言基礎大小的Transformer模型,由微軟在這篇論文中提出。
如果你正在尋找一個更小、更快(但性能稍低)的模型,可以嘗試multilingual-MiniLMv2-L6-mnli-xnli。
💻 使用示例
基礎用法
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="MoritzLaurer/mDeBERTa-v3-base-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/mDeBERTa-v3-base-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種語言過擬合;避免對mDeBERTa預訓練的其他85種語言產生災難性遺忘;並顯著降低訓練成本。
訓練過程
mDeBERTa-v3-base-mnli-xnli使用Hugging Face訓練器進行訓練,使用了以下超參數:
training_args = TrainingArguments(
num_train_epochs=2, # total number of training epochs
learning_rate=2e-05,
per_device_train_batch_size=16, # batch size per device during training
per_device_eval_batch_size=16, # batch size for evaluation
warmup_ratio=0.1, # number of warmup steps for learning rate scheduler
weight_decay=0.06, # strength of weight decay
)
評估結果
該模型在XNLI測試集的15種語言上進行評估(每種語言5010篇文本,總共75150篇)。需要注意的是,多語言NLI模型能夠在不接收特定語言的NLI訓練數據的情況下對NLI文本進行分類(跨語言遷移)。這意味著該模型也能夠對mDeBERTa訓練的其他85種語言進行NLI,但性能很可能低於XNLI中可用的語言。
此外,如果模型中心的其他多語言模型聲稱在非英語語言上的性能約為90%,作者很可能在測試過程中犯了錯誤,因為最新的論文中沒有一篇顯示在XNLI上的多語言平均性能比80%高出幾個百分點(見這裡或這裡)。
語言 |
準確率 |
平均 |
0.808 |
阿拉伯語 (ar) |
0.802 |
保加利亞語 (bg) |
0.829 |
德語 (de) |
0.825 |
希臘語 (el) |
0.826 |
英語 (en) |
0.883 |
西班牙語 (es) |
0.845 |
法語 (fr) |
0.834 |
印地語 (hi) |
0.771 |
俄語 (ru) |
0.813 |
斯瓦希里語 (sw) |
0.748 |
泰語 (th) |
0.793 |
土耳其語 (tr) |
0.807 |
烏爾都語 (ur) |
0.740 |
越南語 (vi) |
0.795 |
中文 (zh) |
0.8116 |
📄 許可證
本項目採用MIT許可證。
侷限性和偏差
請參考原始的DeBERTa - V3論文和不同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聯繫我,或在領英上與我交流。
調試與問題解決
需要注意的是,DeBERTa - v3於2021年末發佈,較舊版本的HF Transformers似乎在運行該模型時存在問題(例如,導致分詞器出現問題)。使用Transformers >= 4.13或更高版本可能會解決一些問題。另外,mDeBERTa目前不支持FP16,詳情見:https://github.com/microsoft/DeBERTa/issues/77
⚠️ 重要提示
若模型中心的其他多語言模型聲稱在非英語語言上的性能約為90%,作者很可能在測試過程中犯了錯誤。
💡 使用建議
使用Transformers >= 4.13或更高版本可能會解決模型運行時的一些問題。