pubmedqa.py 2 KB
Newer Older
jeffhsu3's avatar
jeffhsu3 committed
1
import numpy as np
jeffhsu3's avatar
jeffhsu3 committed
2
import json
3
4
5
import random
from .common import HFTask 
from lm_eval.base import rf, mean
jeffhsu3's avatar
jeffhsu3 committed
6
7
8
9
10
11
12
13
14
15
16
17


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

    def doc_to_text(self, doc):
jeffhsu3's avatar
jeffhsu3 committed
32
        ctxs = "\n".join(doc["context"]["contexts"])
jeffhsu3's avatar
jeffhsu3 committed
33
34
        return "abstract: {}\nquestion: {}\nanswer:".format(
            ctxs,
jeffhsu3's avatar
jeffhsu3 committed
35
36
            doc["question"],
            doc["final_decision"]
jeffhsu3's avatar
jeffhsu3 committed
37
38
39
        )

    def doc_to_target(self, doc):
jeffhsu3's avatar
jeffhsu3 committed
40
        return " {}".format(doc["final_decision"])
jeffhsu3's avatar
jeffhsu3 committed
41

42
43
44
45
46
47
    def fewshot_examples(self, k):
        # Since only test docs sample from test docs
        if self._training_docs is None:
            self._training_docs = list(self.test_docs())
        return random.sample(self._training_docs, k)

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

    def aggregation(self):
        return {
jeffhsu3's avatar
jeffhsu3 committed
67
            "acc" : mean
jeffhsu3's avatar
jeffhsu3 committed
68
69
70
71
        }

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