webqs.py 1.74 KB
Newer Older
1
from . common import HFTask
&'s avatar
& committed
2
3
4
from lm_eval.base import rf
from ..metrics import mean

Leo Gao's avatar
Leo Gao committed
5

6
class WebQs(HFTask):
Leo Gao's avatar
Leo Gao committed
7
    VERSION = 0
Leo Gao's avatar
Leo Gao committed
8
9
    DATASET_PATH = "web_questions"
    DATASET_NAME = None
Leo Gao's avatar
Leo Gao committed
10
11
12
13
14
15
16
17
18
19

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return False

    def has_test_docs(self):
        return True

20
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
21
        return "Question: " + doc['question'] + '\nAnswer:'
Leo Gao's avatar
Leo Gao committed
22

23
24
25
26
27
28
    def should_decontaminate(self):
        return True

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

29
    def doc_to_target(self, doc):
Leo Gao's avatar
Leo Gao committed
30
31
32
        # this picks one answer to be the "correct" one, despite sometimes 
        # multiple correct answers being possible.
        # TODO: make sure we're actually handling multi-answer correctly
33
        return " " + doc['answers'][0]
34
35
36
37
38
39
40
41
42
43
44
        
    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
Leo Gao's avatar
Leo Gao committed
45

Leo Gao's avatar
Leo Gao committed
46
    def construct_requests(self, doc, ctx):
47
48
49
50
51
        ret = []
        for alias in self._remove_prefixes(doc['answers']):
            _, is_prediction = rf.loglikelihood(ctx, " " + alias)
            ret.append(is_prediction)
        return ret
52

Leo Gao's avatar
Leo Gao committed
53
    def process_results(self, doc, results):
54
55
56
        return {
            "acc": float(any(results))
        }
Leo Gao's avatar
Leo Gao committed
57
58

    def aggregation(self):
59
60
61
        return {
            "acc": mean,
        }
Leo Gao's avatar
Leo Gao committed
62
63

    def higher_is_better(self):
64
65
        return {
            "acc": True
66
        }