def add_libitems_to_repo(): def func(*lst): return mkFuncSort(*lst) def lst(t): return mkListSort(t) A = PPSortVar('A') B = PPSortVar('B') C = PPSortVar('C') real_tensor_2d = mkTensorSort(PPReal(), ['a', 'b']) bool_tensor_2d = mkTensorSort(PPBool(), ['a', 'b']) add_lib_item( PPLibItem('compose', func(func(B, C), func(A, B), func(A, C)), pp_compose)) add_lib_item( PPLibItem('repeat', func(PPEnumSort(9, 10), func(A, A), func(A, A)), pp_repeat)) add_lib_item( PPLibItem('map_l', func(func(A, B), func(lst(A), lst(B))), pp_map_list)) add_lib_item( PPLibItem('fold_l', func(func(B, A, B), B, func(lst(A), B)), pp_reduce_list)) add_lib_item( PPLibItem('conv_l', func(func(lst(A), B), func(lst(A), lst(B))), pp_conv_list)) add_lib_item( PPLibItem('zeros', func(PPDimVar('a'), mkRealTensorSort([1, 'a'])), pp_get_zeros)) def graph(t): return mkGraphSort(t) add_lib_item( PPLibItem('conv_g', func(func(lst(A), B), func(graph(A), graph(B))), pp_conv_graph)) add_lib_item( PPLibItem('map_g', func(func(A, B), func(graph(A), graph(B))), pp_map_g)) add_lib_item( PPLibItem('fold_g', func(func(B, A, B), B, func(graph(A), B)), pp_reduce_graph)) add_lib_item( PPLibItem('flatten_2d_list', func(func(B, C), func(A, B), func(A, C)), pp_flatten_2d_list))
def test3(): t123 = mkTensorSort(PPInt(), [1, 2, 3]) t12 = mkTensorSort(PPInt(), [1, 2]) libSynth = FnLibrary() libSynth.addItems([ PPLibItem('one', mkFuncSort(t123, t12), None), ]) ioExamples = None fnSort = PPFuncSort([t123], t12) interpreter = None solver = SymbolicSynthesizer(interpreter, libSynth, fnSort, ioExamples) solver.setEvaluate(False) solution, score = solver.solve() print(solution) print(score)
def test5(): def mk_recognise_5s(): res = NetCNN("recognise_5s", input_ch=1, output_dim=1, output_activation=F.sigmoid) res.load('../Interpreter/Models/is5_classifier.pth.tar') return res libSynth = FnLibrary() t = PPSortVar('T') t1 = PPSortVar('T1') t2 = PPSortVar('T2') libSynth.addItems([ PPLibItem( 'recognise_5s', mkFuncSort(mkTensorSort(PPReal(), ['a', 1, 28, 28]), mkTensorSort(PPReal(), ['a', 1])), mk_recognise_5s()), PPLibItem( 'map', mkFuncSort(mkFuncSort(t1, t2), mkListSort(t1), mkListSort(t2)), pp_map), ]) ioExamples = None img = mkRealTensorSort([1, 1, 28, 28]) imgList = mkListSort(img) isFive = mkRealTensorSort([1, 1]) imgToIsFive = mkFuncSort(img, isFive) isFiveList = mkListSort(isFive) fnSort = mkFuncSort(imgList, isFiveList) interpreter = None """targetProg = lambda inputs: map(lib.recognise_5s, inputs)""" solver = SymbolicSynthesizer(interpreter, libSynth, fnSort, ioExamples, ioExamples) solver.setEvaluate(False) # TODO: use "search" instead of "solve" solution, score = solver.solve() print(solution) print(score)
def test0(): tensorA3 = mkTensorSort(PPInt(), ['A', 3]) tensor2B = mkTensorSort(PPInt(), [2, 'B']) l1 = [tensorA3] l2 = [tensor2B] subst = unifyLists(l1, l2) print(subst) # [(PPDimVar(name='A'), PPDimConst(value=2)), (PPDimVar(name='B'), PPDimConst(value=3))] r1 = [applySubst(subst, x) for x in l1] r2 = [applySubst(subst, x) for x in l2] assert r1 == [ PPTensorSort(param_sort=PPInt(), shape=[PPDimConst(value=2), PPDimConst(value=3)]) ] assert r1 == r2
def xtest4(): t123 = mkTensorSort(PPInt(), [1, 2, 3]) t333 = mkTensorSort(PPInt(), [3, 3, 3]) tabc = mkTensorSort(PPInt(), ['a', 'b', 'c']) tabcc = mkTensorSort(PPInt(), ['a', 'b', 'c', 'd']) tabcd = mkTensorSort(PPInt(), ['a', 'b', 'c', 'd']) tddd = mkTensorSort(PPInt(), ['d', 'd', 'd']) libSynth = FnLibrary() libSynth.addItems([ PPLibItem('one', mkFuncSort(tabc, tabcc), None), ]) libSynth.addItems([ PPLibItem('two', mkFuncSort(tabcd, tddd), None), ]) ioExamples = None fnSort = PPFuncSort([t123], t333) interpreter = None solver = SymbolicSynthesizer(interpreter, libSynth, fnSort, ioExamples) solver.setEvaluate(False) solution, score = solver.solve() print(solution) print(score)
def addImageFunctionsToLibrary(libSynth: FnLibrary, load_recognise_5s=True): real_tensor_2d = mkTensorSort(PPReal(), ['a', 'b']) bool_tensor_2d = mkTensorSort(PPBool(), ['a', 'b']) libSynth.addItems([ PPLibItem('add', mkFuncSort(real_tensor_2d, real_tensor_2d, real_tensor_2d), pp_add), PPLibItem('add1', mkFuncSort(real_tensor_2d, bool_tensor_2d, real_tensor_2d), pp_add), PPLibItem( 'map', mkFuncSort(mkFuncSort(t1, t2), mkListSort(t1), mkListSort(t2)), pp_map), PPLibItem( 'map2d', mkFuncSort(mkFuncSort(t1, t2), mkListSort(mkListSort(t1)), mkListSort(mkListSort(t2))), pp_map2d), # question ^ should we transform map's definition into using vectors? is this not enough? # we don't know the type of the tensor output, w/o knowing the function. # PPLibItem('cat', mkFuncSort(mkTensorSort(PPReal(), ['a', 'b']), mkTensorSort(PPReal(), ['a', 'c']), # mkTensorSort(PPReal(), ['a', 'd'])), pp_cat), # TODO: d = b + c # Question: can we write 'b+c'? I'm not sure if it's useful # Also, the input types don't have to be PPReal, but for not it should suffice to just leave it like this? # ^ It can accept a tuple of tensors of different shapes, but maybe we can restrict it to tuple of 2 for now. # PPLibItem('zeros', mkFuncSort(PPInt(), mkTensorSort(PPReal(), ['a', 'b']), mkTensorSort(PPReal(), ['a', 'c'])), pp_get_zeros), # PPLibItem('zeros', mkFuncSort(PPInt(), PPInt(), mkTensorSort(PPReal(), ['a', 'c'])), pp_get_zeros), # 4, [2, 5] -> [2, 4] # 7, [2, 5] -> [2, 7] # Question: How do we say that the ints are the same number, PPInt() == 'c' # Also, The input tensor type doesn't have to be PPReal, can be int or bool as well # Also, the input tensor can be of any type, doesn't need to be float PPLibItem('zeros', mkFuncSort(PPDimVar('a'), mkRealTensorSort([1, 'a'])), pp_get_zeros), PPLibItem('reduce_general', mkFuncSort(mkFuncSort(t, t1, t), mkListSort(t1), t, t), pp_reduce), PPLibItem('reduce', mkFuncSort(mkFuncSort(t, t, t), mkListSort(t), t), pp_reduce), # pp_get_zeros # PPLibItem('reduce_with_init_zeros', mkFuncSort(mkFuncSort(t, t1, t), mkListSort(t1), t), pp_reduce_w_zeros_init), # Question : the initializer is only optional. How do we encode this information? # The following are just test functions for evaluation, not properly typed. # ,PPLibItem('mult_range09', mkFuncSort(mkFuncSort(t, t1, t), mkListSort(t1), t, t), get_multiply_by_range09()) # ,PPLibItem('argmax', mkFuncSort(mkFuncSort(t, t1, t), mkListSort(t1), t, t), argmax) # PPLibItem('split', mkFuncSort(PPImageSort(), mkListSort(PPImageSort())), split), # PPLibItem('join', mkFuncSort(mkListSort(PPImageSort()), PPImageSort()), None), ]) if load_recognise_5s: libSynth.addItems([ PPLibItem( 'recognise_5s', mkFuncSort(mkTensorSort(PPReal(), ['a', 1, 28, 28]), mkTensorSort(PPBool(), ['a', 1])), mk_recognise_5s()) ]) # set the neural libraries to evaluation mode # TODO: need to make sure we're properly switching between eval and train everywhere libSynth.recognise_5s.eval()
def test6(): t = PPSortVar('T') t1 = PPSortVar('T1') t2 = PPSortVar('T2') def mk_recognise_5s(): res = NetCNN("recognise_5s", input_ch=1, output_dim=1, output_activation=F.sigmoid) res.load('../Interpreter/Models/is5_classifier.pth.tar') return res libSynth = FnLibrary() real_tensor_2d = mkTensorSort(PPReal(), ['a', 'b']) libSynth.addItems([ PPLibItem( 'recognise_5s', mkFuncSort(mkTensorSort(PPReal(), ['a', 1, 28, 28]), mkTensorSort(PPReal(), ['a', 1])), mk_recognise_5s()), PPLibItem( 'map', mkFuncSort(mkFuncSort(t1, t2), mkListSort(t1), mkListSort(t2)), pp_map), PPLibItem('reduce', mkFuncSort(mkFuncSort(t, t, t), mkListSort(t), t), pp_reduce), PPLibItem('add', mkFuncSort(real_tensor_2d, real_tensor_2d, real_tensor_2d), lambda x, y: x + y), ]) train, val = split_into_train_and_validation(0, 10) val_ioExamples = get_batch_count_iseven(digits_to_count=[5], count_up_to=10, batch_size=20, digit_dictionary=val) img = mkRealTensorSort([1, 1, 28, 28]) isFive = mkRealTensorSort([1, 1]) imgToIsFive = mkFuncSort(img, isFive) imgList = mkListSort(img) isFiveList = mkListSort(isFive) sumOfFives = mkRealTensorSort([1, 1]) fnSort = mkFuncSort(imgList, sumOfFives) interpreter = Interpreter(libSynth) """ targetProg = lambda inputs. reduce( add, map(lib.recognise_5s, inputs)) """ # TODO: use "search" instead of "solve" solver = SymbolicSynthesizer(interpreter, libSynth, fnSort, val_ioExamples, val_ioExamples) # solver.setEvaluate(False) solution, score = solver.solve() print(solution) print(score)