triviaqa.py 1.77 KB
Newer Older
1
import os
Anish Thite's avatar
Anish Thite committed
2
3
import json
import random
4
from lm_eval.base import Dataset, mean, rf
Anish Thite's avatar
Anish Thite committed
5
6
7
8
from ..utils import sh

class TriviaQA(Dataset):
    def download(self):
9
10
11
12
13
14
15
        if not os.path.exists('data/triviaqa'):
            sh("""
            mkdir -p data/triviaqa
            wget http://nlp.cs.washington.edu/triviaqa/data/triviaqa-unfiltered.tar.gz -O data/triviaqa/trivia_qa-unfiltered.tar.gz
            tar -xf data/triviaqa/trivia_qa-unfiltered.tar.gz
            mv triviaqa-unfiltered/ data/triviaqa/
            """)
Anish Thite's avatar
Anish Thite committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

    def training_docs(self):
        return json.load(open('data/triviaqa/triviaqa-unfiltered/unfiltered-web-train.json'))['Data']

    def validation_docs(self):
        return  json.load(open('data/triviaqa/triviaqa-unfiltered/unfiltered-web-dev.json'))['Data']

    def test_docs(self):
        return  json.load(open('data/triviaqa/triviaqa-unfiltered/unfiltered-web-test.json'))['Data']     
    
    def fewshot_description(self):
Leo Gao's avatar
Leo Gao committed
36
37
        # TODO: figure out fewshot description
        return ""
Anish Thite's avatar
Anish Thite committed
38
    
39
    def doc_to_text(self, doc):
40
        return ''.join(['Q:', doc['Question'], '\n\n','A:'])
41
42
43

    def doc_to_target(self, doc):
        return doc['Answer']['Aliases'][0]
Anish Thite's avatar
Anish Thite committed
44

Leo Gao's avatar
Leo Gao committed
45
    def construct_requests(self, doc, ctx):
46
47
        ll, is_prediction = rf.loglikelihood(ctx,doc['Answer']['Value'])
        return is_prediction
48

Leo Gao's avatar
Leo Gao committed
49
    def process_results(self, doc, results):
50
51
52
53
        is_prediction = results
        return {
            "acc": float(is_prediction[1])
        }
Leo Gao's avatar
Leo Gao committed
54
55

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

    def higher_is_better(self):
61
62
63
        return {
            "acc": True
        }