pubmedqa.py 1.76 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
8
9
10
11
12
13
14
15
16


class Pubmed_QA(HFTask):
    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
17
18
19
    def has_test_docs(self):
        return True

20
21
22
23
24
    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
25
26
    def fewshot_description(self):
        # Average ctx length in labelled dataset is 238.9
jeffhsu3's avatar
jeffhsu3 committed
27
        # 2 few-shot exmamples pushes it beyond context window
jeffhsu3's avatar
jeffhsu3 committed
28
29
30
        return ""

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

    def doc_to_target(self, doc):
jeffhsu3's avatar
jeffhsu3 committed
39
        return " {}".format(doc["final_decision"])
jeffhsu3's avatar
jeffhsu3 committed
40
41
42
43
44
45
46
47
48
49
50

    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
51
        gold = doc["final_decision"]
jeffhsu3's avatar
jeffhsu3 committed
52
53
54
        ll_yes, ll_no, ll_maybe = results
        pred = np.argmax(results)
        return {
jeffhsu3's avatar
jeffhsu3 committed
55
            "acc": ["yes", "no", "maybe"][pred] == gold, 
jeffhsu3's avatar
jeffhsu3 committed
56
57
58
59
        }

    def aggregation(self):
        return {
jeffhsu3's avatar
jeffhsu3 committed
60
            "acc" : mean
jeffhsu3's avatar
jeffhsu3 committed
61
62
63
64
        }

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