数据集:
基于LONGFORMER-BASE-4096模型在SQuAD v1上的微调版本
这是针对问答任务在SQuAD v1数据集上微调后的longformer-base-4096模型。
Longformer模型由AllenAI的Iz Beltagy、Matthew E. Peters和Arman Cohan开发。如论文所述:
Longformer
是一个适用于长文档的类BERT模型。
该预训练模型可处理最长4096个标记的序列。
模型训练
本模型在Google Colab V100 GPU上完成训练。微调代码可在此处获取:
。
训练Longformer进行QA任务时需注意:
默认情况下Longformer对所有标记采用滑动窗口局部注意力机制。但对于QA任务,所有问题标记应具有全局注意力。具体细节请参阅原论文。LongformerForQuestionAnswering
模型已自动实现该功能,需确保:
- 输入序列必须包含三个分隔标记,即应按此格式编码:
<s>问题</s></s>上下文</s>
。若将问题和上下文作为输入对编码,分词器会自动处理,无需额外操作。
input_ids
必须始终以批处理形式输入。
性能指标
指标 |
值 |
精确匹配率 |
85.1466 |
F1分数 |
91.5415 |
模型实战 🚀
import torch
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
tokenizer = AutoTokenizer.from_pretrained("valhalla/longformer-base-4096-finetuned-squadv1")
model = AutoModelForQuestionAnswering.from_pretrained("valhalla/longformer-base-4096-finetuned-squadv1")
text = "Huggingface已实现NLP民主化。感谢Huggingface的贡献。"
question = "Huggingface取得了什么成就?"
encoding = tokenizer(question, text, return_tensors="pt")
input_ids = encoding["input_ids"]
attention_mask = encoding["attention_mask"]
start_scores, end_scores = model(input_ids, attention_mask=attention_mask)
all_tokens = tokenizer.convert_ids_to_tokens(input_ids[0].tolist())
answer_tokens = all_tokens[torch.argmax(start_scores) : torch.argmax(end_scores)+1]
answer = tokenizer.decode(tokenizer.convert_tokens_to_ids(answer_tokens))
当前pipeline
尚未支持LongformerForQuestionAnswering
,待支持后将更新本卡片。
由Suraj Patil倾情打造 
