斯坦福问答数据集(Stanford Question Answering Dataset, SQuAD)

斯坦福问答数据集(Stanford Question Answering Dataset, SQuAD)

斯坦福问答数据集(Stanford Question Answering Dataset, SQuAD) 是一个广泛用于 机器阅读理解(Machine Reading Comprehension, MRC)问答系统(Question Answering, QA) 研究的高质量数据集。该数据集由斯坦福大学的研究人员创建,旨在推动 NLP 领域 基于文本的问答系统 的发展。


1. SQuAD 数据集简介

SQuAD 任务 旨在让机器模型从一个给定的 段落(context) 中找到 答案(answer),并回答相应的问题(question)。数据集包含:

  • 阅读理解文本(Context):来自 Wikipedia 的段落。
  • 问题(Question):基于文本段落提出的问题。
  • 答案(Answer):答案是从段落中抽取的 连续片段(span extraction),即答案的起始和终止位置在文本中明确存在。

SQuAD 主要版本

版本任务类型规模是否包含不可回答问题
SQuAD 1.1机器阅读理解(MRC),答案可在文本中找到100,000+
SQuAD 2.0机器阅读理解(MRC),包含不可回答的问题150,000+

2. SQuAD 1.1 数据集

SQuAD 1.1 是第一个版本,由 Wikipedia 文章的片段和人工标注的问题-答案对组成。每个问题的答案都是 文章中的一个子串(text span),不会出现无法回答的问题。

SQuAD 1.1 数据格式

SQuAD 1.1 的 JSON 文件结构如下:

{
  "data": [
    {
      "title": "Super_Bowl_50",
      "paragraphs": [
        {
          "context": "Super Bowl 50 was an American football game ...",
          "qas": [
            {
              "id": "56be4db0acb8001400a502ec",
              "question": "What was the name of the football game?",
              "answers": [
                {"text": "Super Bowl 50", "answer_start": 0}
              ]
            }
          ]
        }
      ]
    }
  ]
}

SQuAD 1.1 示例

  • Context(文本片段)
    Super Bowl 50 was an American football game that determined the champion of the National Football League (NFL) for the 2015 season.
    
  • Question(问题)
    What was the name of the football game?
    
  • Answer(答案)
    Super Bowl 50
    
    • answer_start: 0 表示答案的起始位置。

3. SQuAD 2.0 数据集

SQuAD 2.0 在 SQuAD 1.1 的基础上增加了 不可回答的问题(unanswerable questions),即:

  • 一些问题在文章中没有答案。
  • 目的是让模型学会识别 何时应该回答,何时应该拒绝回答

SQuAD 2.0 数据格式

与 SQuAD 1.1 类似,但包含不可回答问题:

{
  "context": "Super Bowl 50 was an American football game ...",
  "qas": [
    {
      "id": "56be4db0acb8001400a502ec",
      "question": "What was the name of the football game?",
      "answers": [
        {"text": "Super Bowl 50", "answer_start": 0}
      ]
    },
    {
      "id": "56be4db0acb8001400a502ed",
      "question": "Who won Super Bowl 51?",
      "answers": []
    }
  ]
}

SQuAD 2.0 示例

  • 可回答问题
    • Question: What was the name of the football game?
    • Answer: Super Bowl 50
  • 不可回答问题
    • Question: Who won Super Bowl 51?
    • Answer: No answer

SQuAD 2.0 训练模型不仅要找到正确答案,还要学会 何时不回答


4. SQuAD 任务的挑战

  1. 答案是文本片段

    • 传统 QA 任务可能允许自由生成答案,但 SQuAD 任务的答案必须是文本中的某个 span(片段)
  2. 不可回答问题(SQuAD 2.0):

    • 训练模型拒绝回答错误的问题比回答已知问题更难。
  3. 上下文推理

    • 许多问题需要跨句推理,模型必须能 理解上下文信息 而不仅仅是模式匹配。
  4. 同义词和变体表达

    • 问题答案 可能使用不同的表达方式,比如:
      • Who is the CEO of Tesla?
      • Elon Musk is the chief executive officer of Tesla.

5. SQuAD 的评估指标

SQuAD 使用以下两个指标来评估模型的表现:

  1. 准确匹配(Exact Match, EM)
    • 计算预测的答案是否与真实答案完全匹配(不区分大小写和标点)。
  2. F1 分数(F1 Score)
    • 计算预测答案和真实答案的 词汇重叠度,即 2 × (Precision × Recall) / (Precision + Recall)

6. 如何使用 SQuAD 数据集

6.1 下载 SQuAD 数据

使用 Hugging Face 的 datasets 库:

from datasets import load_dataset

# 加载 SQuAD 2.0 数据集
dataset = load_dataset("squad_v2")

# 访问数据
print(dataset)

6.2 预处理数据

train_data = dataset["train"]
print(train_data[0])

示例输出:

{
  "context": "Super Bowl 50 was an American football game ...",
  "question": "What was the name of the football game?",
  "answers": {"text": ["Super Bowl 50"], "answer_start": [0]}
}

6.3 使用 BERT 训练 SQuAD 任务

SQuAD 任务可以使用 BERT、RoBERTa、ALBERT 等模型进行训练。

from transformers import BertTokenizer, BertForQuestionAnswering
import torch

# 加载预训练的 BERT 模型和 tokenizer
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
model = BertForQuestionAnswering.from_pretrained("bert-base-uncased")

# 示例输入
question = "What was the name of the football game?"
context = "Super Bowl 50 was an American football game that determined the champion of the National Football League (NFL) for the 2015 season."

# 编码输入
inputs = tokenizer(question, context, return_tensors="pt")

# 预测答案
with torch.no_grad():
    outputs = model(**inputs)
    answer_start = torch.argmax(outputs.start_logits)
    answer_end = torch.argmax(outputs.end_logits) + 1

# 解码答案
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][answer_start:answer_end]))
print("Predicted Answer:", answer)

示例输出

Predicted Answer: Super Bowl 50

7. SQuAD 与其他 QA 数据集的对比

数据集任务类型是否包含不可回答问题规模来源
SQuAD 1.1机器阅读理解(MRC)100KWikipedia
SQuAD 2.0机器阅读理解(MRC)150KWikipedia
Natural Questions(NQ)真实问答(Real QA)300KGoogle Search
HotpotQA多跳推理(Multi-Hop QA)100KWikipedia

8. 总结

  • SQuAD最具影响力的问答数据集之一,用于 机器阅读理解(MRC) 任务。
  • SQuAD 1.1 仅包含 可回答问题SQuAD 2.0 增加了 不可回答问题
  • 评估指标包括 Exact Match(EM)F1 Score
  • BERT 和 Transformer 模型在 SQuAD 任务上取得了 突破性进展

SQuAD 是 阅读理解和问答系统研究的基准,推动了 NLP 领域的快速发展。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彬彬侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值