-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.jl
182 lines (157 loc) · 5.09 KB
/
data.jl
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
# data generation file
# action set:
# "mr" -> move right
const complexities = Dict{AbstractString, Int}("copy"=>1,
"reverse"=>2,
"walk"=>1,
"add"=>2,
"radd"=>3,
"mul"=>1)
const goldacts = Dict{Symbol, AbstractString}(:moveright=>"mr",
:moveleft=>"ml",
:up=>"up",
:down=>"down")
const no_op = -1
# walk data
const LEFT = -2
const DOWN = -3
const UP = -4
const ARROWS = Dict{Int64,Symbol}(LEFT => :moveleft, DOWN => :down, UP => :up)
"""
experiment is a string given as lowercase name
"""
function get_data_generator(experiment::AbstractString)
!haskey(complexities, experiment) && error("wrong key type $experiment")
comp = complexities[experiment]
fname = string(experiment, "_data")
f = eval(parse(fname))
return f
end
function copy_data(seqlen)
data = Any[rand(0:9) for i=1:seqlen]
actions = [goldacts[:moveright] for i =1:seqlen]
ygold = data
actions = map(ai->(ai,WRITE), actions)
return (data, ygold, actions)
end
function reverse_data(seqlen)
data = Any[ rand(0:9) for i=1:seqlen ]
ygold = reverse(data)
push!(data, LEFT)
actions = [goldacts[:moveright] for i=1:seqlen]
actions2 = [goldacts[:moveleft] for i=1:seqlen+1]
acts = append!(actions, actions2)
return (data, ygold, actions)
end
function add_data(seqlen)
low = parse("1"*"0"^(seqlen - 1))
hi = parse("1"*"0"^seqlen) - 1
n1 = rand(low:hi)
num2len = rand(1:seqlen)
num2 = parse("1"*"0"^num2len)-1
n2 = rand(1:num2)
data = (n1, n2)
ygold = n1 + n2
compound_move = Any[goldacts[:down], goldacts[:moveleft], goldacts[:up]]
arrived = 1
if arrived == seqlen
actions = compound_move
return (data, ygold, actions)
end
actions = []
while true
append!(actions, compound_move); arrived += 1;
(arrived == seqlen) && break
append!(actions, [goldacts[:moveleft]]); arrived +=1;
(arrived == seqlen) && break
end
if seqlen % 2 == 1
append!(actions, compound_move)
else
append!(actions, [goldacts[:moveleft]])
append!(actions, [goldacts[:down]])
end
return (data, ygold, actions)
end
function walk_data(seqlen)
direction = rand(-4:-2) # down, up, left
width = Int(seqlen/2)
grid = rand(0:9, width, width)
mainrow = hcat(rand(0:9, 1, width-1), [direction])
rowind = -1
if direction == UP
rowind = width
elseif direction == DOWN
rowind = 1
else
rowind = rand(1:width)
end
grid[rowind,:] = mainrow
actions = vcat(
map(x -> goldacts[:moveright], 1:width-1),
map(x -> goldacts[ARROWS[direction]], 1:width))
ygold = nothing
if direction == UP
ygold = reverse(grid[1:end-1,end])
elseif direction == DOWN
ygold = grid[2:end,end]
else
ygold = reverse(grid[rowind,1:end-1])
end
return (grid,ygold,actions)
end
function mul_data(seqlen)
hi = parse("1"*"0"^seqlen) - 1
low = parse("1"*"0"^(seqlen - 1))
n = rand(low:hi)
digit = rand(0:9)
data = (digit, n)
ygold = n * digit
actions = append!([goldacts[:down]], [ goldacts[:moveleft] for i=1:seqlen+1])
return (data, ygold, actions)
end
function radd_data(seqlen)
hi = parse("1"*"0"^seqlen) - 1
low = parse("1"*"0"^(seqlen - 1))
n = rand(low:hi)
n2 = rand(1:hi)
n3 = rand(1:hi)
data = (n, n2, n3)
ygold = n + n2 + n3
compound_action = Any[ goldacts[:down], goldacts[:down], goldacts[:moveleft], goldacts[:up], goldacts[:up]]
arrived = 1
if arrived == seqlen
actions = [ goldacts[:down], goldacts[:down], goldacts[:moveleft], goldacts[:up]]
return (data, ygold, actions)
end
if seqlen == 2
actions = append!(compound_action, [ goldacts[:moveleft], goldacts[:down]])
return (data, ygold, actions)
end
actions = []
while true
append!(actions, compound_action); arrived +=1;
(arrived == seqlen-1) && break
append!(actions, [goldacts[:moveleft]]); arrived +=1;
(arrived == seqlen-1) && break
end
fs(num) = length(digits(num))
second_big = ( fs(n2) > fs(n3) ? fs(n2) : fs(n3) )
if seqlen % 2 == 1
append!(actions, [ goldacts[:moveleft] ])
if second_big == seqlen
append!(actions, [ goldacts[:down] ])
if fs(n2) == fs(n3)
append!(actions, [ goldacts[:down], goldacts[:moveleft], goldacts[:up] ])
else
append!(actions, [ goldacts[:down], goldacts[:moveleft] ])
end
else
append!(actions, [ goldacts[:down], goldacts[:down] ])
end
else
append!(actions, compound_action)
append!(actions, [ goldacts[:moveleft], goldacts[:down] ])
end
return (data, ygold, actions)
end