🚀 多语言e5-large-xnli-anli模型
本模型是在XNLI和ANLI数据集上对 intfloat/multilingual-e5-large 进行微调后的版本,可用于零样本分类等自然语言处理任务,为多语言文本分类提供了高效解决方案。
🚀 快速开始
本模型可通过零样本分类管道或手动使用PyTorch加载和应用,以下为你详细介绍使用方法。
✨ 主要特性
- 多语言支持:支持英语、阿拉伯语、保加利亚语、德语等多种语言。
- 微调优化:在XNLI和ANLI数据集上进行微调,提升了模型性能。
- 零样本分类:可直接对未见过的类别进行分类。
📦 安装指南
文档未提及安装步骤,可参考Hugging Face相关库的安装方法,确保安装 transformers
、torch
、datasets
、tokenizers
等依赖库。
💻 使用示例
基础用法
使用零样本分类管道
模型可以通过 zero-shot-classification
管道加载,示例代码如下:
from transformers import pipeline
classifier = pipeline("zero-shot-classification",
model="mjwong/multilingual-e5-large-xnli-anli")
你可以使用这个管道将序列分类到你指定的任何类别名称中。
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-anli"
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)
📚 详细文档
评估结果
模型在15种语言的XNLI测试集上进行了评估,使用的指标是准确率。
模型还在MultiNLI的开发集和ANLI的测试集上进行了评估,使用的指标同样是准确率。
训练超参数
训练过程中使用了以下超参数:
- 学习率:2e-05
- 训练批次大小:16
- 评估批次大小:16
- 随机种子:42
- 优化器:Adam,
betas=(0.9, 0.999)
,epsilon=1e-08
- 学习率调度器类型:线性
- 学习率调度器热身比例:0.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等人于2022年提出。该模型通过弱监督对比预训练学习文本嵌入,在多语言自然语言处理任务中表现出色。
📄 许可证
本模型使用MIT许可证。