from openmind import pipeline
classifier = pipeline("zero-shot-classification",
model="huangjingwang/xlm-roberta-large-it-mnli", device=0, use_fast=True, multi_label=True)
# we will classify the following wikipedia entry about Sardinia"
sequence_to_classify = "La Sardegna è una regione italiana a statuto speciale di 1 592 730 abitanti con capoluogo Cagliari, la cui denominazione bilingue utilizzata nella comunicazione ufficiale è Regione Autonoma della Sardegna / Regione Autònoma de Sardigna."
# we can specify candidate labels in Italian:
candidate_labels = ["geografia", "politica", "macchine", "cibo", "moda"]
classifier(sequence_to_classify, candidate_labels)
# {'labels': ['geografia', 'moda', 'politica', 'macchine', 'cibo'],
# 'scores': [0.38871392607688904, 0.22633370757102966, 0.19398456811904907, 0.13735772669315338, 0.13708525896072388]}| matched-it 准确率 | mismatched-it 准确率 | |
|---|---|---|
| XLM-roBERTa-large-it-mnli | 84.75 | 85.39 |
本模型以 xlm-roberta-large 为基础,在从 MNLI 语料库自动翻译版本中提取的 NLI 数据子集上进行了微调。其旨在用于零样本文本分类,例如配合 Hugging Face 的 ZeroShotClassificationPipeline 使用。
本模型旨在用于意大利语文本的零样本文本分类。由于基础模型在 100 种不同语言上进行了预训练,因此该模型在上述列出语言之外的其他语言中也表现出一定的有效性。有关预训练语言的完整列表,请参见 XLM Roberata 论文 的附录 A。对于仅英语的分类,建议使用 bart-large-mnli 或 蒸馏版 bart MNLI 模型。
可通过 zero-shot-classification 管道加载模型,如下所示:
from transformers import pipeline
classifier = pipeline("zero-shot-classification",
model="Jiva/xlm-roberta-large-it-mnli", device=0, use_fast=True, multi_label=True) 然后,你可以使用上述任何一种语言进行分类。你甚至可以用一种语言传入标签,用另一种语言传入待分类的序列:
# we will classify the following wikipedia entry about Sardinia"
sequence_to_classify = "La Sardegna è una regione italiana a statuto speciale di 1 592 730 abitanti con capoluogo Cagliari, la cui denominazione bilingue utilizzata nella comunicazione ufficiale è Regione Autonoma della Sardegna / Regione Autònoma de Sardigna."
# we can specify candidate labels in Italian:
candidate_labels = ["geografia", "politica", "macchine", "cibo", "moda"]
classifier(sequence_to_classify, candidate_labels)
# {'labels': ['geografia', 'moda', 'politica', 'macchine', 'cibo'],
# 'scores': [0.38871392607688904, 0.22633370757102966, 0.19398456811904907, 0.13735772669315338, 0.13708525896072388]}默认的假设模板为英文,即 This text is {}。使用此模型时,提供翻译后的模板可获得更优结果:
sequence_to_classify = "La Sardegna è una regione italiana a statuto speciale di 1 592 730 abitanti con capoluogo Cagliari, la cui denominazione bilingue utilizzata nella comunicazione ufficiale è Regione Autonoma della Sardegna / Regione Autònoma de Sardigna."
candidate_labels = ["geografia", "politica", "macchine", "cibo", "moda"]
hypothesis_template = "si parla di {}"
# classifier(sequence_to_classify, candidate_labels, hypothesis_template=hypothesis_template)
# 'scores': [0.6068345904350281, 0.34715887904167175, 0.32433947920799255, 0.3068877160549164, 0.18744681775569916]}# pose sequence as a NLI premise and label as a hypothesis
from transformers import AutoModelForSequenceClassification, AutoTokenizer
nli_model = AutoModelForSequenceClassification.from_pretrained('Jiva/xlm-roberta-large-it-mnli')
tokenizer = AutoTokenizer.from_pretrained('Jiva/xlm-roberta-large-it-mnli')
premise = sequence
hypothesis = f'si parla di {}.'
# run through model pre-trained on MNLI
x = tokenizer.encode(premise, hypothesis, return_tensors='pt',
truncation_strategy='only_first')
logits = nli_model(x.to(device))[0]
# we throw away "neutral" (dim 1) and take the probability of
# "entailment" (2) as the probability of the label being true
entail_contradiction_logits = logits[:,[0,2]]
probs = entail_contradiction_logits.softmax(dim=1)
prob_label_is_true = probs[:,1]目前已在完整训练集上对模型进行了重新训练。由于部分句子对被翻译模型译得糟糕,训练集中已移除了约 1000 个这样的句子对。
| 指标 | 值 |
|---|---|
| learning_rate | 4e-6 |
| optimizer | AdamW |
| batch_size | 80 |
| mcc | 0.77 |
| train_loss | 0.34 |
| eval_loss | 0.40 |
| stopped_at_step | 9754 |
如原始论文所述,该模型已在包含 100 种语言的数据集上进行了预训练。随后,在 MNLI 数据集的意大利语翻译版本(目前仅使用了 85% 的训练集)上针对自然语言推理(NLI)任务进行了微调。用于文本翻译的模型为 Helsinki-NLP/opus-mt-en-it,最大输出序列长度设为 120。模型训练了 1 个 epoch,学习率为 4e-6,批大小为 80,目前在剩余 15% 的训练集上准确率达到 82。