🚀 Longformer-base-4096在SQuAD v2上微調
本項目是將 Longformer-base-4096模型 在 SQuAD v2 數據集上進行微調,以用於**問答(Q&A)**下游任務。
✨ 主要特性
- 適用於長文檔:Longformer 是一種用於處理長文檔的Transformer模型。
longformer-base-4096
是一個類似BERT的模型,它基於RoBERTa的檢查點,並在長文檔上進行了掩碼語言模型(MLM)預訓練,支持長度達4096的序列。
- 獨特的注意力機制:Longformer結合了滑動窗口(局部)注意力和全局注意力。全局注意力可根據任務進行用戶配置,使模型能夠學習特定任務的表示。
📦 安裝指南
若要從 datasets 加載數據集,可按以下步驟操作:
!pip install datasets
from datasets import load_dataset
dataset = load_dataset('squad_v2')
💻 使用示例
基礎用法
以下是使用該模型進行問答任務的示例代碼:
import torch
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
ckpt = "mrm8488/longformer-base-4096-finetuned-squadv2"
tokenizer = AutoTokenizer.from_pretrained(ckpt)
model = AutoModelForQuestionAnswering.from_pretrained(ckpt)
text = "Huggingface has democratized NLP. Huge thanks to Huggingface for this."
question = "What has Huggingface done ?"
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))
高級用法
使用HF pipleine
進行問答任務:
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
ckpt = "mrm8488/longformer-base-4096-finetuned-squadv2"
tokenizer = AutoTokenizer.from_pretrained(ckpt)
model = AutoModelForQuestionAnswering.from_pretrained(ckpt)
qa = pipeline("question-answering", model=model, tokenizer=tokenizer)
text = "Huggingface has democratized NLP. Huge thanks to Huggingface for this."
question = "What has Huggingface done?"
qa({"question": question, "context": text})
如果在給定的上下文中詢問不存在的內容,無答案的輸出將是 <s>
。
📚 詳細文檔
數據集詳情
- 數據集ID:
squad_v2
,來自 HuggingFace/Datasets
| 數據集 | 劃分 | 樣本數量 |
| ------ | ---- | -------- |
| squad_v2 | 訓練集 | 130319 |
| squad_v2 | 驗證集 | 11873 |
你可以在 Datasets Viewer 中查看更多關於此數據集及其他數據集的信息。
模型微調
訓練腳本是 此腳本 的略微修改版本。
模型指標
任務類型 |
數據集 |
指標類型 |
指標值 |
指標名稱 |
驗證狀態 |
問答 |
squad_v2 |
exact_match |
79.9242 |
Exact Match |
已驗證 |
問答 |
squad_v2 |
f1 |
83.3467 |
F1 |
已驗證 |
其他信息