-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgraphicsSearch.py
More file actions
284 lines (238 loc) · 9.13 KB
/
Copy pathgraphicsSearch.py
File metadata and controls
284 lines (238 loc) · 9.13 KB
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
from DSL import *
from neuralSearch import *
from dispatch import dispatch
import random
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from torch.autograd import Variable
import torch.optim as optimization
import torch.cuda as cuda
from torch.nn.utils.rnn import pack_padded_sequence
GPU = cuda.is_available()
class GraphicsSearchPolicy(SearchPolicy):
def __init__(self):
LEXICON = ["START","END",
"circle",
"rectangle",
"line","arrow = True","arrow = False","solid = True","solid = False",
"for",
"reflect","x","y",
"i","j","None"] + map(str,range(-5,20))
super(GraphicsSearchPolicy,self).__init__(LEXICON)
self.circleEncoder = nn.Linear(2, self.H)
self.interactionEncoder1 = nn.Linear(self.H*2,self.H)
self.interactionEncoder2 = nn.Linear(self.H,self.H)
def encodeProblem(self, s):
if s == []:
t = torch.from_numpy(np.zeros((self.H,1))).float()
return Variable(t.cuda() if GPU else t)
encodings = [ self.circleEncoder(Variable(t.cuda() if GPU else t)).clamp(min = 0) \
for c in s\
for t in [torch.from_numpy(np.array([c.center.x, c.center.y])).float()] ]
interactions = [self.interactionEncoder1(torch.cat([x,y],dim = 0)).clamp(min = 0)
for i,x in enumerate(encodings)
for j,y in enumerate(encodings)]
interactions = [self.interactionEncoder2(interaction).clamp(min = 0)
for interaction in interactions ]
return sum(interactions)
def candidateEnvironments(self, program):
return list(e for e in candidateEnvironments(program))
def applyChange(self, program, environment, line):
if isinstance(program, Loop):
return Loop(body = self.applyChange(program.body, environment, line),
v = program.v,
bound = program.bound)
if isinstance(program, Reflection):
return Reflection(body = self.applyChange(program.body, environment, line),
axis = program.axis,
coordinate = program.coordinate)
assert isinstance(program, Block)
if environment == []:
return Block([self.parseLine(line)] + program.items)
# Figure out what it is that's being indexed into
for j,l in enumerate(program.items):
s = serializeLine(l)
if s == environment[:len(s)]:
lp = self.applyChange(l, environment[len(s):], line)
newItems = list(program.items)
newItems[j] = lp
return Block(newItems)
raise Exception('Environment indexes nonexistent context')
def Oracle(self, program): return list(Oracle(program))
def evaluate(self, program):
try: return program.evaluate(Environment([]))
except EvaluationError: return None
def solvesTask(self, goal, program):
return goal == self.evaluate(program)
def residual(self, goal, current):
#assert len(current - goal) == 0
return goal - current
def value(self, goal, program):
try:
output = self.evaluate(program)
except EvaluationError: return -1.0
if output == None: return -1.0
if len(output - goal) > 0: return 0.0
else: return 1.0/program.cost()
def parseLine(self, l):
def get(l):
n = l[0]
del l[0]
return n
def finish(l):
if l != []: raise Exception('Extra symbols in line')
def parseLinear(l):
b = int(get(l))
x = get(l)
m = int(get(l))
if x == 'None': x = None
return LinearExpression(m,x,b)
k = get(l)
if k == 'circle':
x = parseLinear(l)
y = parseLinear(l)
finish(l)
return Primitive(k,x,y)
if k == 'for':
v = get(l)
b = parseLinear(l)
finish(l)
return Loop(v = v, bound = b, body = Block([]))
if k == 'reflect':
a = get(l)
c = int(get(l))
finish(l)
return Reflection(body = Block([]), axis = a, coordinate = c)
raise Exception('parsing line '+k)
@dispatch(Block)
def Oracle(b):
for j,x in enumerate(b.items):
serialized = serializeLine(x)
yield Block(b.items[:j]), [], serialized
for program, environment, line in Oracle(x):
yield Block(b.items[:j] + [program]), serialized + environment, line
@dispatch(Primitive)
def Oracle(p):
return
yield
@dispatch(Loop)
def Oracle(l):
for program, environment, line in Oracle(l.body):
yield Loop(v = l.v, bound = l.bound, body = program), environment, line
@dispatch(Reflection)
def Oracle(l):
for program, environment, line in Oracle(l.body):
yield Reflection(axis = l.axis, coordinate = l.coordinate, body = program), environment, line
@dispatch(Loop)
def serializeLine(l):
return ["for",l.v] + serializeLine(l.bound)
@dispatch(Reflection)
def serializeLine(r):
return ["reflect",r.axis,str(r.coordinate)]
@dispatch(LinearExpression)
def serializeLine(e):
return [str(e.b),str(e.x),str(e.m)]
@dispatch(Primitive)
def serializeLine(p):
s = [p.k]
for a in p.arguments[:4]:
s += serializeLine(a)
if p.k == 'line':
s += ["arrow = True" if p.arguments[4] else "arrow = False" ]
s += ["solid = True" if p.arguments[5] else "solid = False"]
return s
@dispatch(Circle)
def serializeObservation(c):
return ["circle",str(c.center.x),str(c.center.y)]
@dispatch(Rectangle)
def serializeObservation(c):
return ["rectangle",str(c.p1.x),str(c.p1.y),str(c.p2.x),str(c.p2.y)]
@dispatch(Block)
def candidateEnvironments(b):
yield []
for x in b.items:
for e in candidateEnvironments(x):
yield e
@dispatch(Primitive)
def candidateEnvironments(_):
return
yield
@dispatch(Loop)
def candidateEnvironments(l):
this = serializeLine(l)
for e in candidateEnvironments(l.body):
yield this + e
@dispatch(Reflection)
def candidateEnvironments(r):
this = serializeLine(l)
for e in candidateEnvironments(l.body):
yield this + e
def simpleSceneSample():
def isolatedCircle():
x = random.choice(range(1,16))
y = random.choice(range(1,16))
return Primitive('circle', LinearExpression(0,None,x), LinearExpression(0,None,y))
MINIMUMATOMS = 1
MAXIMUMATOMS = 1
primitives = [isolatedCircle() for _ in range(random.choice(range(MINIMUMATOMS,MAXIMUMATOMS+1))) ]
loopIterations = random.choice([4])
while True:
bx = random.choice(range(1,16))
mx = random.choice(range(-5,6))
if all([x > 0 and x < 16 for j in range(loopIterations) for x in [mx*j + bx] ]): break
while True:
by = random.choice(range(1,16))
my = random.choice(range(-5,6))
if my == 0 and mx == 0: continue
if all([y > 0 and y < 16 for j in range(loopIterations) for y in [my*j + by] ]): break
l = Loop(v = 'j',bound = LinearExpression(0,None,loopIterations),
body = Block([Primitive('circle',
LinearExpression(mx,'j' if mx else None,bx),
LinearExpression(my,'j' if my else None,by))]))
return Block([l] + primitives)
if __name__ == "__main__":
p = GraphicsSearchPolicy()
if os.path.isfile('checkpoints/neuralSearch.p'):
p.load_state_dict(torch.load('checkpoints/neuralSearch.p'))
print "Resuming state from",'checkpoints/neuralSearch.p'
if GPU:
print "Using the GPU"
p.cuda()
o = optimization.Adam(p.parameters(), lr = 0.001)
step = 0
losses = []
while True:
step += 1
program = simpleSceneSample()
scene = set(program.convertToSequence().lines)
examples = p.makeOracleExamples(program, scene)
for example in examples:
o.zero_grad()
loss = p.loss(example)
loss.backward()
o.step()
losses.append(loss.data[0])
if step%100 == 0:
print "LOSS:", step,'\t',sum(losses)/len(losses)
losses = []
if step%5000 == 0:
torch.save(p.state_dict(),'checkpoints/neuralSearch.p')
print scene
print program.pretty()
print p.Oracle(program)
p0 = Block([])
p.beamSearchGraph(scene, p0, 30, 3)
continue
for _ in range(5):
p0 = p.sampleOneStep(scene, p0)
print p0
try:
denotation = p.evaluate(p0)
except EvaluationError:
print "Error evaluating that program"
break
if len(scene - denotation) == 0:
print "Nothing left to explain."
break