🚀 多語言e5大模型XNLI
該模型是在XNLI數據集上對 intfloat/multilingual-e5-large 進行微調後的版本,可用於零樣本分類等自然語言推理任務,支持多種語言。
🚀 快速開始
本模型可通過zero-shot-classification
管道或手動使用PyTorch加載,以下是詳細使用步驟。
✨ 主要特性
- 多語言支持:支持英語(en)、阿拉伯語(ar)、保加利亞語(bg)、德語(de)、希臘語(el)、西班牙語(es)、法語(fr)、印地語(hi)、俄語(ru)、斯瓦希里語(sw)、泰語(th)、土耳其語(tr)、烏爾都語(ur)、越南語(vi)和中文(zh)等多種語言。
- 零樣本分類:可以對未在訓練中明確指定的類別進行分類。
- 自然語言推理:可應用於自然語言推理(NLI)任務。
📦 安裝指南
文檔未提及安裝步驟,可參考transformers
庫的官方安裝指南來安裝所需依賴。
💻 使用示例
基礎用法
使用zero-shot-classification
管道加載模型:
from transformers import pipeline
classifier = pipeline("zero-shot-classification",
model="mjwong/multilingual-e5-large-xnli")
使用此管道對序列進行分類:
sequence_to_classify = "Angela Merkel ist eine Politikerin in Deutschland und Vorsitzende der CDU"
candidate_labels = ["politics", "economy", "entertainment", "environment"]
classifier(sequence_to_classify, candidate_labels)
若有多個候選標籤可能正確,可傳遞multi_class=True
獨立計算每個類別:
candidate_labels = ["politics", "economy", "entertainment", "environment"]
classifier(sequence_to_classify, candidate_labels, multi_label=True)
高級用法
手動使用PyTorch在NLI任務上應用模型:
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model_name = "mjwong/multilingual-e5-large-xnli"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
premise = "But I thought you'd sworn off coffee."
hypothesis = "I thought that you vowed to drink more coffee."
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, 2) for pred, name in zip(prediction, label_names)}
print(prediction)
📚 詳細文檔
評估結果
模型在XNLI測試集的15種語言上進行了評估,使用的指標是準確率:
模型還在MultiNLI的開發集和ANLI的測試集上進行了評估,使用的指標同樣是準確率:
訓練超參數
訓練過程中使用了以下超參數:
- 學習率:2e-05
- 訓練批次大小:16
- 評估批次大小:16
- 隨機種子:42
- 優化器:Adam(β1=0.9,β2=0.999,ε=1e-08)
- 學習率調度器類型:線性
- 學習率調度器熱身比例:0.1
- 訓練輪數:1
框架版本
- Transformers 4.28.1
- Pytorch 1.12.1+cu116
- Datasets 2.11.0
- Tokenizers 0.12.1
🔧 技術細節
本模型基於論文 Text Embeddings by Weakly-Supervised Contrastive Pre-training 進行開發,作者為Liang Wang、Nan Yang、Xiaolong Huang、Binxing Jiao、Linjun Yang、Daxin Jiang、Rangan Majumder、Furu Wei ,於2022年發表在arXiv上。
📄 許可證
本模型使用MIT許可證。