hf.py 1.25 KB
Newer Older
Leo Gao's avatar
Leo Gao committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import base
import nlp

def yesno(x):
    if x: return 'yes'
    else: return 'no'


def mean(x):
    return sum(x) / len(x)


class BoolQ(base.Dataset):
    def __init__(self):
        self.dataset = nlp.load_dataset('boolq')

    def training_docs(self):
        yield from self.dataset['train']
    
    def validation_docs(self):
        yield from self.dataset['validation']
    
    def test_docs(self):
        return []
    
Jason Phang's avatar
Jason Phang committed
26
    def fewshot_prefix(self):
Leo Gao's avatar
Leo Gao committed
27
        return "Read the following passages and answer each question with a yes or a no."
Leo Gao's avatar
Leo Gao committed
28
29
30
31
32
33
34

    def doc_to_text(self, doc, include_target=True):    
        return f"{doc['passage']}\nquestion: {doc['question']}\nanswer: " + (yesno(doc['answer']) if include_target else "")
    
    def evaluate(self, docs, lm, provide_description, num_fewshot):
        acc = []
        for doc in docs:
Leo Gao's avatar
Leo Gao committed
35
36
37
            ctx = '\n\n'.join(map(self.doc_to_text, self.fewshot_examples(k=num_fewshot))) + '\n\n'
            ctx += self.doc_to_text(doc, include_target=False).strip()
            ctx = ((self.fewshot_description() + "\n\n") if provide_description else "") + ctx
Leo Gao's avatar
Leo Gao committed
38
39
40
41
42
43

            ans = lm.loglikelihood(ctx, 'yes') > lm.loglikelihood(ctx, 'no')

            acc.append(int(ans == doc['answer']))
    
        return mean(acc)