模型:
MoritzLaurer/multilingual-MiniLMv2-L12-mnli-xnli
这个多语言模型可以对100多种语言进行自然语言推断(NLI),因此也适用于多语言零-shot分类。基于微软的多语言MiniLM-L12模型,该模型是从XLM-RoBERTa-large中蒸馏而来的(详见详细信息 in the original paper 和最新信息 this repo )。然后对该模型进行了微调,使用了来自15种语言的假设-前提对以及英文 MNLI dataset 。
蒸馏模型的主要优点是相比于其教师模型(XLM-RoBERTa-large),它们更小巧(推断速度更快,内存需求更低)。缺点是它们失去了一部分较大教师模型的性能。
为了获得最高的推断速度,我建议使用 6-layer model (此页面上的模型有12层,较慢)。为了获得更高的性能,我推荐使用 mDeBERTa-v3-base-mnli-xnli (截至2023年2月14日)。
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)
NLI用例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)) # device = "cuda:0" or "cpu"
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个文本)(详见 this paper )。请注意,XNLI包含了15种机器翻译版本的MNLI数据集的训练集,但由于这些机器翻译的质量问题,该模型只是在XNLI开发集的专业翻译文本和原始英文MNLI训练集(392702个文本)上进行训练。不使用机器翻译文本可以避免将模型过拟合到这15种语言上;避免忘记了其他预训练语言的信息;并显著降低了训练成本。
该模型使用Hugging Face的trainer训练,并使用以下超参数。具体的基础模型为 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中的语言。
多语言MiniLM-L12在论文中报告的XNLI平均性能为0.711( see table 11 )。这次的重新实现的平均性能为0.75。性能提升可能要归功于MNLI的添加以及该模型是从XLM-RoBERTa-large而不是-base(多语言MiniLM-L12-v2)蒸馏得到的。
| Datasets | avg_xnli | ar | bg | de | el | en | es | fr | hi | ru | sw | th | tr | ur | vi | zh |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Accuracy | 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 |
| Speed text/sec (A100 GPU, eval_batch=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 |
| Datasets | mnli_m | mnli_mm |
|---|---|---|
| Accuracy | 0.818 | 0.831 |
| Speed text/sec (A100 GPU, eval_batch=120) | 2912.0 | 2902.0 |
请参考原始论文和不同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{点}laurer{at}vu{点}nl或 LinkedIn 与我联系。