pubmedqa.py 1.61 KB
Newer Older
jeffhsu3's avatar
jeffhsu3 committed
1
import numpy as np
2
from .common import HFTask
&'s avatar
& committed
3
4
from lm_eval.base import rf
from ..metrics import mean
jeffhsu3's avatar
jeffhsu3 committed
5
6
7


class Pubmed_QA(HFTask):
Leo Gao's avatar
Leo Gao committed
8
    VERSION = 0
jeffhsu3's avatar
jeffhsu3 committed
9
10
11
12
13
14
15
16
17
    DATASET_PATH = "pubmed_qa"
    DATASET_NAME = "pqa_labeled"

    def has_training_docs(self):
        return False

    def has_validation_docs(self):
        return False

jeffhsu3's avatar
jeffhsu3 committed
18
19
20
    def has_test_docs(self):
        return True

21
22
23
24
25
    def test_docs(self):
        if self.has_test_docs():
            # HF is labelled as train but its really just for testing
            return self.data["train"]

jeffhsu3's avatar
jeffhsu3 committed
26
    def doc_to_text(self, doc):
jeffhsu3's avatar
jeffhsu3 committed
27
        ctxs = "\n".join(doc["context"]["contexts"])
Leo Gao's avatar
Leo Gao committed
28
        return "Abstract: {}\nQuestion: {}\nAnswer:".format(
jeffhsu3's avatar
jeffhsu3 committed
29
            ctxs,
jeffhsu3's avatar
jeffhsu3 committed
30
31
            doc["question"],
            doc["final_decision"]
jeffhsu3's avatar
jeffhsu3 committed
32
33
34
        )

    def doc_to_target(self, doc):
jeffhsu3's avatar
jeffhsu3 committed
35
        return " {}".format(doc["final_decision"])
jeffhsu3's avatar
jeffhsu3 committed
36
37
38
39
40
41
42
43
44
45
46

    def construct_requests(self, doc, ctx):
        """ Uses RequestFactory to construct Requests and returns
        an iterable of Requests which will be sent to the LM.
        """
        ll_yes, _ = rf.loglikelihood(ctx, " yes")
        ll_no, _ = rf.loglikelihood(ctx, " no")
        ll_maybe, _ = rf.loglikelihood(ctx, " maybe")
        return ll_yes, ll_no, ll_maybe

    def process_results(self, doc, results):
jeffhsu3's avatar
jeffhsu3 committed
47
        gold = doc["final_decision"]
jeffhsu3's avatar
jeffhsu3 committed
48
49
50
        ll_yes, ll_no, ll_maybe = results
        pred = np.argmax(results)
        return {
jeffhsu3's avatar
jeffhsu3 committed
51
            "acc": ["yes", "no", "maybe"][pred] == gold, 
jeffhsu3's avatar
jeffhsu3 committed
52
53
54
55
        }

    def aggregation(self):
        return {
jeffhsu3's avatar
jeffhsu3 committed
56
            "acc" : mean
jeffhsu3's avatar
jeffhsu3 committed
57
58
59
60
        }

    def higher_is_better(self):
        return {
jeffhsu3's avatar
jeffhsu3 committed
61
            "acc" : True
jeffhsu3's avatar
jeffhsu3 committed
62
        }