Flan-T5-XXL问答任务得分低且答案错误,求修正方案
问题原因及修正方案
核心问题
Flan-T5-XXL是序列到序列(seq2seq)模型,专为文本生成类任务优化,并非抽取式问答模型。你错误使用AutoModelForQuestionAnswering加载模型,该类是为BERT等抽取式QA模型设计的,不适配Flan-T5的架构,导致输出异常。
修正后的代码
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline import torch model_id = "~/Downloads/test_LLM/flan-t5-xxl" DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # 正确加载Flan-T5的seq2seq模型 tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForSeq2SeqLM.from_pretrained(model_id, return_dict=False).to(DEVICE) # 使用文本生成pipeline适配Flan-T5的任务特性 qa_pipeline = pipeline( "text2text-generation", model=model, tokenizer=tokenizer, device=0 if DEVICE == "cuda" else -1 ) question = "What is 42?" context = "42 is the answer to life, the universe and everything" # 构造Flan-T5适配的指令式prompt(关键:明确任务要求) prompt = f"Answer the question based on the given context. Context: {context} Question: {question}" # 生成回答 result = qa_pipeline(prompt, max_new_tokens=50) print(result[0]['generated_text'])
关键修改点
- 模型加载:替换
AutoModelForQuestionAnswering为AutoModelForSeq2SeqLM,匹配Flan-T5的seq2seq架构。 - Pipeline选择:使用
text2text-generation而非抽取式question-answeringpipeline,适配模型的文本生成能力。 - Prompt设计:添加明确的任务指令,Flan-T5经过指令微调,清晰的prompt能大幅提升回答准确性。
内容的提问来源于stack exchange,提问作者AnonX
相关产品推荐
相关产品推荐

