🚀 多语言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许可证。