-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_generator.py
153 lines (104 loc) · 5.38 KB
/
dataset_generator.py
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 json
import os.path as osp
import random
from typing import Union
import fire
def addition(volume):
# Addition up to 16 digits
pairs = \
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(1,10) for j in range(i,10) for k in range(volume)] +\
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(3,10) for j in range(i,10) for k in range(volume)] +\
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(6,10) for j in range(i,10) for k in range(volume)] +\
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(9,10) for j in range(i,10) for k in range(volume)] #+\
#[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(12,20) for j in range(i,20) for k in range(volume)] +\
#[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(15,20) for j in range(i,20) for k in range(volume)] +\
#[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(18,20) for j in range(i,20) for k in range(volume)]
random.shuffle(pairs)
print("Addition:", len(pairs))
data_add = []
for num1, num2 in pairs:
if random.random()<0.5:
num1, num2 = num2, num1
answer = num1 + num2
question = f"{num1} + {num2}"
output = f"{num1} + {num2} = {answer}"
assert(output.split()[-1] == str(answer))
data_add.append({"input": question, "output": output, "answer": str(answer)})
return data_add
def subtraction(volume):
pairs = \
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(1,20) for j in range(i,20) for k in range(volume)] +\
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(3,20) for j in range(i,20) for k in range(volume)] +\
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(6,20) for j in range(i,20) for k in range(volume)] +\
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(9,20) for j in range(i,20) for k in range(volume)] +\
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(12,20) for j in range(i,20) for k in range(volume)] +\
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(15,20) for j in range(i,20) for k in range(volume)] +\
[(random.randint(10**(i-1), 10**i), random.randint(10**(j-1), 10**j)) for i in range(18,20) for j in range(i,20) for k in range(volume)]
random.shuffle(pairs)
print("Subtraction:", len(pairs))
data_sub = []
for num1, num2 in pairs:
if random.random()<0.5:
num1, num2 = num2, num1
answer = num1 - num2
question = f"{num1} - {num2}"
output = f"{num1} - {num2} = {answer}"
assert(output.split()[-1] == str(answer))
data_sub.append({"input": question, "output": output, "answer": str(answer)})
return data_sub
def main(
dataset_name = "dataset.json",
data_path = "./data/",
need_add = True,
need_sub = True,
add_volume = 100,
sub_volume = 100,
):
data_add, data_sub = [], []
if need_add:
data_add = addition(add_volume)
if need_sub:
data_sub = subtraction(sub_volume)
# dump arithmetic data into json
template_name = "./templates/goat.json"
with open(data_path + dataset_name, "w") as f:
json.dump(data_add + data_sub, f, indent=4)
print("Total:", len(data_add + data_sub))
print("Adding instructions and noise")
### Add natural language instruction to the generated arithmetic data using template
with open(template_name) as fp:
template = json.load(fp)
with open(data_path + dataset_name, "rb") as test_file:
data_original = json.load(test_file)
data_converted = []
for instance in data_original:
arithmetic = instance["input"]
output_dict = {}
# add noise to instruction so that the model is robust to diverse question formats
if random.random() < 0.05:
if " + " in arithmetic:
arithmetic = "the sum of " + arithmetic.replace("+", "and")
if " - " in arithmetic:
arithmetic = "the difference of " + arithmetic.replace("-", "and")
if random.random() < 0.5:
arithmetic = arithmetic.replace("*", "x")
if random.random() < 0.1:
arithmetic = arithmetic.replace("+", "plus").replace("-", "minus")
if random.random() < 0.5:
if "+" in arithmetic or "-" in arithmetic:
arithmetic = arithmetic.replace(" ", "")
num = random.randint(1,500)
instruction = template[str(num)].format(
input = arithmetic
)
output_dict["instruction"] = instruction
output_dict["input"] = instance["input"]
output_dict["output"] = instance["output"]
output_dict["answer"] = instance["answer"]
data_converted.append(output_dict)
print("Total:", len(data_converted))
with open(data_path + dataset_name, "w") as f:
json.dump(data_converted, f, indent=4)
print("Dataset generated!")
if __name__ == "__main__":
fire.Fire(main)