本模型在 MultiNLI、Fever-NLI、对抗性自然语言推理(ANLI)、LingNLI 和 WANLI 数据集上进行了微调,这些数据集包含 885,242 个自然语言推理(NLI)假设-前提对。截至 2022 年 6 月 6 日,该模型是 Hugging Face Hub 上性能最佳的 NLI 模型,可用于零样本分类。它在 ANLI 基准 上的表现显著优于所有其他大型模型。
基础模型是 微软的 DeBERTa-v3-large。与 BERT、RoBERTa 等传统掩码语言模型相比,DeBERTa-v3 融合了多项最新技术创新,详见 论文。
import torch
import argparse
from openmind import pipeline, is_torch_npu_available
import time
def parse_args():
parser = argparse.ArgumentParser(description="Eval the model")
parser.add_argument(
"--model_name_or_path",
type=str,
help="path or model",
default="JeffDing/DeBERTa-v3-large-mnli-fever-anli-ling-wanli",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
model_path = args.model_name_or_path
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
start_time = time.time()
classifier = pipeline("zero-shot-classification", model=model_path,device_map=device)
sequence_to_classify = "Angela Merkel ist eine Politikerin in Deutschland und Vorsitzende der CDU"
candidate_labels = ["politics", "economy", "entertainment", "environment"]
output = classifier(sequence_to_classify, candidate_labels, multi_label=False)
print(output)
end_time = time.time()
print(f"硬件环境:{device},推理执行时间:{end_time - start_time}秒")
if __name__ == "__main__":
main()from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="MoritzLaurer/DeBERTa-v3-large-mnli-fever-anli-ling-wanli")
sequence_to_classify = "Angela Merkel is a politician in Germany and leader of the CDU"
candidate_labels = ["politics", "economy", "entertainment", "environment"]
output = classifier(sequence_to_classify, candidate_labels, multi_label=False)
print(output)from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model_name = "MoritzLaurer/DeBERTa-v3-large-mnli-fever-anli-ling-wanli"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
premise = "I first thought that I liked the movie, but upon second thought it was actually disappointing."
hypothesis = "The movie was not good."
input = tokenizer(premise, hypothesis, truncation=True, return_tensors="pt")
output = model(input["input_ids"].to(device)) # device = "cuda:0" or "cpu"
prediction = torch.softmax(output["logits"][0], -1).tolist()
label_names = ["entailment", "neutral", "contradiction"]
prediction = {name: round(float(pred) * 100, 1) for pred, name in zip(prediction, label_names)}
print(prediction)DeBERTa-v3-large-mnli-fever-anli-ling-wanli 是在 MultiNLI、Fever-NLI、对抗性自然语言推理(ANLI)、LingNLI 和 WANLI 数据集上训练的,这些数据集包含 885,242 个自然语言推理假设-前提对。请注意,由于数据集存在质量问题,SNLI 被明确排除在外。更多的数据并不一定能造就更好的自然语言推理模型。
DeBERTa-v3-large-mnli-fever-anli-ling-wanli 使用 Hugging Face 训练器进行训练,采用以下超参数。需要注意的是,在我的测试中,使用更多轮次进行更长时间的训练会损害性能(过拟合)。
training_args = TrainingArguments(
num_train_epochs=4, # total number of training epochs
learning_rate=5e-06,
per_device_train_batch_size=16, # batch size per device during training
gradient_accumulation_steps=2, # doubles the effective batch_size to 32, while decreasing memory requirements
per_device_eval_batch_size=64, # batch size for evaluation
warmup_ratio=0.06, # number of warmup steps for learning rate scheduler
weight_decay=0.01, # strength of weight decay
fp16=True # mixed precision training
)该模型使用 MultiNLI、ANLI、LingNLI、WANLI 的测试集以及 Fever-NLI 的开发集进行了评估。所使用的指标为准确率。
该模型在每个数据集上均达到了最先进的性能。令人惊讶的是,它在 ANLI 上的表现比之前的最先进水平(ALBERT-XXL)高出 8.3%。我认为这是因为 ANLI 的创建初衷是为了“欺骗”像 RoBERTa(或 ALBERT)这样的掩码语言模型,而 DeBERTa-v3 采用了更优的预训练目标(RTD)、解耦注意力机制,并且我在更高质量的 NLI 数据上对其进行了微调。
| 数据集 | mnli_test_m | mnli_test_mm | anli_test | anli_test_r3 | ling_test | wanli_test |
|---|---|---|---|---|---|---|
| 准确率 | 0.912 | 0.908 | 0.702 | 0.64 | 0.87 | 0.77 |
| 速度(文本/秒,A100 GPU) | 696.0 | 697.0 | 488.0 | 425.0 | 828.0 | 980.0 |
有关训练数据和潜在偏差的更多信息,请参考原始的 DeBERTa-v3 论文以及关于不同 NLI 数据集的文献。该模型会复现训练数据中的统计模式。
如果您使用此模型,请引用:Laurer, Moritz, Wouter van Atteveldt, Andreu Salleras Casas, and Kasper Welbers. 2022. ‘Less Annotating, More Classifying – Addressing the Data Scarcity Issue of Supervised Machine Learning with Deep Transfer Learning and BERT - NLI’. Preprint, June. Open Science Framework. https://osf.io/74b8k.
如果您有问题或合作想法,请通过 m{dot}laurer{at}vu{dot}nl 或 LinkedIn 与我联系。
请注意,DeBERTa-v3 于 2021 年 12 月 6 日发布,较旧版本的 HF Transformers 在运行该模型时似乎存在问题(例如,导致分词器出现问题)。使用 Transformers>=4.13 可能会解决一些问题。