-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.py
292 lines (220 loc) · 9.35 KB
/
data.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import numpy as np
logic_gates = [
[[1, 0], [0, 0]],
[[0, 1], [0, 0]],
[[0, 0], [1, 0]],
[[0, 1], [1, 0]],
[[1, 1], [1, 0]],
[[0, 0], [0, 1]],
[[1, 0], [0, 1]],
[[1, 1], [0, 1]],
[[1, 0], [1, 1]],
[[0, 1], [1, 1]]
]
def generate_parity_data(batch_size, dimensions=64, fixed_parity_bits=0, seed=None):
inputs, targets = [], []
if seed is not None:
state = np.random.get_state()
np.random.seed(seed)
for b in range(batch_size):
input_vector = np.zeros([dimensions])
idx = np.arange(0, dimensions)
np.random.shuffle(idx)
if fixed_parity_bits > 0 and fixed_parity_bits <= dimensions:
threshold = fixed_parity_bits
else:
threshold = np.random.randint(1, dimensions + 1)
values = np.random.randint(2, size=[threshold]) * 2 - 1
parity = np.sum(np.where(values > 0, [1.0], [0.0]), dtype=np.int32) % 2
input_vector[idx[:threshold]] = values
inputs.append([input_vector])
targets.append([[parity]])
if seed is not None:
np.random.set_state(state)
return np.stack(inputs), np.stack(targets)
def generate_logic_data(batch_size, min_time_steps=1, max_time_steps=10,
min_gates=1, max_gates=10, fixed_gates=0, used_gates=10, seed=None):
inputs, targets, seq_length = [], [], []
if used_gates > len(logic_gates):
raise AttributeError("used_gates can't be greater than {0}".format(len(logic_gates)))
if seed is not None:
state = np.random.get_state()
np.random.seed(seed)
for b in range(batch_size):
input_steps = np.zeros([max_time_steps, max_gates * len(logic_gates) + 2])
target_steps = np.zeros([max_time_steps, 1])
seq_length.append(
np.random.randint(
min_time_steps,
max_time_steps + 1
)
)
b0 = np.random.randint(2)
for t in range(seq_length[-1]):
b1 = np.random.randint(2)
if t == 0:
input_steps[t][0] = b0
input_steps[t][1] = b1
if fixed_gates > 0 and fixed_gates <= max_gates:
num_gates = fixed_gates
else:
num_gates = np.random.randint(min_gates, max_gates + 1)
for g in range(num_gates):
gate = np.random.randint(used_gates)
b1, b0 = logic_gates[gate][b1][b0], b1
input_steps[t][2 + g * len(logic_gates) + gate] = 1
target_steps[t][0] = b1
b0 = b1
inputs.append(input_steps)
targets.append(target_steps)
if seed is not None:
np.random.set_state(state)
return np.stack(inputs), np.stack(targets), seq_length
def generate_addition_data(batch_size, min_time_steps=1, max_time_steps=5, max_digits=5,
fixed_digits=0, seed=None):
inputs, targets, seq_length = [], [], []
if seed is not None:
state = np.random.get_state()
np.random.seed(seed)
for b in range(batch_size):
input_steps = np.zeros([max_time_steps, max_digits * 10])
target_steps = np.zeros([max_time_steps, max_digits + 1])
seq_length.append(
np.random.randint(
min_time_steps,
max_time_steps + 1
)
)
running_sum = 0
for t in range(seq_length[-1]):
if fixed_digits > 0 and fixed_digits <= max_digits:
digits_no = fixed_digits
else:
digits_no = np.random.randint(1, max_digits + 1)
current_number = np.random.randint(10 ** (digits_no - 1), 10 ** digits_no)
number_digits = np.array([int(c) for c in str(current_number)])
for d in range(digits_no):
input_steps[t][d * 10 + number_digits[d]] = 1
running_sum += current_number
sum_digits = np.array([int(c) for c in str(running_sum)])
for d in range(max_digits + 1):
if d < len(sum_digits):
target_steps[t][d] = sum_digits[d]
else:
target_steps[t][d] = 10
inputs.append(input_steps)
targets.append(target_steps)
if seed is not None:
np.random.set_state(state)
return np.stack(inputs), np.stack(targets), seq_length
def generate_sort_data(batch_size, min_numbers=2, max_numbers=15, fixed_numbers=0, seed=None):
inputs, targets, seq_length = [], [], []
if seed is not None:
state = np.random.get_state()
np.random.seed(seed)
for b in range(batch_size):
input_steps = np.zeros([max_numbers * 2, 2])
target_steps = np.zeros([max_numbers * 2, 1])
if fixed_numbers > 0 and fixed_numbers <= max_numbers:
numbers_count = fixed_numbers
else:
numbers_count = np.random.randint(
min_numbers,
max_numbers + 1
)
seq_length.append(numbers_count * 2)
numbers = np.random.randn(numbers_count)
indices = np.argsort(numbers)
for n in range(numbers_count * 2):
if n < numbers_count:
input_steps[n][1] = numbers[n]
if n == numbers_count - 1:
input_steps[n][0] = 1
else:
target_steps[n][0] = indices[n - numbers_count]
inputs.append(input_steps)
targets.append(target_steps)
if seed is not None:
np.random.set_state(state)
return np.stack(inputs), np.stack(targets), seq_length
def test_parity_data(inputs, targets):
for b in range(len(inputs)):
computed_parity = 0
for d in range(len(inputs[b][0])):
if inputs[b][0][d] == 1.0:
computed_parity = 1 - computed_parity
target_parity = targets[b][0][0]
assert (computed_parity == target_parity),\
"Parity does not match at batch {0}: {1} (computed) vs. {2} (target)".format(
b, computed_parity, target_parity
)
def test_logic_data(inputs, targets, seq_length):
gates_num = len(logic_gates)
for b in range(len(inputs)):
for t in range(seq_length[b]):
b0 = int(inputs[b][0][0]) if t == 0 else b1
b1 = int(inputs[b][t][1])
if t > 0:
assert (inputs[b][t][0] == 0.0), \
"First bit is not zero in batch {0} time-step {1}".format(b, t)
for g in range((len(inputs[b][t]) - 2) // gates_num):
one_hot = inputs[b][t][g * gates_num + 2:(g+1) * gates_num + 2]
if np.max(one_hot) == 1.0:
gate = np.asscalar(np.argmax(one_hot))
b1, b0 = logic_gates[gate][b1][b0], b1
target_bit = targets[b][t][0]
assert (target_bit == b1), \
"Target bit does not match at batch {0} time step {1}: {2} (computed) vs. {3} (target)".format(
b, t, b1, target_bit
)
def test_addition_data(inputs, targets, seq_length):
for b in range(len(inputs)):
running_sum = 0
for t in range(seq_length[b]):
current_number = 0
for d in range(len(inputs[b][t]) // 10):
one_hot = inputs[b][t][d * 10:d * 10 + 10]
if np.max(one_hot) == 1.0:
current_number = current_number * 10 + np.argmax(one_hot)
target_sum = 0
for d in range(len(targets[b][t])):
if targets[b][t][d] != 10:
target_sum = target_sum * 10 + targets[b][t][d]
running_sum += current_number
assert (running_sum == target_sum), \
"Running sum doesn't match at batch {0} time step {1}: {2} (computed) vs. {3} (target)".format(
b, t, running_sum, target_sum
)
def test_sort_data(inputs, targets, seq_length):
for b in range(len(inputs)):
numbers, target_indices = [], []
numbers_count = seq_length[b] // 2
for n in range(numbers_count):
numbers.append(inputs[b][n][1])
target_indices.append(targets[b][numbers_count + n][0])
if n == numbers_count - 1:
assert (inputs[b][n][0] == 1.0), "End bit is not one at batch {0} time step {1}".format(b, n)
else:
assert (inputs[b][n][0] == 0.0), "Non-end bit is not zero at batch {0} time step {1}".format(b, n)
computed_indices = np.argsort(numbers)
assert np.all(computed_indices == target_indices), \
"Sorted indices don't match at batch {0}: {1} (computed) vs. {2} (target)".format(
b, computed_indices, target_indices
)
if __name__ == "__main__":
print("Testing parity data... ", end="")
for i in range(100):
test_parity_data(*generate_parity_data(128, fixed_parity_bits=i % 10))
print("passed")
print("Testing logic data... ", end="")
for i in range(100):
test_logic_data(*generate_logic_data(16, fixed_gates=i % 10))
print("passed")
print("Testing addition data... ", end="")
for i in range(100):
test_addition_data(*generate_addition_data(32, fixed_digits=i % 10))
print("passed")
print("Testing sort data... ", end="")
for i in range(100):
test_sort_data(*generate_sort_data(16, fixed_numbers=i % 10))
print("passed")