๐ Multilingual ernie-m-base-mnli-xnli
This multilingual model can perform natural language inference on 100 languages and is suitable for zero - shot classification, offering a powerful solution for multilingual text processing.
๐ Quick Start
This multilingual model can perform natural language inference (NLI) on 100 languages, making it well - suited for multilingual zero - shot classification. The underlying model was pre - trained by Baidu, based on Meta's RoBERTa, which was pre - trained on the CC100 multilingual dataset. It was then fine - tuned on the XNLI dataset, containing hypothesis - premise pairs from 15 languages, and the English MNLI dataset. The model was introduced by Baidu in this paper and outperforms RoBERTa models of equal size.
If you're seeking a faster (but less performant) model, try [multilingual - MiniLMv2 - L6 - mnli - xnli](https://huggingface.co/MoritzLaurer/multilingual - MiniLMv2 - L6 - mnli - xnli). Among models of equal size, [mDeBERTa - v3 - base - mnli - xnli](https://huggingface.co/MoritzLaurer/mDeBERTa - v3 - base - mnli - xnli) performs better on the XNLI benchmark. For better performance, you can try the slower [ernie - m - large - mnli - xnli](https://huggingface.co/MoritzLaurer/ernie - m - large - mnli - xnli).
โจ Features
- Multilingual Capability: Can perform NLI on 100 languages and is suitable for multilingual zero - shot classification.
- Outperforms Peers: Outperforms RoBERTa models of equal size.
๐ป Usage Examples
Basic Usage
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)
Advanced Usage
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)
๐ Documentation
Training data
This model was trained on the XNLI development dataset and the MNLI train dataset. The XNLI development set consists of 2490 professionally translated texts from English to 14 other languages (37350 texts in total) (see this paper). Note that the XNLI contains a training set of 15 machine - translated versions of the MNLI dataset for 15 languages. However, due to quality issues with these machine translations, this model was only trained on the professional translations from the XNLI development set and the original English MNLI training set (392 702 texts). Not using machine - translated texts can avoid overfitting the model to the 15 languages, prevent catastrophic forgetting of the other 85 languages ernie - m was pre - trained on, and significantly reduce training costs.
Training procedure
ernie - m - base - mnli - xnli was trained using the Hugging Face trainer with the following hyperparameters.
training_args = TrainingArguments(
num_train_epochs=3, # total number of training epochs
learning_rate=3e-05,
per_device_train_batch_size=16, # batch size per device during training
gradient_accumulation_steps=2,
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.01, # strength of weight decay
fp16=True,
)
Eval results
The model was evaluated on the XNLI test set on 15 languages (5010 texts per language, 75150 in total). Note that multilingual NLI models can classify NLI texts without receiving NLI training data in the specific language (cross - lingual transfer). This means the model can also do NLI on the other 85 languages ernie - m was trained on, but performance is likely lower than for the languages in XNLI.
Also, if other multilingual models on the model hub claim performance of around 90% on non - English languages, the authors probably made a mistake during testing since no recent papers show a multilingual average performance of more than a few points above 80% on XNLI (see here or here).
Property |
Details |
Model Type |
Multilingual ernie - m - base - mnli - xnli |
Training Data |
XNLI development dataset and MNLI train dataset |
Datasets |
avg_xnli |
mnli_m |
mnli_mm |
ar |
bg |
de |
el |
en |
es |
fr |
hi |
ru |
sw |
th |
tr |
ur |
vi |
zh |
Accuracy |
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 |
Inference text/sec (A100, batch = 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 |
๐ง Technical Details
The ernie - m architecture is only supported with transformers==4.27 or higher (which was not yet released as of 03.03.23 and causes an error in the inference widget). To run the model before the release of 4.27, you need to install transformers from source with: pip install git+https://github.com/huggingface/transformers
and the sentencepiece tokenizer with: pip install sentencepiece
. After the release, you can run: pip install transformers[sentencepiece]>=4.27
๐ License
This project is licensed under the apache - 2.0 license.
Limitations and bias
Please consult the original ernie - m paper and literature on different NLI datasets for potential biases.
Citation
If you use this model, please cite: 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.
Ideas for cooperation or questions?
If you have questions or ideas for cooperation, contact me at m{dot}laurer{at}vu{dot}nl or [LinkedIn](https://www.linkedin.com/in/moritz - laurer/)
โ ๏ธ Important Note
The ernie - m architecture is only supported with transformers==4.27 or higher. As of 03.03.23, it causes an error in the inference widget.
๐ก Usage Tip
To run the model before the release of transformers 4.27, install transformers from source with pip install git+https://github.com/huggingface/transformers
and the sentencepiece tokenizer with pip install sentencepiece
. After the release, use pip install transformers[sentencepiece]>=4.27
.