-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit_tests_recognition.py
256 lines (217 loc) · 9.13 KB
/
unit_tests_recognition.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
import logging, unittest, random, copy, torch, torch.nn as nn, numpy as np, torch.nn.functional as F
from torch.nn.utils.rnn import pack_padded_sequence
from type_system import Type, PolymorphicType, PrimitiveType, Arrow, List, UnknownType, INT, BOOL
from program import Program, Function, Variable, BasicPrimitive, New
from dsl import DSL
from pcfg_logprob import LogProbPCFG
from embedding import Embedding
from pcfg_predictions import PCFG_Predictor
from Q_predictions import Q_Predictor
logging_levels = {0:logging.INFO, 1:logging.DEBUG}
verbosity = 0
logging.basicConfig(format='%(message)s', level=logging_levels[verbosity])
class TestSum(): # unittest.TestCase
def test_predictions_noinputs(self):
primitive_types = {
"if": Arrow(BOOL, Arrow(INT, INT)),
"+": Arrow(INT, Arrow(INT, INT)),
"0": INT,
"1": INT,
"and": Arrow(BOOL, Arrow(BOOL, BOOL)),
"lt": Arrow(INT, Arrow(INT, BOOL)),
}
semantics = {
"if": lambda b: lambda x: lambda y: x if b else y,
"+": lambda x: lambda y: x + y,
"0": 0,
"1": 1,
"and": lambda b1: lambda b2: b1 and b2,
"lt": lambda x: lambda y: x <= y,
}
template_dsl = DSL(semantics, primitive_types)
type_request = INT
template_cfg = template_dsl.DSL_to_CFG(type_request=type_request,
upper_bound_type_size=4,
max_program_depth=4,
min_variable_depth=2,
n_gram = 1)
list_variables = [
Variable(i, type_, probability={})
for i,type_ in enumerate(type_request.arguments())
]
H = 128 # hidden size of neural network
lexicon = list(range(10)) # all elements in range(10)
fe = None #RecurrentFeatureExtractor(lexicon=lexicon,
# H=H,
# bidirectional=True)
PCFG_predictor = PCFG_Predictor(
fe,
template_cfg=template_cfg
)
Q_predictor = Q_Predictor(
fe,
template_dsl=template_dsl,
template_cfg=template_cfg,
list_variables=list_variables,
)
programs = [
Function(BasicPrimitive("+", Arrow(INT, Arrow(INT, INT))),[BasicPrimitive("0", INT),BasicPrimitive("1", INT)], INT),
]
x = [] # input
y = [] # output
ex = (x,y) # a single input/output example
tasks = [[ex]]
# PCFG_predictor.train(programs, tasks)
# PCFG_predictor.test(programs, tasks)
# Q_predictor.train(programs, tasks)
# Q_predictor.test(programs, tasks)
def test_predictions_with_inputs(self):
t0 = PolymorphicType('t0')
t1 = PolymorphicType('t1')
primitive_types = {
"if": Arrow(BOOL, Arrow(INT, INT)),
"+": Arrow(INT, Arrow(INT, INT)),
"0": INT,
"1": INT,
"and": Arrow(BOOL, Arrow(BOOL, BOOL)),
"lt": Arrow(INT, Arrow(INT, BOOL)),
"map": Arrow(Arrow(t0, t1), Arrow(List(t0), List(t1))),
}
semantics = {
"if": lambda b: lambda x: lambda y: x if b else y,
"+": lambda x: lambda y: x + y,
"0": 0,
"1": 1,
"and": lambda b1: lambda b2: b1 and b2,
"lt": lambda x: lambda y: x <= y,
"map": lambda f: lambda l: list(map(f, l)),
}
template_dsl = DSL(semantics, primitive_types)
type_request = Arrow(List(INT), List(INT))
template_cfg = template_dsl.DSL_to_CFG(type_request=type_request,
upper_bound_type_size=10,
max_program_depth=4,
min_variable_depth=1,
n_gram = 1)
H = 128 # hidden size of neural network
lexicon = list(range(10))
fe = None # RecurrentFeatureExtractor(lexicon=lexicon,
# H=H,
# bidirectional=True)
list_variables = [
Variable(i, type_, probability={})
for i,type_ in enumerate(type_request.arguments())
]
PCFG_predictor = PCFG_Predictor(
fe,
template_cfg=template_cfg
)
Q_predictor = Q_Predictor(
fe,
template_dsl=template_dsl,
template_cfg=template_cfg,
list_variables=list_variables,
)
programs = [
Function(
BasicPrimitive("map", Arrow(Arrow(INT, INT), Arrow(List(INT), List(INT)))),
[
Function(
BasicPrimitive("+", Arrow(INT, Arrow(INT, INT))),
[BasicPrimitive("1", INT)],
Arrow(INT, INT)
),
Variable(0, List(INT))
],
List(INT)
),
Function(
BasicPrimitive("map", Arrow(Arrow(INT, INT), Arrow(List(INT), List(INT)))),
[
Function(
BasicPrimitive("+", Arrow(INT, Arrow(INT, INT))),
[Function(
BasicPrimitive("+", Arrow(INT, Arrow(INT, INT))),
[BasicPrimitive("1", INT), BasicPrimitive("1", INT)],
INT),
],
Arrow(INT, INT)
),
Variable(0, List(INT))
],
List(INT)
)
]
# each task is a list of I/O
# each I/O is a tuple of input, output
# each output is a list whose members are elements of self.lexicon
# each input is a tuple of lists, and each member of each such list is an element of self.lexicon
x = ([4,4,2],) # input
y = [5,5,3] # output
ex1 = (x,y) # a single input/output example
x = ([7,1],) # input
y = [8,2] # output
ex2 = (x,y) # a single input/output example
task1 = [ex1,ex2] # a task is a list of input/outputs
x = ([4,4,2],) # input
y = [6,6,4] # output
ex1 = (x,y) # a single input/output example
task2 = [ex1] # a task is a list of input/outputs
assert fe.forward_one_task(task1).shape == torch.Size([H])
# batched forward pass - test cases
assert fe.forward([task1,task2]).shape == torch.Size([2,H])
assert torch.all( fe.forward([task1,task2])[0] == fe.forward_one_task(task1) )
assert torch.all( fe.forward([task1,task2])[1] == fe.forward_one_task(task2) )
# pooling of examples happens through averages - check via this assert
assert(torch.stack([fe.forward_one_task(task1),fe.forward_one_task(task1)],0).mean(0) - fe.forward_one_task(task1)).abs().max() < 1e-5
assert(torch.stack([fe.forward_one_task(task1),fe.forward_one_task(task2)],0).mean(0) - fe.forward_one_task(task1)).abs().max() > 1e-5
tasks = [task1,task2]
PCFG_predictor.train(programs, tasks)
PCFG_predictor.test(programs, tasks)
Q_predictor.train(programs, tasks)
Q_predictor.test(programs, tasks)
from Louis.misc import *
from Louis.solutions import *
class TestARC(unittest.TestCase):
def test_predictions_with_inputs(self):
template_dsl = DSL(semantics, primitive_types, no_repetitions_DS)
type_request = Arrow(List(OBJ), List(OBJ))
template_cfg = template_dsl.DSL_to_CFG(type_request=type_request,
upper_bound_type_size=10,
max_program_depth=4,
min_variable_depth=1,
n_gram = 1)
list_variables = [
Variable(i, type_, probability={})
for i,type_ in enumerate(type_request.arguments())
]
PCFG_predictor = PCFG_Predictor(
Embedding(),
template_cfg=template_cfg
)
Q_predictor = Q_Predictor(
Embedding(),
template_dsl=template_dsl,
template_cfg=template_cfg,
list_variables=list_variables,
)
programs = [solutions[name] for name in solutions]
tasks = []
for name in solutions:
pb = json_read('Louis/ARC/data/training/'+name)
c_type = cohesions[name]
task = []
for mode in pb:
for pair in pb[mode]:
x = find_objects(pair['input'], c_type)
y = find_objects(pair['output'], c_type)
task.append((x, y))
tasks.append(task)
break
print('output_dimensionality: "=', Embedding()(tasks[:1]).shape)
# PCFG_predictor.train(programs, tasks)
# PCFG_predictor.test(programs, tasks)
Q_predictor.train(programs, tasks)
# Q_predictor.test(programs, tasks)
if __name__ == "__main__":
unittest.main(verbosity=2)