khulnasoft commited on
Commit
7c92466
·
verified ·
1 Parent(s): 76856e0

Create math_dataset.py

Browse files
Files changed (1) hide show
  1. math_dataset.py +275 -0
math_dataset.py ADDED
@@ -0,0 +1,275 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # Lint as: python3
17
+ """Mathematics database."""
18
+
19
+ import datasets
20
+
21
+
22
+ logger = datasets.logging.get_logger(__name__)
23
+
24
+
25
+ _CITATION = """
26
+ @article{2019arXiv,
27
+ author = {Saxton, Grefenstette, Hill, Kohli},
28
+ title = {Analysing Mathematical Reasoning Abilities of Neural Models},
29
+ year = {2019},
30
+ journal = {arXiv:1904.01557}
31
+ }
32
+ """
33
+
34
+ _DESCRIPTION = """
35
+ Mathematics database.
36
+
37
+ This dataset code generates mathematical question and answer pairs,
38
+ from a range of question types at roughly school-level difficulty.
39
+ This is designed to test the mathematical learning and algebraic
40
+ reasoning skills of learning models.
41
+
42
+ Original paper: Analysing Mathematical Reasoning Abilities of Neural Models
43
+ (Saxton, Grefenstette, Hill, Kohli).
44
+
45
+ Example usage:
46
+ train_examples, val_examples = datasets.load_dataset(
47
+ 'math_dataset/arithmetic__mul',
48
+ split=['train', 'test'],
49
+ as_supervised=True)
50
+ """
51
+
52
+ _DATA_URL = "https://storage.googleapis.com/mathematics-dataset/mathematics_dataset-v1.0.tar.gz"
53
+
54
+ _TRAIN_CATEGORY = [
55
+ "train-easy",
56
+ "train-medium",
57
+ "train-hard",
58
+ ]
59
+
60
+ _INTERPOLATE_CATEGORY = [
61
+ "interpolate",
62
+ ]
63
+
64
+ _MODULES = [
65
+ # extrapolate
66
+ "measurement__conversion",
67
+ # interpolate
68
+ "algebra__linear_1d",
69
+ "algebra__linear_1d_composed",
70
+ "algebra__linear_2d",
71
+ "algebra__linear_2d_composed",
72
+ "algebra__polynomial_roots",
73
+ "algebra__polynomial_roots_composed",
74
+ "algebra__sequence_next_term",
75
+ "algebra__sequence_nth_term",
76
+ "arithmetic__add_or_sub",
77
+ "arithmetic__add_or_sub_in_base",
78
+ "arithmetic__add_sub_multiple",
79
+ "arithmetic__div",
80
+ "arithmetic__mixed",
81
+ "arithmetic__mul",
82
+ "arithmetic__mul_div_multiple",
83
+ "arithmetic__nearest_integer_root",
84
+ "arithmetic__simplify_surd",
85
+ "calculus__differentiate",
86
+ "calculus__differentiate_composed",
87
+ "comparison__closest",
88
+ "comparison__closest_composed",
89
+ "comparison__kth_biggest",
90
+ "comparison__kth_biggest_composed",
91
+ "comparison__pair",
92
+ "comparison__pair_composed",
93
+ "comparison__sort",
94
+ "comparison__sort_composed",
95
+ "measurement__conversion",
96
+ "measurement__time",
97
+ "numbers__base_conversion",
98
+ "numbers__div_remainder",
99
+ "numbers__div_remainder_composed",
100
+ "numbers__gcd",
101
+ "numbers__gcd_composed",
102
+ "numbers__is_factor",
103
+ "numbers__is_factor_composed",
104
+ "numbers__is_prime",
105
+ "numbers__is_prime_composed",
106
+ "numbers__lcm",
107
+ "numbers__lcm_composed",
108
+ "numbers__list_prime_factors",
109
+ "numbers__list_prime_factors_composed",
110
+ "numbers__place_value",
111
+ "numbers__place_value_composed",
112
+ "numbers__round_number",
113
+ "numbers__round_number_composed",
114
+ "polynomials__add",
115
+ "polynomials__coefficient_named",
116
+ "polynomials__collect",
117
+ "polynomials__compose",
118
+ "polynomials__evaluate",
119
+ "polynomials__evaluate_composed",
120
+ "polynomials__expand",
121
+ "polynomials__simplify_power",
122
+ "probability__swr_p_level_set",
123
+ "probability__swr_p_sequence",
124
+ # train-easy train-medium train-hard
125
+ "algebra__linear_1d",
126
+ "algebra__linear_1d_composed",
127
+ "algebra__linear_2d",
128
+ "algebra__linear_2d_composed",
129
+ "algebra__polynomial_roots",
130
+ "algebra__polynomial_roots_composed",
131
+ "algebra__sequence_next_term",
132
+ "algebra__sequence_nth_term",
133
+ "arithmetic__add_or_sub",
134
+ "arithmetic__add_or_sub_in_base",
135
+ "arithmetic__add_sub_multiple",
136
+ "arithmetic__div",
137
+ "arithmetic__mixed",
138
+ "arithmetic__mul",
139
+ "arithmetic__mul_div_multiple",
140
+ "arithmetic__nearest_integer_root",
141
+ "arithmetic__simplify_surd",
142
+ "calculus__differentiate",
143
+ "calculus__differentiate_composed",
144
+ "comparison__closest",
145
+ "comparison__closest_composed",
146
+ "comparison__kth_biggest",
147
+ "comparison__kth_biggest_composed",
148
+ "comparison__pair",
149
+ "comparison__pair_composed",
150
+ "comparison__sort",
151
+ "comparison__sort_composed",
152
+ "measurement__conversion",
153
+ "measurement__time",
154
+ "numbers__base_conversion",
155
+ "numbers__div_remainder",
156
+ "numbers__div_remainder_composed",
157
+ "numbers__gcd",
158
+ "numbers__gcd_composed",
159
+ "numbers__is_factor",
160
+ "numbers__is_factor_composed",
161
+ "numbers__is_prime",
162
+ "numbers__is_prime_composed",
163
+ "numbers__lcm",
164
+ "numbers__lcm_composed",
165
+ "numbers__list_prime_factors",
166
+ "numbers__list_prime_factors_composed",
167
+ "numbers__place_value",
168
+ "numbers__place_value_composed",
169
+ "numbers__round_number",
170
+ "numbers__round_number_composed",
171
+ "polynomials__add",
172
+ "polynomials__coefficient_named",
173
+ "polynomials__collect",
174
+ "polynomials__compose",
175
+ "polynomials__evaluate",
176
+ "polynomials__evaluate_composed",
177
+ "polynomials__expand",
178
+ "polynomials__simplify_power",
179
+ "probability__swr_p_level_set",
180
+ "probability__swr_p_sequence",
181
+ ]
182
+
183
+ _QUESTION = "question"
184
+ _ANSWER = "answer"
185
+
186
+ _DATASET_VERSION = "mathematics_dataset-v1.0"
187
+
188
+
189
+ def _generate_builder_configs():
190
+ """Generate configs with different subsets of mathematics dataset."""
191
+ configs = []
192
+ for module in sorted(set(_MODULES)):
193
+ configs.append(
194
+ datasets.BuilderConfig(
195
+ name=module,
196
+ version=datasets.Version("1.0.0"),
197
+ description=_DESCRIPTION,
198
+ )
199
+ )
200
+
201
+ return configs
202
+
203
+
204
+ class MathDataset(datasets.GeneratorBasedBuilder):
205
+ """Math Dataset."""
206
+
207
+ BUILDER_CONFIGS = _generate_builder_configs()
208
+
209
+ def _info(self):
210
+ return datasets.DatasetInfo(
211
+ description=_DESCRIPTION,
212
+ features=datasets.Features(
213
+ {
214
+ _QUESTION: datasets.Value("string"),
215
+ _ANSWER: datasets.Value("string"),
216
+ }
217
+ ),
218
+ supervised_keys=(_QUESTION, _ANSWER),
219
+ homepage="https://github.com/deepmind/mathematics_dataset",
220
+ citation=_CITATION,
221
+ )
222
+
223
+ def _get_filepaths_from_categories(self, config, categories):
224
+ filepaths = []
225
+ for category in categories:
226
+ data_file = "/".join([_DATASET_VERSION, category, config])
227
+ filepaths.append(data_file)
228
+ return set(filepaths)
229
+
230
+ def _split_generators(self, dl_manager):
231
+ """Returns SplitGenerators."""
232
+
233
+ archive = dl_manager.download(_DATA_URL)
234
+ config = self.config.name + ".txt"
235
+
236
+ return [
237
+ datasets.SplitGenerator(
238
+ name=datasets.Split.TRAIN,
239
+ gen_kwargs={
240
+ "files": dl_manager.iter_archive(archive),
241
+ "config": config,
242
+ "categories": _TRAIN_CATEGORY,
243
+ },
244
+ ),
245
+ datasets.SplitGenerator(
246
+ name=datasets.Split.TEST,
247
+ gen_kwargs={
248
+ "files": dl_manager.iter_archive(archive),
249
+ "config": config,
250
+ "categories": _INTERPOLATE_CATEGORY,
251
+ },
252
+ ),
253
+ ]
254
+
255
+ def _generate_examples(self, files, config, categories):
256
+ """Yields examples based on directory, module file.."""
257
+
258
+ idx = 0
259
+ filepaths = self._get_filepaths_from_categories(config, categories)
260
+ for path, f in files:
261
+ if not filepaths:
262
+ break
263
+ elif path in filepaths:
264
+ for question in f:
265
+ if not question:
266
+ continue
267
+ else:
268
+ for answer in f:
269
+ if not answer:
270
+ continue
271
+ else:
272
+ yield idx, {_QUESTION: question, _ANSWER: answer}
273
+ idx += 1
274
+ break
275
+ filepaths.remove(path)