-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlinearsearch.lua
290 lines (226 loc) · 9.38 KB
/
linearsearch.lua
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
-- This a config for the copy task
local config = {}
-- For conversion to distributions
local distUtils = require 'nc.distUtils'
local f = distUtils.flatDist
-- Name of the algorithm
config.name = "LinearSearch"
-- Number of available registers (excluding the RI)
config.nb_registers = 7
-- Number of instructions this program uses
config.nb_existing_ops = 11
-- Size of the memory tape and largest number addressable
config.memory_size = 15
-- Initial state of the registers
config.registers_init = torch.Tensor{6,0,1,0,0,1,0}
config.registers_init = distUtils.toDistTensor(config.registers_init, config.memory_size)
-- Program
config.nb_states = 8
config.program = {
torch.Tensor{4,5,1,1,5,4,4,6},
torch.Tensor{6,6,3,0,6,2,5,6},
torch.Tensor{3,1,1,6,5,6,6,6},
torch.Tensor{8,8,4,10,2,10,9,0},
}
config.example_input = torch.zeros(config.memory_size)
config.example_input[1] = 3
config.example_input[2] = 10
config.example_input[3] = 14
config.example_input[4] = 8
config.example_input[5] = 7
config.example_input[6] = 6
config.example_input[7] = 5
config.example_input[8] = 3
config.example_input[9] = 1
config.example_input[10] = 2
config.example_output = config.example_input:clone()
config.example_output[1] = 7
config.example_input = distUtils.toDistTensor(config.example_input, config.memory_size)
config.example_output = distUtils.toDistTensor(config.example_output, config.memory_size)
config.example_loss_mask = torch.ones(config.memory_size, config.memory_size)
local function randint(a, b)
return a + math.floor(torch.uniform()*(b-a))
end
config.gen_sample = function()
-- If memory size is 15, the worst case takes 72 iterations
-- Remove 5 iterations per decrease in the max size
local max_size = config.memory_size - 1
local list_length = randint(2, max_size)
-- We don't want any duplicates in the list
local full_list = torch.randperm(max_size)
local input = torch.zeros(config.memory_size)
input:narrow(1,2, list_length):copy(full_list:narrow(1,1,list_length))
-- Which element are we looking for?
local index = randint(1, list_length)
input[1] = full_list[index]
local output = input:clone()
output[1] = index
local loss_mask = torch.zeros(config.memory_size, config.memory_size)
loss_mask[1]:fill(1)
input = distUtils.toDistTensor(input, config.memory_size)
output = distUtils.toDistTensor(output, config.memory_size)
return input, output, loss_mask
end
config.gen_biased_sample_not_before_k = function(k)
-- This is biased because the correct value can not be in the first (k-1) ones.
local max_size = config.memory_size - 1
local list_length = randint(k, max_size)
-- We don't want any duplicates in the list
local full_list = torch.randperm(max_size)
local input = torch.zeros(config.memory_size)
input:narrow(1,2, list_length):copy(full_list:narrow(1,1,list_length))
-- Which element are we looking for?
local index = randint(k, list_length)
input[1] = full_list[index]
local output = input:clone()
output[1] = index
local loss_mask = torch.zeros(config.memory_size, config.memory_size)
loss_mask[1]:fill(1)
input = distUtils.toDistTensor(input, config.memory_size)
output = distUtils.toDistTensor(output, config.memory_size)
return input, output, loss_mask
end
local not_before_k_ateur = function(k)
return function()
return config.gen_biased_sample_not_before_k(k)
end
end
config.gen_biased_only_at_even_pos = function()
-- This is biased because the correct value can only be even
local max_size = config.memory_size - 1
local list_length = randint(2, max_size)
-- We don't want any duplicates in the list
local full_list = torch.randperm(max_size)
local input = torch.zeros(config.memory_size)
input:narrow(1,2, list_length):copy(full_list:narrow(1,1,list_length))
-- Which element are we looking for?
local index = randint(1, math.floor(list_length/2))
index = 2 * index
input[1] = full_list[index]
local output = input:clone()
output[1] = index
local loss_mask = torch.zeros(config.memory_size, config.memory_size)
loss_mask[1]:fill(1)
input = distUtils.toDistTensor(input, config.memory_size)
output = distUtils.toDistTensor(output, config.memory_size)
return input, output, loss_mask
end
config.gen_biased_only_at_odd_pos = function()
-- This is biased because the correct value can only be odd
local max_size = config.memory_size - 1
local list_length = randint(2, max_size)
-- We don't want any duplicates in the list
local full_list = torch.randperm(max_size)
local input = torch.zeros(config.memory_size)
input:narrow(1,2, list_length):copy(full_list:narrow(1,1,list_length))
-- Which element are we looking for?
local index = randint(0, math.floor((list_length-1)/2))
index = 1+ 2 * index
input[1] = full_list[index]
local output = input:clone()
output[1] = index
local loss_mask = torch.zeros(config.memory_size, config.memory_size)
loss_mask[1]:fill(1)
input = distUtils.toDistTensor(input, config.memory_size)
output = distUtils.toDistTensor(output, config.memory_size)
return input, output, loss_mask
end
config.gen_biased_sorted_down = function()
-- This is biased because all the elements are sorted in decreasing order
local max_size = config.memory_size - 1
local list_length = randint(2, max_size)
-- We don't want any duplicates in the list
local full_list = torch.randperm(max_size)
full_list = full_list:narrow(1,1,list_length):sort(1, true)
local input = torch.zeros(config.memory_size)
input:narrow(1,2, list_length):copy(full_list)
-- Which element are we looking for?
local index = randint(0, math.floor((list_length-1)/2))
index = 1+ 2 * index
input[1] = full_list[index]
local output = input:clone()
output[1] = index
local loss_mask = torch.zeros(config.memory_size, config.memory_size)
loss_mask[1]:fill(1)
input = distUtils.toDistTensor(input, config.memory_size)
output = distUtils.toDistTensor(output, config.memory_size)
return input, output, loss_mask
end
config.gen_biased_sorted_up = function()
-- This is biased because all the elements are sorted in increasing order
local max_size = config.memory_size - 1
local list_length = randint(2, max_size)
-- We don't want any duplicates in the list
local full_list = torch.randperm(max_size)
full_list = full_list:narrow(1,1,list_length):sort(1, false)
local input = torch.zeros(config.memory_size)
input:narrow(1,2, list_length):copy(full_list)
-- Which element are we looking for?
local index = randint(0, math.floor((list_length-1)/2))
index = 1+ 2 * index
input[1] = full_list[index]
local output = input:clone()
output[1] = index
local loss_mask = torch.zeros(config.memory_size, config.memory_size)
loss_mask[1]:fill(1)
input = distUtils.toDistTensor(input, config.memory_size)
output = distUtils.toDistTensor(output, config.memory_size)
return input, output, loss_mask
end
config.gen_biased_natural = function()
-- This is biased because the list is always the sequence of natural orders
-- (potentially wrapping around)
local max_size = config.memory_size - 1
local list_length = randint(2, max_size)
local list_start = randint(1, config.memory_size-1)
-- We don't want any duplicates in the list
local full_list = torch.cat(torch.range(list_start, config.memory_size-1), torch.range(0, list_start),1)
local input = torch.zeros(config.memory_size)
input:narrow(1,2, list_length):copy(full_list:narrow(1,1,list_length))
-- Which element are we looking for?
local index = randint(0, math.floor((list_length-1)/2))
index = 1+ 2 * index
input[1] = full_list[index]
local output = input:clone()
output[1] = index
local loss_mask = torch.zeros(config.memory_size, config.memory_size)
loss_mask[1]:fill(1)
input = distUtils.toDistTensor(input, config.memory_size)
output = distUtils.toDistTensor(output, config.memory_size)
return input, output, loss_mask
end
config.gen_biased_natural_reverse = function()
-- This is biased because the list is always the sequence of natural orders, in reverse orders
-- (potentially wrapping around)
local max_size = config.memory_size - 1
local list_length = randint(2, max_size)
local list_start = randint(1, config.memory_size-1)
-- We don't want any duplicates in the list
local full_list = torch.cat(torch.range(0, list_start):sort(1,true),
torch.range(list_start, config.memory_size-1):sort(1,true),1)
local input = torch.zeros(config.memory_size)
input:narrow(1,2, list_length):copy(full_list:narrow(1,1,list_length))
-- Which element are we looking for?
local index = randint(0, math.floor((list_length-1)/2))
index = 1+ 2 * index
input[1] = full_list[index]
local output = input:clone()
output[1] = index
local loss_mask = torch.zeros(config.memory_size, config.memory_size)
loss_mask[1]:fill(1)
input = distUtils.toDistTensor(input, config.memory_size)
output = distUtils.toDistTensor(output, config.memory_size)
return input, output, loss_mask
end
config.gen_biased_sample = {
config.gen_biased_only_at_even_pos,
config.gen_biased_only_at_odd_pos,
config.gen_biased_sorted_down,
config.gen_biased_sorted_up,
config.gen_biased_natural,
config.gen_biased_natural_reverse,
not_before_k_ateur(3),
not_before_k_ateur(5),
not_before_k_ateur(8)
}
return config