rbiswasfc commited on
Commit
6cdd91d
·
verified ·
1 Parent(s): 5c13f72

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +268 -68
README.md CHANGED
@@ -1,68 +1,268 @@
1
- ---
2
- license: apache-2.0
3
- dataset_info:
4
- - config_name: derived-split
5
- features:
6
- - name: question
7
- dtype: string
8
- - name: answer
9
- dtype: string
10
- splits:
11
- - name: train
12
- num_bytes: 5284
13
- num_examples: 120
14
- - name: valid
15
- num_bytes: 4808
16
- num_examples: 104
17
- download_size: 6274
18
- dataset_size: 10092
19
- - config_name: direct-split
20
- features:
21
- - name: question
22
- dtype: string
23
- - name: answer
24
- dtype: string
25
- splits:
26
- - name: train
27
- num_bytes: 9726
28
- num_examples: 216
29
- - name: valid
30
- num_bytes: 366
31
- num_examples: 8
32
- download_size: 5842
33
- dataset_size: 10092
34
- - config_name: random-split
35
- features:
36
- - name: question
37
- dtype: string
38
- - name: answer
39
- dtype: string
40
- splits:
41
- - name: train
42
- num_bytes: 11497
43
- num_examples: 240
44
- - name: valid
45
- num_bytes: 11158
46
- num_examples: 240
47
- download_size: 38532
48
- dataset_size: 22655
49
- configs:
50
- - config_name: derived-split
51
- data_files:
52
- - split: train
53
- path: derived-split/train-*
54
- - split: valid
55
- path: derived-split/valid-*
56
- - config_name: direct-split
57
- data_files:
58
- - split: train
59
- path: direct-split/train-*
60
- - split: valid
61
- path: direct-split/valid-*
62
- - config_name: random-split
63
- data_files:
64
- - split: train
65
- path: random-split/train-*
66
- - split: valid
67
- path: random-split/valid-*
68
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ dataset_info:
4
+ - config_name: derived-split
5
+ features:
6
+ - name: question
7
+ dtype: string
8
+ - name: answer
9
+ dtype: string
10
+ splits:
11
+ - name: train
12
+ num_bytes: 5284
13
+ num_examples: 120
14
+ - name: valid
15
+ num_bytes: 4808
16
+ num_examples: 104
17
+ download_size: 6274
18
+ dataset_size: 10092
19
+ - config_name: direct-split
20
+ features:
21
+ - name: question
22
+ dtype: string
23
+ - name: answer
24
+ dtype: string
25
+ splits:
26
+ - name: train
27
+ num_bytes: 9726
28
+ num_examples: 216
29
+ - name: valid
30
+ num_bytes: 366
31
+ num_examples: 8
32
+ download_size: 5842
33
+ dataset_size: 10092
34
+ - config_name: random-split
35
+ features:
36
+ - name: question
37
+ dtype: string
38
+ - name: answer
39
+ dtype: string
40
+ splits:
41
+ - name: train
42
+ num_bytes: 11497
43
+ num_examples: 240
44
+ - name: valid
45
+ num_bytes: 11158
46
+ num_examples: 240
47
+ download_size: 38532
48
+ dataset_size: 22655
49
+ configs:
50
+ - config_name: derived-split
51
+ data_files:
52
+ - split: train
53
+ path: derived-split/train-*
54
+ - split: valid
55
+ path: derived-split/valid-*
56
+ - config_name: direct-split
57
+ data_files:
58
+ - split: train
59
+ path: direct-split/train-*
60
+ - split: valid
61
+ path: direct-split/valid-*
62
+ - config_name: random-split
63
+ data_files:
64
+ - split: train
65
+ path: random-split/train-*
66
+ - split: valid
67
+ path: random-split/valid-*
68
+ ---
69
+
70
+ A synthetic dataset to test memorization capabilities of an LLM.
71
+
72
+ Code used to create the dataset:
73
+
74
+ ```python
75
+ import argparse
76
+ import random
77
+
78
+ import pandas as pd
79
+ from datasets import Dataset
80
+
81
+ kv_templates = [
82
+ "What is the value of {key}?",
83
+ "What number is assigned to {key}?",
84
+ "In this dataset, what does {key} represent?",
85
+ ]
86
+
87
+ rel_templates = [
88
+ "What is the relationship between {key1} and {key2}?",
89
+ "How does {key1} compare to {key2}?",
90
+ "Is {key1} greater than, less than, or equal to {key2}?",
91
+ ]
92
+
93
+ arithmetic_templates = [
94
+ "What is {key1} {operation} {key2}?",
95
+ "If you {operation_word} {key1} and {key2}, what do you get?",
96
+ "Calculate {key1} {operation} {key2}.",
97
+ "What's the result when you {operation_word} the values of {key1} and {key2}?",
98
+ ]
99
+
100
+ op_dict = {"+": "add", "-": "subtract", "*": "multiply"}
101
+
102
+
103
+ def generate_key_value_pairs(num_pairs, amax=32, offset=0):
104
+ pairs = {}
105
+ for i in range(num_pairs):
106
+ key = f"x{i + offset}"
107
+ value = random.randint(-amax, amax)
108
+ pairs[key] = value
109
+ return pairs
110
+
111
+
112
+ def generate_relations(kv_pairs, num_gen_relations=None):
113
+ relations = []
114
+ keys = list(kv_pairs.keys())
115
+
116
+ if num_gen_relations is None:
117
+ relation_fraction = 1.0 # 0.5
118
+ max_num_relations = len(keys) * (len(keys) - 1) // 2 - len(keys)
119
+ num_gen_relations = int(relation_fraction * max_num_relations)
120
+
121
+ visited = set()
122
+ while len(visited) < num_gen_relations:
123
+ key1, key2 = random.sample(keys, 2)
124
+ if key1 == key2:
125
+ continue
126
+ pair = tuple(sorted([key1, key2]))
127
+ if pair in visited:
128
+ continue
129
+ visited.add(pair)
130
+
131
+ if kv_pairs[key1] > kv_pairs[key2]:
132
+ relation = ">"
133
+ elif kv_pairs[key1] < kv_pairs[key2]:
134
+ relation = "<"
135
+ else:
136
+ relation = "="
137
+
138
+ relations.append(f"{key1} {relation} {key2}")
139
+ return relations
140
+
141
+
142
+ def generate_compositions(kv_pairs, num_gen_compositions=None):
143
+ combined_facts = []
144
+ operator_set = ["+", "-", "*"]
145
+ keys = list(kv_pairs.keys())
146
+
147
+ if num_gen_compositions is None:
148
+ composition_fraction = 1.0 # 0.5
149
+ max_num_compositions = len(keys) * (len(keys) - 1) // 2 * len(operator_set)
150
+ num_gen_compositions = int(composition_fraction * max_num_compositions)
151
+
152
+ visited = set()
153
+ while len(visited) < num_gen_compositions:
154
+ key1, key2 = random.sample(keys, 2)
155
+ operation = random.choice(operator_set)
156
+
157
+ instance = tuple(sorted([key1, key2, operation]))
158
+ if instance in visited:
159
+ continue
160
+ visited.add(instance)
161
+
162
+ if operation == "+":
163
+ result = kv_pairs[key1] + kv_pairs[key2]
164
+ fact = f"{key1} + {key2} = {result}"
165
+ elif operation == "-":
166
+ result = kv_pairs[key1] - kv_pairs[key2]
167
+ fact = f"{key1} - {key2} = {result}"
168
+ elif operation == "*":
169
+ result = kv_pairs[key1] * kv_pairs[key2]
170
+ fact = f"{key1} * {key2} = {result}"
171
+
172
+ combined_facts.append(fact)
173
+
174
+ return combined_facts
175
+
176
+
177
+ def create_examples(kv_size=8):
178
+ num_pairs = kv_size
179
+
180
+ kv_pairs = generate_key_value_pairs(num_pairs)
181
+ relations = generate_relations(kv_pairs)
182
+ compositions = generate_compositions(kv_pairs)
183
+
184
+ example_dict = {}
185
+
186
+ examples = []
187
+ for key, value in kv_pairs.items():
188
+ template = random.choice(kv_templates)
189
+ examples.append(
190
+ {
191
+ "question": template.format(key=key),
192
+ "answer": f"{value}",
193
+ }
194
+ )
195
+ example_dict["direct"] = examples
196
+
197
+ examples = []
198
+ for relation in relations:
199
+ key1, rel, key2 = relation.split()
200
+ template = random.choice(rel_templates)
201
+ examples.append({"question": template.format(key1=key1, key2=key2), "answer": relation})
202
+ example_dict["relations"] = examples
203
+
204
+ examples = []
205
+ for ex in compositions:
206
+ lhs, rhs = ex.split("=")
207
+ answer = rhs.strip()
208
+ key1, op, key2 = lhs.split()
209
+ operation_word = op_dict[op]
210
+
211
+ template = random.choice(arithmetic_templates)
212
+
213
+ examples.append(
214
+ {
215
+ "question": template.format(key1=key1, key2=key2, operation=op, operation_word=operation_word),
216
+ "answer": answer,
217
+ }
218
+ )
219
+ example_dict["compositions"] = examples
220
+ return example_dict
221
+
222
+
223
+ if __name__ == "__main__":
224
+ ap = argparse.ArgumentParser()
225
+ ap.add_argument("--kv_size", type=int, default=8, help="Number of key-value pairs")
226
+ ap.add_argument("--split_strategy", type=str, default="random", help="Split strategy for training and evaluation data")
227
+ ap.add_argument("--seed", type=int, default=42, help="Random seed for reproducibility")
228
+ args = ap.parse_args()
229
+
230
+ # set seed
231
+ random.seed(args.seed)
232
+
233
+ if args.split_strategy == "random":
234
+ example_dict = create_examples(kv_size=args.kv_size)
235
+ all_examples = example_dict["direct"] + example_dict["relations"] + example_dict["compositions"]
236
+ # 50-50 split
237
+ random.shuffle(all_examples)
238
+ split_idx = len(all_examples) // 2
239
+ train_examples = all_examples[:split_idx]
240
+ eval_examples = all_examples[split_idx:]
241
+
242
+ else:
243
+ example_dict_part_a = create_examples(kv_size=args.kv_size // 2)
244
+ example_dict_part_b = create_examples(kv_size=args.kv_size // 2)
245
+
246
+ part_a_examples = example_dict_part_a["direct"] + example_dict_part_a["relations"] + example_dict_part_a["compositions"]
247
+ part_b_direct_examples = example_dict_part_b["direct"]
248
+ part_b_derived_examples = example_dict_part_b["relations"] + example_dict_part_b["compositions"]
249
+
250
+ if args.split_strategy == "direct":
251
+ train_examples = part_a_examples + part_b_derived_examples
252
+ eval_examples = part_b_direct_examples
253
+
254
+ elif args.split_strategy == "derived":
255
+ train_examples = part_a_examples + part_b_direct_examples
256
+ eval_examples = part_b_derived_examples
257
+
258
+ # push to HF
259
+ train_ds = Dataset.from_pandas(pd.DataFrame(train_examples), preserve_index=False)
260
+ eval_ds = Dataset.from_pandas(pd.DataFrame(eval_examples), preserve_index=False)
261
+
262
+ train_ds.push_to_hub("rbiswasfc/synthetic-kv", f"{args.split_strategy}-split", split="train")
263
+ eval_ds.push_to_hub("rbiswasfc/synthetic-kv", f"{args.split_strategy}-split", split="valid")
264
+
265
+
266
+ # python generate_datasets.py --kv_size 16 --split_strategy derived --seed 42
267
+
268
+ ```