adversarial_hotpotqa / adversarial_hotpotqa.py
sagnikrayc's picture
chore: add datasets
8c21775
# coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
"""HotpotQA: A Dataset for Diverse, Explainable Multi-hop Question Answering."""
import json
import datasets
_CITATION = """
@inproceedings{jiang-bansal-2019-avoiding,
title = "Avoiding Reasoning Shortcuts: Adversarial Evaluation, Training, and Model Development for Multi-Hop {QA}",
author = "Jiang, Yichen and
Bansal, Mohit",
booktitle = "Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics",
month = jul,
year = "2019",
address = "Florence, Italy",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/P19-1262",
doi = "10.18653/v1/P19-1262",
pages = "2726--2736",
abstract = "Multi-hop question answering requires a model to connect multiple pieces of evidence scattered in a long context to answer the question. In this paper, we show that in the multi-hop HotpotQA (Yang et al., 2018) dataset, the examples often contain reasoning shortcuts through which models can directly locate the answer by word-matching the question with a sentence in the context. We demonstrate this issue by constructing adversarial documents that create contradicting answers to the shortcut but do not affect the validity of the original answer. The performance of strong baseline models drops significantly on our adversarial test, indicating that they are indeed exploiting the shortcuts rather than performing multi-hop reasoning. After adversarial training, the baseline{'}s performance improves but is still limited on the adversarial test. Hence, we use a control unit that dynamically attends to the question at different reasoning hops to guide the model{'}s multi-hop reasoning. We show that our 2-hop model trained on the regular data is more robust to the adversaries than the baseline. After adversarial training, it not only achieves significant improvements over its counterpart trained on regular data, but also outperforms the adversarially-trained baseline significantly. Finally, we sanity-check that these improvements are not obtained by exploiting potential new shortcuts in the adversarial data, but indeed due to robust multi-hop reasoning skills of the models.",
}
"""
_DESCRIPTION = """\
This dataset is from the paper: "Avoiding Reasoning Shortcuts: Adversarial Evaluation, Training, and Model Development for
Multi-Hop QA" by Yichen Jiang and Mohit Bansal.
The dataset was created using the code provided in the repo: https://github.com/jiangycTarheel-zz/Adversarial-MultiHopQA.
"""
TRAIN_JSON = "https://huggingface.co/datasets/sagnikrayc/adversarial_hotpotqa/resolve/main/train.json"
VALID_JSON = "https://huggingface.co/datasets/sagnikrayc/adversarial_hotpotqa/resolve/main/validation.json"
class AdvHotpotQA(datasets.GeneratorBasedBuilder):
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"_id": datasets.Value("string"),
"question": datasets.Value("string"),
"answer": datasets.Value("string"),
"type": datasets.Value("string"),
"level": datasets.Value("string"),
"supporting_facts": datasets.features.Sequence(
{
"title": datasets.Value("string"),
"sent_id": datasets.Value("int32"),
}
),
"context": datasets.features.Sequence(
{
"title": datasets.Value("string"),
"sentences": datasets.features.Sequence(datasets.Value("string")),
}
),
}
),
supervised_keys=None,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
paths = {
datasets.Split.TRAIN: TRAIN_JSON,
datasets.Split.VALIDATION: VALID_JSON,
}
files = dl_manager.download(paths)
split_generators = []
for split in files:
split_generators.append(datasets.SplitGenerator(name=split, gen_kwargs={"data_file": files[split]}))
return split_generators
def _generate_examples(self, data_file):
"""This function returns the examples."""
data = json.load(open(data_file))
for idx, example in enumerate(data):
# Test set has missing keys
for k in ["answer", "type", "level"]:
if k not in example.keys():
example[k] = None
if "supporting_facts" not in example.keys():
example["supporting_facts"] = []
yield idx, {
"_id": example["_id"],
"question": example["question"],
"answer": example["answer"],
"type": example["type"],
"level": example["level"],
"supporting_facts": [{"title": f[0], "sent_id": f[1]} for f in example["supporting_facts"]],
"context": [{"title": f[0], "sentences": f[1]} for f in example["context"]],
}