hellaswag.py 3.3 KB
Newer Older
Charles Foster's avatar
Charles Foster committed
1
import numpy as np
2
3
4
from ..base import rf, mean
from . common import HFTask

Charles Foster's avatar
Charles Foster committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

class HellaSwag(HFTask):
    DATASET_PATH = "hellaswag"
    DATASET_NAME = None

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

    def training_docs(self):
        if self.has_training_docs():
            return self.data["train"]

    def validation_docs(self):
        if self.has_validation_docs():
            return self.data["validation"]

    def test_docs(self):
        if self.has_test_docs():
            return self.data["test"]

    def fewshot_description(self):
32
33
34
        return "Label for the relevant action: Sentences describing the " \
            "context, with an incomplete sentence trailing\nanswer that " \
            "plausibly completes the situation."
Charles Foster's avatar
Charles Foster committed
35

36
    def doc_to_text(self, doc):
37
        return doc['activity_label'] + ': ' + doc['ctx'] + '\n'
38
39
40
41
42
43
44
45
46
47
48
49

    def doc_to_target(self, doc):
        letter_answer = doc['label']
        if letter_answer == '0':
            index = 0
        elif letter_answer == '1':
            index = 1
        elif letter_answer == '2':
            index = 2
        elif letter_answer == '3':
            index = 3
        else:
50
51
            raise ValueError(
                "HellaSwag from HF datasets contained an invalid answer key")
52
        return doc['endings'][index]
Charles Foster's avatar
Charles Foster committed
53

Leo Gao's avatar
Leo Gao committed
54
    def construct_requests(self, doc, ctx):
55
        """ Uses RequestFactory to construct Requests and returns an iterable of
Leo Gao's avatar
Leo Gao committed
56
        Requests which will be sent to the LM.
57

Leo Gao's avatar
Leo Gao committed
58
59
60
        :param doc:
            The document as returned from training_docs, validation_docs, or test_docs.
        :param ctx: str
61
            The context string, generated by fewshot_context. This includes the natural
Leo Gao's avatar
Leo Gao committed
62
            language description, as well as the few shot examples, and the question
63
            part of the document for `doc`.
Leo Gao's avatar
Leo Gao committed
64
        """
65
66
67
68
69
        ll_answers = [
            rf.loglikelihood(ctx, doc['endings'][i])[0] for i in range(4)
        ]
        return ll_answers

Leo Gao's avatar
Leo Gao committed
70
    def process_results(self, doc, results):
71
72
        """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
Leo Gao's avatar
Leo Gao committed
73
74
75
76
77
78
79
        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.
        """
80
81
82
83
84
85
        gold = int(doc['label'])
        pred = np.argmax(results)
        acc = 1. if pred == gold else 0.
        return {
            "acc": acc
        }
Leo Gao's avatar
Leo Gao committed
86
87
88
89

    def aggregation(self):
        """
        :returns: {str: [float] -> float}
90
            A dictionary where keys are the names of submetrics and values are
Leo Gao's avatar
Leo Gao committed
91
92
            functions that aggregate a list of metrics
        """
93
94
95
        return {
            "acc": mean
        }
Leo Gao's avatar
Leo Gao committed
96
97
98
99

    def higher_is_better(self):
        """
        :returns: {str: bool}
100
            A dictionary where keys are the names of submetrics and values are
Leo Gao's avatar
Leo Gao committed
101
102
            whether a higher value of the submetric is better
        """
103
104
105
        return {
            "acc": True
        }