🚀 flan-t5-large用于抽取式问答
本项目基于 flan-t5-large 模型,使用 SQuAD2.0 数据集进行微调。该模型针对抽取式问答任务,在问答对(包括无法回答的问题)上进行了训练。
更新:在 transformers 4.31.0 版本之后,不再需要 use_remote_code=True
。
此模型使用了通过 PEFT 库 实现的 LoRA 进行训练。
注意:为使模型正常工作,必须手动在问题开头添加 <cls>
标记。模型使用 <cls>
标记来进行“无答案”的预测。由于 t5 分词器不会自动添加这个特殊标记,因此需要手动添加。
🚀 快速开始
模型概述
属性 |
详情 |
语言模型 |
flan-t5-large |
语言 |
英语 |
下游任务 |
抽取式问答 |
训练数据 |
SQuAD 2.0 |
评估数据 |
SQuAD 2.0 |
基础设施 |
1x NVIDIA 3070 |
模型使用
使用 Transformers 库
此方法使用合并后的权重(基础模型权重 + LoRA 权重),以便在 Transformers 管道中简单使用。在使用 PEFT 库时,其性能与分别使用权重相同。
import torch
from transformers import(
AutoModelForQuestionAnswering,
AutoTokenizer,
pipeline
)
model_name = "sjrhuschlee/flan-t5-large-squad2"
nlp = pipeline(
'question-answering',
model=model_name,
tokenizer=model_name,
)
qa_input = {
'question': f'{nlp.tokenizer.cls_token}Where do I live?',
'context': 'My name is Sarah and I live in London'
}
res = nlp(qa_input)
model = AutoModelForQuestionAnswering.from_pretrained(
model_name,
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
question = f'{tokenizer.cls_token}Where do I live?'
context = 'My name is Sarah and I live in London'
encoding = tokenizer(question, context, return_tensors="pt")
output = model(
encoding["input_ids"],
attention_mask=encoding["attention_mask"]
)
all_tokens = tokenizer.convert_ids_to_tokens(encoding["input_ids"][0].tolist())
answer_tokens = all_tokens[torch.argmax(output["start_logits"]):torch.argmax(output["end_logits"]) + 1]
answer = tokenizer.decode(tokenizer.convert_tokens_to_ids(answer_tokens))
评估指标
{
"eval_HasAns_exact": 85.08771929824562,
"eval_HasAns_f1": 90.598422845031,
"eval_HasAns_total": 5928,
"eval_NoAns_exact": 88.47771236333053,
"eval_NoAns_f1": 88.47771236333053,
"eval_NoAns_total": 5945,
"eval_best_exact": 86.78514276088605,
"eval_best_exact_thresh": 0.0,
"eval_best_f1": 89.53654936623764,
"eval_best_f1_thresh": 0.0,
"eval_exact": 86.78514276088605,
"eval_f1": 89.53654936623776,
"eval_runtime": 1908.3189,
"eval_samples": 12001,
"eval_samples_per_second": 6.289,
"eval_steps_per_second": 0.787,
"eval_total": 11873
}
{
"eval_HasAns_exact": 85.99810785241249,
"eval_HasAns_f1": 91.296119057944,
"eval_HasAns_total": 10570,
"eval_best_exact": 85.99810785241249,
"eval_best_exact_thresh": 0.0,
"eval_best_f1": 91.296119057944,
"eval_best_f1_thresh": 0.0,
"eval_exact": 85.99810785241249,
"eval_f1": 91.296119057944,
"eval_runtime": 1508.9596,
"eval_samples": 10657,
"eval_samples_per_second": 7.062,
"eval_steps_per_second": 0.883,
"eval_total": 10570
}
使用 Peft 库
注意:此方法需要 PEFT 库的 PR https://github.com/huggingface/peft/pull/473 中的代码。
from peft import LoraConfig, PeftModelForQuestionAnswering
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
model_name = "sjrhuschlee/flan-t5-large-squad2"
📄 许可证
本项目采用 MIT 许可证。