triviaqa.py 2.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
"""
TriviaQA: A Large Scale Distantly Supervised Challenge Dataset for Reading Comprehension
https://arxiv.org/pdf/1705.03551.pdf

TriviaQA is a reading comprehension dataset containing over 650K question-answer-evidence
triples. TriviaQA includes 95K question-answer pairs authored by trivia enthusiasts
and independently gathered evidence documents, six per question on average, that provide
high quality distant supervision for answering the questions.

Homepage: https://nlp.cs.washington.edu/triviaqa/
"""
Jonathan Tow's avatar
Jonathan Tow committed
12
13
import inspect
import lm_eval.datasets.triviaqa.triviaqa
&'s avatar
& committed
14
from lm_eval.base import Task, rf
Jonathan Tow's avatar
Jonathan Tow committed
15
from lm_eval.metrics import mean
Anish Thite's avatar
Anish Thite committed
16

17

18
19
20
21
22
23
24
25
26
27
28
29
30
_CITATION = """
@InProceedings{JoshiTriviaQA2017,
    author = {Joshi, Mandar and Choi, Eunsol and Weld, Daniel S. and Zettlemoyer, Luke},
    title = {TriviaQA: A Large Scale Distantly Supervised Challenge Dataset for Reading Comprehension},
    booktitle = {Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics},
    month = {July},
    year = {2017},
    address = {Vancouver, Canada},
    publisher = {Association for Computational Linguistics},
}
"""


31
class TriviaQA(Task):
bzantium's avatar
bzantium committed
32
    VERSION = 1
Jonathan Tow's avatar
Jonathan Tow committed
33
34
    DATASET_PATH = inspect.getfile(lm_eval.datasets.triviaqa.triviaqa)
    DATASET_NAME = None
Anish Thite's avatar
Anish Thite committed
35
36
37
38
39
40
41
42

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
Leo Gao's avatar
Leo Gao committed
43
        return False
Anish Thite's avatar
Anish Thite committed
44
45

    def training_docs(self):
bzantium's avatar
bzantium committed
46
        return self.dataset["train"]
Anish Thite's avatar
Anish Thite committed
47
48

    def validation_docs(self):
bzantium's avatar
bzantium committed
49
        return self.dataset["validation"]
Anish Thite's avatar
Anish Thite committed
50
51

    def test_docs(self):
52
        raise NotImplementedError()
Jonathan Tow's avatar
Jonathan Tow committed
53

54
    def doc_to_text(self, doc):
Jonathan Tow's avatar
Jonathan Tow committed
55
        return f"Question: {doc['question']}\nAnswer:"
56

bzantium's avatar
bzantium committed
57
58
59
60
61
62
    def should_decontaminate(self):
        return True

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

63
    def doc_to_target(self, doc):
bzantium's avatar
bzantium committed
64
        return " " + doc["answer"]["value"]
Leo Gao's avatar
Leo Gao committed
65
66
67
68
69
70
71
72
73
74

    def _remove_prefixes(self, aliases):
        # Optimization: Remove any alias that has a strict prefix elsewhere in the list
        # we can do this because if the prefix is acceptable by isgreedy, we can stop looking
        aliases.sort()
        ret = [aliases[0]]
        for alias in aliases[1:]:
            if not alias.startswith(ret[-1]):
                ret.append(alias)
        return ret
Anish Thite's avatar
Anish Thite committed
75

Leo Gao's avatar
Leo Gao committed
76
    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Leo Gao committed
77
        ret = []
bzantium's avatar
bzantium committed
78
        for alias in self._remove_prefixes(doc["answer"]["aliases"]):
Leo Gao's avatar
Leo Gao committed
79
80
81
            _, is_prediction = rf.loglikelihood(ctx, " " + alias)
            ret.append(is_prediction)
        return ret
82

Leo Gao's avatar
Leo Gao committed
83
    def process_results(self, doc, results):
bzantium's avatar
bzantium committed
84
        return {"acc": float(any(results))}
Leo Gao's avatar
Leo Gao committed
85
86

    def aggregation(self):
87
88
89
        return {
            "acc": mean,
        }
Leo Gao's avatar
Leo Gao committed
90
91

    def higher_is_better(self):
bzantium's avatar
bzantium committed
92
        return {"acc": True}