Mt5 Large Finetuned Mnli Xtreme Xnli
基于多语言T5模型微调,专为零样本文本分类任务设计,支持15种语言
下载量 964
发布时间 : 3/2/2022
模型简介
该模型在多语言自然语言推理数据集上微调,适用于零样本文本分类任务,特别针对非英语语言场景。
模型特点
多语言支持
支持15种语言的零样本文本分类任务
NLI微调
在MNLI和xtreme_xnli数据集上进行了专门微调
文本到文本架构
保留T5模型的文本生成特性,通过特定前缀标识任务
模型能力
多语言文本分类
零样本学习
自然语言推理
使用案例
文本分类
多语言情感分析
无需特定语言训练数据即可进行情感分类
内容审核
跨语言识别不当内容
🚀 mt5-large-finetuned-mnli-xtreme-xnli
本模型基于预训练的大型 multilingual-t5(也可从 models 获取),并在英文 MNLI 和 xtreme_xnli 训练集上进行微调。它旨在用于零样本文本分类,灵感来源于 xlm-roberta-large-xnli。
🚀 快速开始
本模型专为零样本文本分类而设计,尤其适用于英文以外的语言。它在英文 MNLI 和 xtreme_xnli 训练集(一个多语言自然语言推理数据集)上进行了微调。因此,该模型可用于 XNLI 语料库中的任何语言:
- 阿拉伯语
- 保加利亚语
- 中文
- 英语
- 法语
- 德语
- 希腊语
- 印地语
- 俄语
- 西班牙语
- 斯瓦希里语
- 泰语
- 土耳其语
- 乌尔都语
- 越南语
根据 xlm-roberta-large-xnli 中的建议,若仅进行英文分类,你可以考虑以下模型:
✨ 主要特性
- 基于预训练的多语言 T5 模型进行微调,适用于多语言零样本文本分类。
- 微调后保留了文本到文本的特性,输出为文本形式。
- 可用于 XNLI 语料库中的多种语言。
💻 使用示例
基础用法
from torch.nn.functional import softmax
from transformers import MT5ForConditionalGeneration, MT5Tokenizer
model_name = "alan-turing-institute/mt5-large-finetuned-mnli-xtreme-xnli"
tokenizer = MT5Tokenizer.from_pretrained(model_name)
model = MT5ForConditionalGeneration.from_pretrained(model_name)
model.eval()
sequence_to_classify = "¿A quién vas a votar en 2020?"
candidate_labels = ["Europa", "salud pública", "política"]
hypothesis_template = "Este ejemplo es {}."
ENTAILS_LABEL = "▁0"
NEUTRAL_LABEL = "▁1"
CONTRADICTS_LABEL = "▁2"
label_inds = tokenizer.convert_tokens_to_ids(
[ENTAILS_LABEL, NEUTRAL_LABEL, CONTRADICTS_LABEL])
def process_nli(premise: str, hypothesis: str):
""" process to required xnli format with task prefix """
return "".join(['xnli: premise: ', premise, ' hypothesis: ', hypothesis])
# construct sequence of premise, hypothesis pairs
pairs = [(sequence_to_classify, hypothesis_template.format(label)) for label in
candidate_labels]
# format for mt5 xnli task
seqs = [process_nli(premise=premise, hypothesis=hypothesis) for
premise, hypothesis in pairs]
print(seqs)
# ['xnli: premise: ¿A quién vas a votar en 2020? hypothesis: Este ejemplo es Europa.',
# 'xnli: premise: ¿A quién vas a votar en 2020? hypothesis: Este ejemplo es salud pública.',
# 'xnli: premise: ¿A quién vas a votar en 2020? hypothesis: Este ejemplo es política.']
inputs = tokenizer.batch_encode_plus(seqs, return_tensors="pt", padding=True)
out = model.generate(**inputs, output_scores=True, return_dict_in_generate=True,
num_beams=1)
# sanity check that our sequences are expected length (1 + start token + end token = 3)
for i, seq in enumerate(out.sequences):
assert len(
seq) == 3, f"generated sequence {i} not of expected length, 3." \
f" Actual length: {len(seq)}"
# get the scores for our only token of interest
# we'll now treat these like the output logits of a `*ForSequenceClassification` model
scores = out.scores[0]
# scores has a size of the model's vocab.
# However, for this task we have a fixed set of labels
# sanity check that these labels are always the top 3 scoring
for i, sequence_scores in enumerate(scores):
top_scores = sequence_scores.argsort()[-3:]
assert set(top_scores.tolist()) == set(label_inds), \
f"top scoring tokens are not expected for this task." \
f" Expected: {label_inds}. Got: {top_scores.tolist()}."
# cut down scores to our task labels
scores = scores[:, label_inds]
print(scores)
# tensor([[-2.5697, 1.0618, 0.2088],
# [-5.4492, -2.1805, -0.1473],
# [ 2.2973, 3.7595, -0.1769]])
# new indices of entailment and contradiction in scores
entailment_ind = 0
contradiction_ind = 2
# we can show, per item, the entailment vs contradiction probas
entail_vs_contra_scores = scores[:, [entailment_ind, contradiction_ind]]
entail_vs_contra_probas = softmax(entail_vs_contra_scores, dim=1)
print(entail_vs_contra_probas)
# tensor([[0.0585, 0.9415],
# [0.0050, 0.9950],
# [0.9223, 0.0777]])
# or we can show probas similar to `ZeroShotClassificationPipeline`
# this gives a zero-shot classification style output across labels
entail_scores = scores[:, entailment_ind]
entail_probas = softmax(entail_scores, dim=0)
print(entail_probas)
# tensor([7.6341e-03, 4.2873e-04, 9.9194e-01])
print(dict(zip(candidate_labels, entail_probas.tolist())))
# {'Europa': 0.007634134963154793,
# 'salud pública': 0.0004287279152777046,
# 'política': 0.9919371604919434}
高级用法
# 注意:TF 等效模型的 `generate` 函数与 PyTorch 版本不完全一致,上述代码无法直接迁移。
# 该模型目前与现有的 `zero-shot-classification` 管道不兼容。
🔧 技术细节
本模型在 mC4 中的 101 种语言上进行了预训练,如 mt5 论文 所述。然后,它在 mt5_xnli_translate_train 任务上进行了 8000 步的微调,微调方式与 官方仓库 中描述的类似,并参考了 Stephen Mayhew 的笔记本。最后,将得到的模型转换为 :hugging_face: 格式。
📚 详细文档
评估结果
XNLI 测试集上的准确率:
语言代码 | 准确率 |
---|---|
ar | 81.0 |
bg | 85.0 |
de | 84.3 |
el | 84.3 |
en | 88.8 |
es | 85.3 |
fr | 83.9 |
hi | 79.9 |
ru | 82.6 |
sw | 78.0 |
th | 81.0 |
tr | 81.6 |
ur | 76.4 |
vi | 81.7 |
zh | 82.3 |
平均 | 82.4 |
📄 许可证
本模型采用 Apache-2.0 许可证。
Phi 2 GGUF
其他
Phi-2是微软开发的一个小型但强大的语言模型,具有27亿参数,专注于高效推理和高质量文本生成。
大型语言模型 支持多种语言
P
TheBloke
41.5M
205
Roberta Large
MIT
基于掩码语言建模目标预训练的大型英语语言模型,采用改进的BERT训练方法
大型语言模型 英语
R
FacebookAI
19.4M
212
Distilbert Base Uncased
Apache-2.0
DistilBERT是BERT基础模型的蒸馏版本,在保持相近性能的同时更轻量高效,适用于序列分类、标记分类等自然语言处理任务。
大型语言模型 英语
D
distilbert
11.1M
669
Llama 3.1 8B Instruct GGUF
Meta Llama 3.1 8B Instruct 是一个多语言大语言模型,针对多语言对话用例进行了优化,在常见的行业基准测试中表现优异。
大型语言模型 英语
L
modularai
9.7M
4
Xlm Roberta Base
MIT
XLM-RoBERTa是基于100种语言的2.5TB过滤CommonCrawl数据预训练的多语言模型,采用掩码语言建模目标进行训练。
大型语言模型 支持多种语言
X
FacebookAI
9.6M
664
Roberta Base
MIT
基于Transformer架构的英语预训练模型,通过掩码语言建模目标在海量文本上训练,支持文本特征提取和下游任务微调
大型语言模型 英语
R
FacebookAI
9.3M
488
Opt 125m
其他
OPT是由Meta AI发布的开放预训练Transformer语言模型套件,参数量从1.25亿到1750亿,旨在对标GPT-3系列性能,同时促进大规模语言模型的开放研究。
大型语言模型 英语
O
facebook
6.3M
198
1
基于transformers库的预训练模型,适用于多种NLP任务
大型语言模型
Transformers

1
unslothai
6.2M
1
Llama 3.1 8B Instruct
Llama 3.1是Meta推出的多语言大语言模型系列,包含8B、70B和405B参数规模,支持8种语言和代码生成,优化了多语言对话场景。
大型语言模型
Transformers 支持多种语言

L
meta-llama
5.7M
3,898
T5 Base
Apache-2.0
T5基础版是由Google开发的文本到文本转换Transformer模型,参数规模2.2亿,支持多语言NLP任务。
大型语言模型 支持多种语言
T
google-t5
5.4M
702
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers 支持多种语言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers 英语

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统 中文
R
uer
2,694
98