🚀 多语言e5大模型指令微调XNLI
本模型是基于XNLI数据集对 intfloat/multilingual-e5-large-instruct 模型进行微调后的版本,可用于多语言的零样本分类任务。
🚀 快速开始
本模型支持多语言,包括英语(en)、阿拉伯语(ar)、保加利亚语(bg)、德语(de)、希腊语(el)、西班牙语(es)、法语(fr)、印地语(hi)、俄语(ru)、斯瓦希里语(sw)、泰语(th)、土耳其语(tr)、乌尔都语(ur)、越南语(vi)和中文(zh)。
✨ 主要特性
- 多语言支持:支持15种语言,可广泛应用于不同语言环境。
- 零样本分类:无需大量标注数据,即可对新的类别进行分类。
- 微调优化:基于XNLI数据集进行微调,提高了模型在自然语言推理任务上的性能。
📦 安装指南
本模型使用了以下Python库,你可以使用pip
进行安装:
pip install transformers datasets torch tokenizers
💻 使用示例
基础用法
使用zero-shot-classification
管道加载模型:
from transformers import pipeline
classifier = pipeline("zero-shot-classification",
model="mjwong/multilingual-e5-large-instruct-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-instruct-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测试集上进行了评估,使用的指标同样是准确率:
训练超参数
训练过程中使用了以下超参数:
- 学习率(learning_rate):2e-05
- 训练批次大小(train_batch_size):16
- 评估批次大小(eval_batch_size):16
- 随机种子(seed):42
- 优化器(optimizer):Adam,β1=0.9,β2=0.999,ε=1e-08
- 学习率调度器类型(lr_scheduler_type):线性
- 学习率调度器预热比例(lr_scheduler_warmup_ratio):0.1
- 训练轮数(num_epochs):1
框架版本
- Transformers 4.28.1
- Pytorch 1.12.1+cu116
- Datasets 2.19.2
- 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,发表于arXiv 2022。
📄 许可证
本模型使用的是MIT许可证。