superglue.py 1.14 KB
Newer Older
Jason Phang's avatar
Jason Phang committed
1
2
3
4
5
6
from . common import NLP_TASK, simple_accuracy_metric, yesno
from . import TASK_REGISTRY


@TASK_REGISTRY.register("boolq")
class BoolQ(NLP_TASK):
Jason Phang's avatar
Jason Phang committed
7
    NLP_PATH = "super_glue"
Jason Phang's avatar
Jason Phang committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    NLP_NAME = "boolq"

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

    def fewshot_description(self):
        return "Read the following passages and answer each question with a yes or a no."

    def doc_to_text(self, doc, include_target=True):
        return f"{doc['passage']}\nquestion: {doc['question']}\nanswer: " \
Jason Phang's avatar
Jason Phang committed
24
            + (yesno(doc['label']) if include_target else "")
Jason Phang's avatar
Jason Phang committed
25
26

    def evaluate(self, docs, lm, provide_description, num_fewshot):
Jason Phang's avatar
Jason Phang committed
27
        golds = [doc["label"] for doc in docs]
Jason Phang's avatar
Jason Phang committed
28
29
30
31
32
33
34
35
36
        preds = []
        for doc in docs:
            ctx = self.fewshot_context(
                doc=doc,
                provide_description=provide_description,
                num_fewshot=num_fewshot,
            )
            preds.append(lm.loglikelihood(ctx, ' yes') > lm.loglikelihood(ctx, ' no'))
        return simple_accuracy_metric(preds=preds, golds=golds)