nqopen.py 5.23 KB
Newer Older
Björn Bebensee's avatar
Björn Bebensee committed
1
"""
Björn Bebensee's avatar
Björn Bebensee committed
2
3
4
Latent Retrieval for Weakly Supervised Open Domain Question Answering
https://arxiv.org/pdf/1906.00300.pdf

Björn Bebensee's avatar
Björn Bebensee committed
5
6
7
Natural Questions: a Benchmark for Question Answering Research
https://storage.googleapis.com/pub-tools-public-publication-data/pdf/1f7b46b5378d757553d3e92ead36bda2e4254244.pdf

Björn Bebensee's avatar
Björn Bebensee committed
8
9
10
11
The NQ-Open task, introduced by Lee et. al. 2019, is an open-domain question
answering benchmark that is derived from Natural Questions. The goal is to predict
an English answer string for an input English question. All questions can be
answered using the contents of English Wikipedia.
Björn Bebensee's avatar
Björn Bebensee committed
12

Björn Bebensee's avatar
Björn Bebensee committed
13
Homepage: https://github.com/google-research-datasets/natural-questions/tree/master/nq_open
Björn Bebensee's avatar
Björn Bebensee committed
14
"""
15
import regex
Björn Bebensee's avatar
Björn Bebensee committed
16
17
18
19
20
import string
from lm_eval.base import Task, rf
from lm_eval.metrics import mean

_CITATION = """
Björn Bebensee's avatar
Björn Bebensee committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@inproceedings{lee-etal-2019-latent,
    title = "Latent Retrieval for Weakly Supervised Open Domain Question Answering",
    author = "Lee, Kenton  and
      Chang, Ming-Wei  and
      Toutanova, Kristina",
    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-1612",
    doi = "10.18653/v1/P19-1612",
    pages = "6086--6096",
    abstract = "Recent work on open domain question answering (QA) assumes strong supervision of the supporting evidence and/or assumes a blackbox information retrieval (IR) system to retrieve evidence candidates. We argue that both are suboptimal, since gold evidence is not always available, and QA is fundamentally different from IR. We show for the first time that it is possible to jointly learn the retriever and reader from question-answer string pairs and without any IR system. In this setting, evidence retrieval from all of Wikipedia is treated as a latent variable. Since this is impractical to learn from scratch, we pre-train the retriever with an Inverse Cloze Task. We evaluate on open versions of five QA datasets. On datasets where the questioner already knows the answer, a traditional IR system such as BM25 is sufficient. On datasets where a user is genuinely seeking an answer, we show that learned retrieval is crucial, outperforming BM25 by up to 19 points in exact match.",
Björn Bebensee's avatar
Björn Bebensee committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
}
"""


class NQOpen(Task):
    VERSION = 0
    DATASET_PATH = "nq_open"
    DATASET_NAME = None

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return False

    def training_docs(self):
        return self.dataset["train"]

    def validation_docs(self):
        return self.dataset["validation"]

    def test_docs(self):
        raise NotImplementedError()
jonabur's avatar
jonabur committed
61

Björn Bebensee's avatar
Björn Bebensee committed
62
    def doc_to_text(self, doc):
63
        return f"Q: {doc['question']}\nA:"
Björn Bebensee's avatar
Björn Bebensee committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

    def should_decontaminate(self):
        return True

    def doc_to_decontamination_query(self, doc):
        return doc["question"]

    def doc_to_target(self, doc):
        return " " + doc["answer"][0]

    def construct_requests(self, doc, ctx):
        """Uses RequestFactory to construct Requests and returns an iterable of
        Requests which will be sent to the LM.
        :param doc:
                The document as returned from training_docs, validation_docs, or test_docs.
        :param ctx: str
                The context string, generated by fewshot_context. This includes the natural
                language description, as well as the few shot examples, and the question
                part of the document for `doc`.
        """
        continuation = rf.greedy_until(ctx, {"until": ["\n", ".", ","]})
        return continuation

87
88
    def _normalize_answer(self, text):
        # Lowercase and remove punctuation, strip whitespace
jonabur's avatar
jonabur committed
89
        text = text.strip().lower().translate(str.maketrans("", "", string.punctuation))
90
91

        # Remove articles, resulting in duplicate whitespace
jonabur's avatar
jonabur committed
92
        text = regex.sub(r"\b(a|an|the)\b", " ", text)
93
94
95
96
97
98

        # Remove duplicate whitespace
        text = " ".join(text.split())

        return text

Björn Bebensee's avatar
Björn Bebensee committed
99
100
101
102
103
104
105
106
107
108
    def process_results(self, doc, results):
        """Take a single document and the LM results and evaluates, returning a
        dict where keys are the names of submetrics and values are the values of
        the metric for that one document

        :param doc:
            The document as returned from training_docs, validation_docs, or test_docs.
        :param results:
            The results of the requests created in construct_requests.
        """
109
110
        continuation = self._normalize_answer(results[0])
        answers = [self._normalize_answer(answer) for answer in doc["answer"]]
jonabur's avatar
jonabur committed
111
112

        return {"em": float(continuation in answers)}
Björn Bebensee's avatar
Björn Bebensee committed
113
114
115
116
117
118
119
120
121

    def aggregation(self):
        """
        :returns: {str: [float] -> float}
            A dictionary where keys are the names of submetrics and values are
            functions that aggregate a list of metrics
        """
        return {
            "em": mean,
jonabur's avatar
jonabur committed
122
123
        }

Björn Bebensee's avatar
Björn Bebensee committed
124
125
126
127
128
129
130
131
    def higher_is_better(self):
        """
        :returns: {str: bool}
            A dictionary where keys are the names of submetrics and values are
            whether a higher value of the submetric is better
        """
        return {
            "em": True,
jonabur's avatar
jonabur committed
132
        }