You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行squad_to_dpr.py处理自定义SQuAD2训练集时遇变量未绑定错误

解决SQuAD2转DPR格式时的UnboundLocalError问题

根因

训练数据集存在无关联上下文的问答条目,脚本处理这类数据时cur_doc变量未初始化就被引用,而开发集无此类异常数据,因此能正常运行。

修复方案

1. 清理异常数据集

  • 打开urqa_train_nqa_v1.json,删除所有缺失context字段或context为空的问答条目,确保每个QA都关联有效上下文(符合SQuAD2格式要求)。

2. 给脚本添加容错逻辑

若不想修改数据集,可修改squad_to_dpr.py避免未赋值错误:

  • 找到遍历段落的代码块,添加空值判断:
for paragraph in doc["paragraphs"]:
    cur_doc = paragraph["context"]
    if not cur_doc:  # 跳过空上下文
        continue
    # 保留原有处理逻辑
  • 在引用cur_doc的代码段前,增加兜底判断:
if 'cur_doc' not in locals() or not cur_doc:
    continue  # 跳过无有效上下文的QA

3. 验证修复

重新执行转换命令:

python squad_to_dpr.py --squad_input_filename dataset_squad/urqa_train_nqa_v1.json --dpr_output_filename corpus_dpr/urqa_train_dpr.json --num_hard_negative_ctxs 2

额外建议

  • 用Haystack工具校验数据集格式:
from haystack.utils import SquadData
squad_data = SquadData.from_file("dataset_squad/urqa_train_nqa_v1.json")
squad_data.validate()  # 输出格式错误详情
  • 升级Haystack到最新稳定版,旧版本脚本可能存在边界场景的bug。

内容的提问来源于stack exchange,提问作者Muhammad Shakeel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 15:32:16