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

Charles Foster's avatar
Charles Foster committed
6
7
8
9
10

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

Jonathan Tow's avatar
Jonathan Tow committed
11
12
13
14
15
16
17
18
19
    @classmethod
    def remove_brackets(cls, text):
        """ Removes brackets from HellaSwag documents. 
        NOTE: The brackets are artifacts of the WikiHow dataset portion underlying
        HellaSwag.
        """
        text = re.sub('\[.*?\]', '', text)
        return text

Charles Foster's avatar
Charles Foster committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    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):
42
43
44
        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
45

46
    def doc_to_text(self, doc):
Jonathan Tow's avatar
Jonathan Tow committed
47
48
        text = doc['activity_label'] + ': ' + doc['ctx'] + '\n'
        return self.remove_brackets(text)
49
50
51
52
53
54
55
56
57
58
59
60

    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:
61
62
            raise ValueError(
                "HellaSwag from HF datasets contained an invalid answer key")
Jonathan Tow's avatar
Jonathan Tow committed
63
64
        target = doc['endings'][index]
        return self.remove_brackets(target)
Charles Foster's avatar
Charles Foster committed
65

Leo Gao's avatar
Leo Gao committed
66
    def construct_requests(self, doc, ctx):
67
        """ Uses RequestFactory to construct Requests and returns an iterable of
Leo Gao's avatar
Leo Gao committed
68
69
70
71
        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
72
            The context string, generated by fewshot_context. This includes the natural
Leo Gao's avatar
Leo Gao committed
73
            language description, as well as the few shot examples, and the question
74
            part of the document for `doc`.
Leo Gao's avatar
Leo Gao committed
75
        """
Jonathan Tow's avatar
Jonathan Tow committed
76
77
78
79
        ll_answers = []
        for i in range(4):
            continuation = self.remove_brackets(doc['endings'][i])
            ll_answers.append(rf.loglikelihood(ctx, continuation))
80
81
        return ll_answers

Leo Gao's avatar
Leo Gao committed
82
    def process_results(self, doc, results):
83
84
        """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
85
86
87
88
89
90
        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.
        """
91
92
93
94
95
96
        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
97
98
99
100

    def aggregation(self):
        """
        :returns: {str: [float] -> float}
101
            A dictionary where keys are the names of submetrics and values are
Leo Gao's avatar
Leo Gao committed
102
103
            functions that aggregate a list of metrics
        """
104
105
106
        return {
            "acc": mean
        }
Leo Gao's avatar
Leo Gao committed
107
108
109
110

    def higher_is_better(self):
        """
        :returns: {str: bool}
111
            A dictionary where keys are the names of submetrics and values are
Leo Gao's avatar
Leo Gao committed
112
113
            whether a higher value of the submetric is better
        """
114
115
116
        return {
            "acc": True
        }