Datasets:

Modalities:
Text
ArXiv:
Libraries:
Datasets
bfattori commited on
Commit
c63e278
·
1 Parent(s): 141e413

Create arithmetic.py

Browse files
Files changed (1) hide show
  1. arithmetic.py +156 -0
arithmetic.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """GPT-3 Arithmetic Test Dataset."""
15
+
16
+
17
+ import json
18
+
19
+ import datasets
20
+
21
+
22
+ _CITATION = """\
23
+ @inproceedings{NEURIPS2020_1457c0d6,
24
+ author = {Brown, Tom and Mann, Benjamin and Ryder, Nick and Subbiah, Melanie and Kaplan, Jared D and Dhariwal, Prafulla and Neelakantan, Arvind and Shyam, Pranav and Sastry, Girish and Askell, Amanda and Agarwal, Sandhini and Herbert-Voss, Ariel and Krueger, Gretchen and Henighan, Tom and Child, Rewon and Ramesh, Aditya and Ziegler, Daniel and Wu, Jeffrey and Winter, Clemens and Hesse, Chris and Chen, Mark and Sigler, Eric and Litwin, Mateusz and Gray, Scott and Chess, Benjamin and Clark, Jack and Berner, Christopher and McCandlish, Sam and Radford, Alec and Sutskever, Ilya and Amodei, Dario},
25
+ booktitle = {Advances in Neural Information Processing Systems},
26
+ editor = {H. Larochelle and M. Ranzato and R. Hadsell and M. F. Balcan and H. Lin},
27
+ pages = {1877--1901},
28
+ publisher = {Curran Associates, Inc.},
29
+ title = {Language Models are Few-Shot Learners},
30
+ url = {https://proceedings.neurips.cc/paper/2020/file/1457c0d6bfcb4967418bfb8ac142f64a-Paper.pdf},
31
+ volume = {33},
32
+ year = {2020}
33
+ }
34
+ """
35
+
36
+ _DESCRIPTION = """\
37
+ A small battery of 10 tests that involve asking language models a simple arithmetic
38
+ problem in natural language.
39
+ """
40
+
41
+ _HOMEPAGE = "https://github.com/openai/gpt-3/tree/master/data"
42
+
43
+ # TODO: Add the licence for the dataset here if you can find it
44
+ _LICENSE = ""
45
+
46
+ _BASE_URL = "https://huggingface.co/datasets/bfattori/arithmetic/resolve/main/data"
47
+
48
+
49
+ _URLS = {
50
+ "arithmetic_2da": f"{_BASE_URL}/two_digit_addition.jsonl",
51
+ "arithmetic_2ds": f"{_BASE_URL}/two_digit_subtraction.jsonl",
52
+ "arithmetic_3da": f"{_BASE_URL}/three_digit_addition.jsonl",
53
+ "arithmetic_3ds": f"{_BASE_URL}/three_digit_subtraction.jsonl",
54
+ "arithmetic_4da": f"{_BASE_URL}/four_digit_addition.jsonl",
55
+ "arithmetic_4ds": f"{_BASE_URL}/four_digit_subtraction.jsonl",
56
+ "arithmetic_5da": f"{_BASE_URL}/five_digit_addition.jsonl",
57
+ "arithmetic_5ds": f"{_BASE_URL}/five_digit_subtraction.jsonl",
58
+ "arithmetic_2dm": f"{_BASE_URL}/two_digit_multiplication.jsonl",
59
+ "arithmetic_1dc": f"{_BASE_URL}/single_digit_three_ops.jsonl"
60
+ }
61
+
62
+ class Arithmetic(datasets.GeneratorBasedBuilder):
63
+ """A small battery of 10 tests involving simple arithmetic problems."""
64
+
65
+ VERSION = datasets.Version("0.0.1")
66
+
67
+ BUILDER_CONFIGS = [
68
+ datasets.BuilderConfig(
69
+ name = "arithmetic_2da",
70
+ version = VERSION,
71
+ description = "2-digit addition"
72
+ ),
73
+ datasets.BuilderConfig(
74
+ name = "arithmetic_2ds",
75
+ version = VERSION,
76
+ description = "2-digit subtraction"
77
+ ),
78
+ datasets.BuilderConfig(
79
+ name = "arithmetic_3da",
80
+ version = VERSION,
81
+ description = "3-digit addition"
82
+ ),
83
+ datasets.BuilderConfig(
84
+ name = "arithmetic_3ds",
85
+ version = VERSION,
86
+ description = "3-digit subtraction"
87
+ ),
88
+ datasets.BuilderConfig(
89
+ name = "arithmetic_4da",
90
+ version = VERSION,
91
+ description = "4-digit addition"
92
+ ),
93
+ datasets.BuilderConfig(
94
+ name = "arithmetic_4ds",
95
+ version = VERSION,
96
+ description = "4-digit subtraction"
97
+ ),
98
+ datasets.BuilderConfig(
99
+ name = "arithmetic_5da",
100
+ version = VERSION,
101
+ description = "5-digit addition"
102
+ ),
103
+ datasets.BuilderConfig(
104
+ name = "arithmetic_5ds",
105
+ version = VERSION,
106
+ description = "5-digit subtraction"
107
+ ),
108
+ datasets.BuilderConfig(
109
+ name = "arithmetic_2dm",
110
+ version = VERSION,
111
+ description = "2-digit multiplication"
112
+ ),
113
+ datasets.BuilderConfig(
114
+ name = "arithmetic_1dc",
115
+ version = VERSION,
116
+ description = "Single digit 3 operations"
117
+ )
118
+ ]
119
+
120
+ def _info(self):
121
+ return datasets.DatasetInfo(
122
+ description=f"{_DESCRIPTION}\n{self.config.description}",
123
+ features=self.config.features,
124
+ homepage=_HOMEPAGE,
125
+ license=_LICENSE,
126
+ citation=_CITATION,
127
+ )
128
+
129
+ def _split_generators(self, dl_manager):
130
+ urls = _URLS[self.config.name]
131
+ data_dir = dl_manager.download_and_extract(urls)
132
+ return [
133
+ datasets.SplitGenerator(
134
+ name=datasets.Split.VALIDATION,
135
+ # These kwargs will be passed to _generate_examples
136
+ gen_kwargs={
137
+ "filepath": data_dir,
138
+ "split": datasets.Split.VALIDATION,
139
+ },
140
+ ),
141
+ ]
142
+
143
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
144
+ def _generate_examples(self, filepath, split):
145
+ with open(filepath, encoding="utf-8") as f:
146
+ for key, row in enumerate(f):
147
+ data = json.loads(row)
148
+ context = (
149
+ data["context"]
150
+ .strip()
151
+ .replace("\n\n", "\n")
152
+ .replace("Q:", "Question:")
153
+ .replace("A:", "Answer:")
154
+ )
155
+ completion = data["completion"]
156
+ yield key, {"context": context, "completion": completion}