🚀 多語言 ernie-m-base-mnli-xnli
多語言 ernie-m-base-mnli-xnli 模型可對 100 種語言進行自然語言推理(NLI),適用於多語言零樣本分類任務。該模型在同等規模下,性能優於 RoBERTa 模型。
🚀 快速開始
本模型可用於多語言自然語言推理和零樣本分類任務。以下是使用示例:
簡單的零樣本分類管道
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="MoritzLaurer/ernie-m-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)
NLI 使用場景
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model_name = "MoritzLaurer/ernie-m-base-mnli-xnli"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device)
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)
✨ 主要特性
- 多語言支持:可對 100 種語言進行自然語言推理。
- 零樣本分類:適用於多語言零樣本分類任務。
- 性能優越:在同等規模下,性能優於 RoBERTa 模型。
📦 安裝指南
ernie-m 架構僅支持 transformers==4.27 或更高版本(截至 2023 年 3 月 3 日尚未發佈,會在推理小部件中引發錯誤)。在 4.27 版本發佈之前,若要運行該模型,你需要從源代碼安裝 transformers:
pip install git+https://github.com/huggingface/transformers
同時安裝 sentencepiece 分詞器:
pip install sentencepiece
4.27 版本發佈後,你可以運行:
pip install transformers[sentencepiece]>=4.27
📚 詳細文檔
模型描述
這個多語言模型可以對 100 種語言進行自然語言推理(NLI),因此也適用於多語言零樣本分類。其基礎模型由百度基於 Meta 的 RoBERTa 進行預訓練(在 CC100 多語言數據集 上預訓練)。然後在 XNLI 數據集 上進行微調,該數據集包含 15 種語言的假設 - 前提對,以及英文 MNLI 數據集。該模型由百度在 這篇論文 中提出,在同等規模下,性能優於 RoBERTa 模型。
如果你正在尋找一個更快(但性能稍低)的模型,可以嘗試 multilingual-MiniLMv2-L6-mnli-xnli。在同等規模的模型中,mDeBERTa-v3-base-mnli-xnli 在 XNLI 基準測試中表現更好。若追求更好的性能,你可以嘗試速度較慢的 ernie-m-large-mnli-xnli。
訓練數據
該模型在 XNLI 開發數據集和 MNLI 訓練數據集上進行訓練。XNLI 開發集包含 2490 條從英語專業翻譯為其他 14 種語言的文本(總共 37350 條文本)(詳見 這篇論文)。需要注意的是,XNLI 包含 15 種語言的 MNLI 數據集的 15 個機器翻譯版本的訓練集,但由於這些機器翻譯存在質量問題,該模型僅在 XNLI 開發集的專業翻譯和原始英文 MNLI 訓練集(392702 條文本)上進行訓練。不使用機器翻譯文本可以避免模型對 15 種語言過擬合;避免對 ernie-m 預訓練的其他 85 種語言產生災難性遺忘;並顯著降低訓練成本。
訓練過程
ernie-m-base-mnli-xnli 使用 Hugging Face 訓練器進行訓練,使用了以下超參數:
training_args = TrainingArguments(
num_train_epochs=3,
learning_rate=3e-05,
per_device_train_batch_size=16,
gradient_accumulation_steps=2,
per_device_eval_batch_size=16,
warmup_ratio=0.1,
weight_decay=0.01,
fp16=True,
)
評估結果
該模型在 15 種語言的 XNLI 測試集上進行了評估(每種語言 5010 條文本,總共 75150 條)。需要注意的是,多語言 NLI 模型能夠在不接收特定語言的 NLI 訓練數據的情況下對 NLI 文本進行分類(跨語言遷移)。這意味著該模型也能夠對 mDeBERTa 訓練的其他 85 種語言進行 NLI,但性能很可能低於 XNLI 中可用的語言。
另外需要注意的是,如果模型中心的其他多語言模型聲稱在非英語語言上的性能約為 90%,作者很可能在測試過程中出現了錯誤,因為最新的論文均未顯示在 XNLI 上的多語言平均性能比 80% 高出幾個百分點(詳見 此處 或 此處)。
數據集 |
平均 XNLI |
MNLI-m |
MNLI-mm |
阿拉伯語 |
保加利亞語 |
德語 |
希臘語 |
英語 |
西班牙語 |
法語 |
印地語 |
俄語 |
斯瓦希里語 |
泰語 |
土耳其語 |
烏爾都語 |
越南語 |
中文 |
準確率 |
0.78 |
0.849 |
0.85 |
0.777 |
0.812 |
0.804 |
0.797 |
0.854 |
0.814 |
0.803 |
0.744 |
0.784 |
0.711 |
0.765 |
0.776 |
0.717 |
0.793 |
0.749 |
推理速度(文本/秒,A100,批量大小 = 120) |
3310.0 |
1967.0 |
1944.0 |
3443.0 |
3277.0 |
3338.0 |
2884.0 |
3696.0 |
3439.0 |
3071.0 |
3094.0 |
3222.0 |
3445.0 |
3490.0 |
3690.0 |
3175.0 |
3295.0 |
3096.0 |
🔧 技術細節
模型基於 Meta 的 RoBERTa 架構,先在 CC100 多語言數據集上進行預訓練,再在 XNLI 開發數據集和 MNLI 訓練數據集上進行微調。通過不使用機器翻譯文本,避免了模型過擬合和災難性遺忘問題,同時降低了訓練成本。
📄 許可證
本模型採用 Apache-2.0 許可證。
📋 侷限性和偏差
請參考原始 ernie-m 論文和不同 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 聯繫我,或在 LinkedIn 上與我交流。