arithmetic.py 4.85 KB
Newer Older
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import abc
import json
import random
from collections import namedtuple
from lm_eval.base import Dataset, mean, rf

ArithmeticDoc = namedtuple('ArithmeticDoc', ['question_text', 'answer_text'])

class Arithmetic(Dataset):
    def __init__(self, number_of_problems=2000):
        super().__init__()
        self.problems = self.generate_problems(number_of_problems)

    @abc.abstractmethod
    def generate_problems(self, number_of_problems):
        pass

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return False

    def training_docs(self):
        return self.problems

    def validation_docs(self):
        return self.generate_problems(50)

    def test_docs(self):
        return NotImplemented
    
    def doc_to_text(self, doc):
        return f"Q: What is {doc.question_text}?\nA: "

    def doc_to_target(self, doc):
        return doc.answer_text

    def construct_requests(self, doc, ctx):
        ll, is_prediction = rf.loglikelihood(ctx, doc.answer_text)
        # not sure what the difference between the two objects returned by rf.loglikehood are here
        return is_prediction

    def process_results(self, doc, results):
        ll, is_prediction = results
        return {
            "acc": is_prediction
        }

    def aggregation(self):
        return {
            "acc": mean,
        }

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


class Arithmetic2DPlus(Arithmetic):
    def generate_problems(self, number_of_problems):
        l = []
        for i in range(number_of_problems):
            x,y = random.randint(0,99), random.randint(0,99)
            a = x+y
            l.append(ArithmeticDoc(question_text=f'{x}+{y}', answer_text=f'{a}'))
        return l

class Arithmetic2DMinus(Arithmetic):
    def generate_problems(self, number_of_problems):
        l = []
        for i in range(number_of_problems):
            x,y = random.randint(0,99), random.randint(0,99)
            a = x-y
            l.append(ArithmeticDoc(question_text=f'{x}-{y}', answer_text=f'{a}'))
        return l

class Arithmetic3DPlus(Arithmetic):
    def generate_problems(self, number_of_problems):
        l = []
        for i in range(number_of_problems):
            x,y = random.randint(0,999), random.randint(0,999)
            a = x+y
            l.append(ArithmeticDoc(question_text=f'{x}+{y}', answer_text=f'{a}'))
        return l

class Arithmetic3DMinus(Arithmetic):
    def generate_problems(self, number_of_problems):
        l = []
        for i in range(number_of_problems):
            x,y = random.randint(0,999), random.randint(0,999)
            a = x-y
            l.append(ArithmeticDoc(question_text=f'{x}-{y}', answer_text=f'{a}'))
        return l

class Arithmetic4DPlus(Arithmetic):
    def generate_problems(self, number_of_problems):
        l = []
        for i in range(number_of_problems):
            x,y = random.randint(0,9999), random.randint(0,9999)
            a = x+y
            l.append(ArithmeticDoc(question_text=f'{x}+{y}', answer_text=f'{a}'))
        return l

class Arithmetic4DMinus(Arithmetic):
    def generate_problems(self, number_of_problems):
        l = []
        for i in range(number_of_problems):
            x,y = random.randint(0,9999), random.randint(0,9999)
            a = x-y
            l.append(ArithmeticDoc(question_text=f'{x}-{y}', answer_text=f'{a}'))
        return l

class Arithmetic5DPlus(Arithmetic):
    def generate_problems(self, number_of_problems):
        l = []
        for i in range(number_of_problems):
            x,y = random.randint(0,99999), random.randint(0,99999)
            a = x+y
            l.append(ArithmeticDoc(question_text=f'{x}+{y}', answer_text=f'{a}'))
        return l

class Arithmetic5DMinus(Arithmetic):
    def generate_problems(self, number_of_problems):
        l = []
        for i in range(number_of_problems):
            x,y = random.randint(0,99999), random.randint(0,99999)
            a = x-y
            l.append(ArithmeticDoc(question_text=f'{x}-{y}', answer_text=f'{a}'))
        return l

class Arithmetic2DMultiplication(Arithmetic):
    def generate_problems(self, number_of_problems):
        l = []
        for i in range(number_of_problems):
            x,y = random.randint(0,99), random.randint(0,99)
            a = x*y
            l.append(ArithmeticDoc(question_text=f'{x}*{y}', answer_text=f'{a}'))
        return l

class Arithmetic1DComposite(Arithmetic):
    def generate_problems(self, number_of_problems):
        l = []
        for i in range(number_of_problems):
            x,y,z = random.randint(0,9), random.randint(0,9), random.randint(0,9)
            op1, op2 = random.choice('-+*'), random.choice('-+*') 
            to_eval = f'{x}{op1}({y}{op2}{z})'
            l.append(ArithmeticDoc(question_text=to_eval, answer_text=str(eval(to_eval))))
        return l