Exemple #1
0
def get_synth_lib():
    libSynth = FnLibrary()
    A = PPSortVar('A')
    B = PPSortVar('B')
    C = PPSortVar('C')

    tr5 = mkRealTensorSort([5])
    tb5 = mkBoolTensorSort([5])
    ti5 = mkIntTensorSort([5])
    ppint = PPInt()

    repeatEnum = PPEnumSort(10, 10)

    libSynth.addItems([
        PPLibItem('compose', func(func(B, C), func(A, B), func(A, C)), None),
        PPLibItem('map_l', func(func(A, B), func(lst(A), lst(B))), None),
        PPLibItem('fold_l', func(func(B, A, B), B, func(lst(A), B)), None),
        PPLibItem('conv_l', func(func(lst(A), B), func(lst(A), lst(B))), None),
        PPLibItem('conv_g', func(func(lst(A), B), func(graph(A), graph(B))),
                  None),
        PPLibItem('map_g', func(func(A, B), func(graph(A), graph(B))), None),
        PPLibItem('fold_g', func(func(B, A, B), B, func(graph(A), B)), None),
        PPLibItem('zeros', func(PPDimVar('a'), mkRealTensorSort([1, 'a'])),
                  None),
        PPLibItem('repeat', func(repeatEnum, func(A, A), func(A, A)), None),
        PPLibItem(
            'regress_speed_mnist',
            func(mkRealTensorSort([1, 3, 32, 32]), mkRealTensorSort([1, 2])),
            None),

        # PPLibItem('nav_mnist', func(mkGraphSort(mkRealTensorSort([1, 3, 32, 32])),
        #                             mkGraphSort(mkRealTensorSort([1, 2]))), None),
    ])

    return libSynth
Exemple #2
0
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)
Exemple #3
0
def getLib():
    libSynth = FnLibrary()
    A = PPSortVar('A')
    B = PPSortVar('B')
    C = PPSortVar('C')

    tr5 = mkRealTensorSort([5])
    tb5 = mkBoolTensorSort([5])
    ti5 = mkIntTensorSort([5])
    ppint = PPInt()

    cnts = PPEnumSort(2, 50)

    libSynth.addItems([
        PPLibItem('map', func(func(A, B), func(lst(A), lst(B))), None),
        PPLibItem('fold', func(func(B, A, B), B, func(lst(A), B)), None),
        PPLibItem('conv', func(func(A, lst(A), A), func(lst(A), lst(A))),
                  None),
        PPLibItem('compose', func(func(B, C), func(A, B), func(A, C)), None),
        PPLibItem('repeat', func(cnts, func(A, A), func(A, A)), None),
        PPLibItem('zeros', func(PPDimVar('a'), mkRealTensorSort([1, 'a'])),
                  None),
        PPLibItem('nn_fun_0', func(tr5, tr5), None),
        PPLibItem('nn_fun_1', func(tr5, tb5), None),
        PPLibItem('nn_fun_2', func(tb5, ti5), None),
    ])
    return libSynth
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))
Exemple #5
0
def testProgTreeSize():
    lib = mkDefaultLib()

    # region #+ Add recogFive to library
    inType = mkRealTensorSort([1, 1, 28, 28])
    outType = mkBoolTensorSort([1, 1])
    recogDigitType = mkFuncSort(inType, outType)

    lib.addItem(PPLibItem('recogFive', recogDigitType, None))
    # endregion

    inType = mkListSort(mkRealTensorSort([1, 1, 28, 28]))
    outType = mkRealTensorSort([1, 1])
    sort = mkFuncSort(inType, outType)

    prog = \
        PPFuncApp(fn=PPVar(name='lib.compose'),
                  args=[PPFuncApp(fn=PPVar(name='lib.fold_l'),
                                  args=[PPTermUnk(name='nn_fun_x_906',
                                                  sort=PPFuncSort(
                                                      args=[PPTensorSort(param_sort=PPReal(),
                                                                         shape=[PPDimConst(value=1),
                                                                                PPDimConst(value=1)]),
                                                            PPTensorSort(param_sort=PPBool(),
                                                                         shape=[PPDimConst(value=1),
                                                                                PPDimConst(value=1)])],
                                                      rtpe=PPTensorSort(param_sort=PPReal(),
                                                                        shape=[PPDimConst(value=1),
                                                                               PPDimConst(value=1)]))),
                                        PPFuncApp(fn=PPVar(name='lib.zeros'), args=[PPIntConst(value=1)])]),
                        PPFuncApp(fn=PPVar(name='lib.map_l'), args=[PPVar(name='lib.recogFive')])])

    size = ASTUtils.progTreeSize(prog)

    assert size == 7
Exemple #6
0
def testInfer5():
    lib = mkDefaultLib()

    # region #+ Add recogFive to library
    inType = mkRealTensorSort([1, 1, 28, 28])
    outType = mkBoolTensorSort([1, 1])
    recogDigitType = mkFuncSort(inType, outType)

    lib.addItem(PPLibItem('recogFive', recogDigitType, None))
    # endregion

    inType = mkRealTensorSort([1, 1, 28, 28])
    outType = mkRealTensorSort([1, 1])
    sort = mkFuncSort(inType, outType)

    prog = PPVar(name='lib.recogFive')

    sortInferred = inferType(prog, lib)

    print(repr_py_sort(sort))
    print(sort)

    print(repr_py_sort(sortInferred))
    print(sortInferred)

    assert sort == sortInferred
Exemple #7
0
def testMutate0():
    lib = mkDefaultLib()

    # region #+ Add recogFive to library
    inType = mkRealTensorSort([1, 1, 28, 28])
    outType = mkBoolTensorSort([1, 1])
    recogDigitType = mkFuncSort(inType, outType)

    lib.addItem(PPLibItem('recogFive', recogDigitType, None))
    # endregion

    inType = mkListSort(mkRealTensorSort([1, 1, 28, 28]))
    outType = mkListSort(mkBoolTensorSort([1, 1]))
    sort = mkFuncSort(inType, outType)

    prog = PPFuncApp(fn=PPVar(name='lib.map_l'),
                     args=[PPVar(name='lib.recogFive')])

    for i in range(100):
        newProg = mutate(prog, lib)
        if newProg is None:
            continue
        print(repr_py(newProg))

        isort = inferType(newProg, lib)
        assert isort is not None
        assert sort == isort
Exemple #8
0
def test_synthesizer_count_5s():
    libSynth = getLib()

    tr1_1_28_28 = mkRealTensorSort([1, 1, 28, 28])
    tr1_1 = mkRealTensorSort([1, 1])
    tb1_1 = mkBoolTensorSort([1, 1])

    libSynth.addItems([
        PPLibItem('recog_5', func(tr1_1_28_28, tb1_1), None),
    ])

    fn_sort = func(lst(tr1_1_28_28), tr1_1)

    synth = SymbolicSynthesizer(libSynth, fn_sort)

    I = 10000
    i = 0
    for prog, unkMap in synth.genProgs():

        i = i + 1
        if i > I:
            break
        if i % 100 == 0:
            print(i)

        unks = ASTUtils.getUnks(prog)

        if len(unks) > 1:
            continue

        for unk in unks:
            if ASTUtils.isAbstract(unk.sort):
                continue

        print(repr_py_ann(prog))
Exemple #9
0
def testInfer7():
    lib = mkDefaultLib()

    # region #+ Add recogFive to library
    inType = mkRealTensorSort([1, 1, 28, 28])
    outType = mkBoolTensorSort([1, 1])
    recogDigitType = mkFuncSort(inType, outType)

    lib.addItem(PPLibItem('recogFive', recogDigitType, None))
    # endregion

    prog = PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPTermUnk(
                name='Unk',
                sort=PPFuncSort(
                    args=[
                        PPFuncSort(
                            args=[
                                PPListSort(param_sort=PPSortVar(name='A_1'))
                            ],
                            rtpe=PPListSort(param_sort=PPSortVar(name='B')))
                    ],
                    rtpe=PPListSort(param_sort=PPTensorSort(
                        param_sort=PPBool(),
                        shape=[PPDimConst(
                            value=1), PPDimConst(value=1)])))),
            PPVar(name='lib.map_l')
        ])

    sortInferred = inferType(prog, lib)
    assert sortInferred is None
Exemple #10
0
def alphaConvertLibItem(libItem, term):
    libItemSort = renameSortVars(libItem.sort, term)
    libItemSort = renameDimVars(libItemSort, term)

    libItem = PPLibItem(name=libItem.name, sort=libItemSort, obj=libItem.obj)

    return libItem
Exemple #11
0
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)
Exemple #12
0
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)
Exemple #13
0
 def update_library(self, lib: FnLibrary,
                    task_result_single: TaskResultSingle, taskid):
     if self.seq_settings.update_library:
         # Add learned modules to the library
         top_solution = task_result_single.get_top_solution_details()
         if top_solution is not None:
             prog, resDict = top_solution
             unk_sort_map: Dict[str,
                                PPSort] = ASTUtils.getUnkNameSortMap(prog)
             lib_items = [
                 PPLibItem(unk, unk_sort, resDict['new_fns_dict'][unk])
                 for unk, unk_sort in unk_sort_map.items()
             ]
             if lib_items.__len__() > 0:
                 lib.addItems(lib_items)
             # Save the library.
             lib.save1(self.getLibLocation(), taskid)
Exemple #14
0
def test1():
    intSort = PPInt()
    boolSort = PPBool()

    libSynth = FnLibrary()
    libSynth.addItems([
        PPLibItem('itob', mkFuncSort(intSort, boolSort), None),
    ])
    ioExamples = None

    fnSort = PPFuncSort([intSort], boolSort)
    interpreter = None

    solver = SymbolicSynthesizer(interpreter, libSynth, fnSort, ioExamples)
    solver.setEvaluate(False)
    targetProg = PPVar('lib.itobX')
    count = solver.search(targetProg, 100)
    assert count == -1
def loadLibrary(libDirPath):
    libFilePath = libDirPath + '/' + 'lib.pickle'

    if not os.path.isfile(libFilePath):
        return None

    with open(libFilePath, 'rb') as fh:
        libDict = pickle.load(fh)

    lib = FnLibrary()
    for name, (li, isNN) in libDict.items():
        if isNN:
            modelFilePath = libDirPath + '/' + name + '.pth'
            obj = SaveableNNModule.create_and_load(libDirPath, name)
            lib.addItem(PPLibItem(name, li, obj))
        else:
            lib.addItem(get_item_from_repo(name))

    return lib
Exemple #16
0
def xtest2():
    intSort = PPInt()
    boolSort = PPBool()
    libSynth = FnLibrary()
    libSynth.addItems([
        PPLibItem('itob', mkFuncSort(intSort, boolSort), None),
    ])
    ioExamples = None

    fnSort = PPFuncSort([intSort], boolSort)
    interpreter = None

    solver = SymbolicSynthesizer(interpreter, libSynth, fnSort, ioExamples)
    solver.setEvaluate(False)
    targetProg = PPLambda(params=[PPVarDecl(name='x1', sort=PPInt())],
                          body=PPFuncApp(fn=PPVar('lib.itob'),
                                         args=[PPVar(name='x1')]))
    count = solver.search(targetProg, 100)
    assert count >= 0
def _debug_info(prog: PPTerm, unkSortMap, lib: FnLibrary, fnSort: PPSort):
    lib_items = [
        PPLibItem(li.name, li.sort, None) for (_, li) in lib.items.items()
    ]
    dprog = """
    io_examples_tr, io_examples_val = None, None
    prog = %s
    unkSortMap = %s
    lib = NewLibrary()
    lib.addItems(%s)
    fn_sort = %s
    interpreter = Interpreter(lib, epochs=1)
    res = interpreter.evaluate(program=prog,
                                        output_type_s=fn_sort.rtpe,
                                        unkSortMap=unkSortMap,
                                        io_examples_tr=io_examples_tr,
                                        io_examples_val=io_examples_val)
    """ % (str(prog), str(unkSortMap), str(lib_items), str(fnSort))

    return (NSDebugInfo(dprog), )
Exemple #18
0
def test_zeros():
    # IO Examples
    train, val = split_into_train_and_validation(0, 10)
    train_io_examples = get_batch_count_iseven(digits_to_count=[5],
                                               count_up_to=10,
                                               batch_size=100,
                                               digit_dictionary=train)
    val_io_examples = get_batch_count_iseven(digits_to_count=[5],
                                             count_up_to=10,
                                             batch_size=20,
                                             digit_dictionary=val)

    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

    # Library
    libSynth = FnLibrary()

    t = PPSortVar('T')
    t1 = PPSortVar('T1')
    t2 = PPSortVar('T2')

    libSynth.addItems([
        PPLibItem('zeros', mkFuncSort(PPDimVar('a'),
                                      mkRealTensorSort([1, 'a'])), pp_map),
        # PPLibItem('zeros2', mkFuncSort(PPDimVar('a'), PPDimVar('b'), mkRealTensorSort(['a', 'b'])), pp_map),
    ])

    fnSort = mkFuncSort(PPDimConst(2), mkRealTensorSort([2]))

    interpreter = Interpreter(libSynth)
    solver = SymbolicSynthesizer(interpreter, libSynth, fnSort,
                                 train_io_examples, val_io_examples)
    solver.setEvaluate(False)
    solution, score = solver.solve()
def synthesizeCountDigitProgram():
    lib = mkDefaultLib()

    inType = mkRealTensorSort([1, 1, 28, 28])
    outType = mkBoolTensorSort([1, 1])
    recogDigitType = mkFuncSort(inType, outType)

    lib.addItem(PPLibItem('recogFive', recogDigitType, None))

    inType = mkListSort(mkRealTensorSort([1, 1, 28, 28]))
    outType = mkRealTensorSort([1, 1])
    countDigitType = mkFuncSort(inType, outType)

    synth = SymbolicSynthesizer(lib, countDigitType, nnprefix='x')
    for i, (prog, unks) in enumerate(synth.genProgs()):
        pstr = repr_py(prog)
        if 'fold' in pstr:
            if 'recogFive' in pstr:
                if 'zero' in pstr:
                    print(i, pstr)
                    print(i, prog)
                    print(i, unks)
Exemple #20
0
def test_synthesizer_sum_digits():
    libSynth = getLib()

    input_type = mkListSort(mkRealTensorSort([1, 1, 28, 28]))
    output_type = mkRealTensorSort([1, 1])
    fn_sort = mkFuncSort(input_type, output_type)

    tr1_1_28_28 = mkRealTensorSort([1, 1, 28, 28])
    tb_1_10 = mkBoolTensorSort([1, 10])
    classify_digit = mkFuncSort(tr1_1_28_28, tb_1_10)

    libSynth.addItems([
        PPLibItem('classify_digit', classify_digit, None),
    ])

    synth = SymbolicSynthesizer(libSynth, fn_sort)

    I = 10000
    i = 0
    for prog, unkMap in synth.genProgs():

        i = i + 1
        if i > I:
            break
        if i % 100 == 0:
            print(i)

        unks = ASTUtils.getUnks(prog)

        if len(unks) > 1:
            continue

        for unk in unks:
            if ASTUtils.isAbstract(unk.sort):
                continue

        print(repr_py_ann(prog))
Exemple #21
0
def addRecogFive2(lib):
    inType = mkRealTensorSort([1, 1, 28, 28])
    outType = mkBoolTensorSort([1, 1])
    recogDigitType = mkFuncSort(inType, outType)
    lib.addItem(PPLibItem('recogFive2', recogDigitType, None))
Exemple #22
0
def main():
    tio, vio = get_io_examples_classify_digits(2000, 200)

    # Task Name: classify_digits
    prog = PPTermUnk(name='nn_fun_cs1cd_1', sort=PPFuncSort(args=[PPTensorSort(param_sort=PPReal(),
                                                                               shape=[PPDimConst(value=1),
                                                                                      PPDimConst(value=1),
                                                                                      PPDimConst(value=28),
                                                                                      PPDimConst(value=28)])],
                                                            rtpe=PPTensorSort(param_sort=PPBool(),
                                                                              shape=[PPDimConst(value=1),
                                                                                     PPDimConst(value=10)])))
    unkSortMap = {'nn_fun_cs1cd_1': PPFuncSort(args=[PPTensorSort(param_sort=PPReal(),
                                                                  shape=[PPDimConst(value=1), PPDimConst(value=1),
                                                                         PPDimConst(value=28), PPDimConst(value=28)])],
                                               rtpe=PPTensorSort(param_sort=PPBool(),
                                                                 shape=[PPDimConst(value=1), PPDimConst(value=10)]))}

    lib = FnLibrary()
    lib.addItems([PPLibItem(name='compose', sort=PPFuncSort(
        args=[PPFuncSort(args=[PPSortVar(name='B')], rtpe=PPSortVar(name='C')),
              PPFuncSort(args=[PPSortVar(name='A')], rtpe=PPSortVar(name='B'))],
        rtpe=PPFuncSort(args=[PPSortVar(name='A')], rtpe=PPSortVar(name='C'))), obj=None), PPLibItem(name='repeat',
                                                                                                     sort=PPFuncSort(
                                                                                                         args=[
                                                                                                             PPEnumSort(
                                                                                                                 start=8,
                                                                                                                 end=10),
                                                                                                             PPFuncSort(
                                                                                                                 args=[
                                                                                                                     PPSortVar(
                                                                                                                         name='A')],
                                                                                                                 rtpe=PPSortVar(
                                                                                                                     name='A'))],
                                                                                                         rtpe=PPFuncSort(
                                                                                                             args=[
                                                                                                                 PPSortVar(
                                                                                                                     name='A')],
                                                                                                             rtpe=PPSortVar(
                                                                                                                 name='A'))),
                                                                                                     obj=None),
                  PPLibItem(name='map_l',
                            sort=PPFuncSort(args=[PPFuncSort(args=[PPSortVar(name='A')], rtpe=PPSortVar(name='B'))],
                                            rtpe=PPFuncSort(args=[PPListSort(param_sort=PPSortVar(name='A'))],
                                                            rtpe=PPListSort(param_sort=PPSortVar(name='B')))),
                            obj=None), PPLibItem(name='fold_l', sort=PPFuncSort(
            args=[PPFuncSort(args=[PPSortVar(name='B'), PPSortVar(name='A')], rtpe=PPSortVar(name='B')),
                  PPSortVar(name='B')],
            rtpe=PPFuncSort(args=[PPListSort(param_sort=PPSortVar(name='A'))], rtpe=PPSortVar(name='B'))), obj=None),
                  PPLibItem(name='conv_l', sort=PPFuncSort(
                      args=[PPFuncSort(args=[PPListSort(param_sort=PPSortVar(name='A'))], rtpe=PPSortVar(name='B'))],
                      rtpe=PPFuncSort(args=[PPListSort(param_sort=PPSortVar(name='A'))],
                                      rtpe=PPListSort(param_sort=PPSortVar(name='B')))), obj=None),
                  PPLibItem(name='zeros', sort=PPFuncSort(args=[PPDimVar(name='a')],
                                                          rtpe=PPTensorSort(param_sort=PPReal(),
                                                                            shape=[PPDimConst(value=1),
                                                                                   PPDimVar(name='a')])), obj=None)])
    fn_sort = PPFuncSort(args=[PPTensorSort(param_sort=PPReal(),
                                            shape=[PPDimConst(value=1), PPDimConst(value=1), PPDimConst(value=28),
                                                   PPDimConst(value=28)])],
                         rtpe=PPTensorSort(param_sort=PPBool(), shape=[PPDimConst(value=1), PPDimConst(value=10)]))

    interpreter = Interpreter(lib, 150)
    res = interpreter.evaluate(program=prog,
                               output_type_s=fn_sort.rtpe,
                               unkSortMap=unkSortMap,
                               io_examples_tr=tio,
                               io_examples_val=vio)
Exemple #23
0
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)
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()
Exemple #25
0
def test_sum_digits():
    libSynth = getLib()

    input_type = mkListSort(mkRealTensorSort([1, 1, 28, 28]))
    output_type = mkRealTensorSort([1, 1])
    fn_sort = mkFuncSort(input_type, output_type)

    tr1_1_28_28 = mkRealTensorSort([1, 1, 28, 28])
    tb_1_10 = mkBoolTensorSort([1, 10])
    classify_digit = mkFuncSort(tr1_1_28_28, tb_1_10)

    libSynth.addItems([
        PPLibItem('classify_digit', classify_digit, None),
    ])

    t = PPTermNT("Z", fn_sort)
    assert (t == PPTermNT(
        name='Z',
        sort=PPFuncSort(args=[
            PPListSort(param_sort=PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=28),
                                                   PPDimConst(value=28)
                                               ]))
        ],
                        rtpe=PPTensorSort(
                            param_sort=PPReal(),
                            shape=[PPDimConst(value=1),
                                   PPDimConst(value=1)]))))
    printTerm(t)
    # (Z: (List[Tensor[real][1,1,28,28]] --> Tensor[real][1,1]))

    t = Rules.expandToFuncApp(libSynth, t, 1, 'compose')
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[PPSortVar(name='B')],
                                     rtpe=PPTensorSort(param_sort=PPReal(),
                                                       shape=[
                                                           PPDimConst(value=1),
                                                           PPDimConst(value=1)
                                                       ]))),
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPListSort(
                             param_sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=28),
                                                         PPDimConst(value=28)
                                                     ]))
                     ],
                                     rtpe=PPSortVar(name='B')))
        ]))
    printTerm(t)
    # lib.compose((Z: (B --> Tensor[real][1,1])), (Z: (List[Tensor[real][1,1,28,28]] --> B)))

    t = Rules.expandToFuncApp(libSynth, t, 1, 'fold')
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(fn=PPVar(name='lib.fold'),
                      args=[
                          PPTermNT(name='Z',
                                   sort=PPFuncSort(args=[
                                       PPTensorSort(param_sort=PPReal(),
                                                    shape=[
                                                        PPDimConst(value=1),
                                                        PPDimConst(value=1)
                                                    ]),
                                       PPSortVar(name='A')
                                   ],
                                                   rtpe=PPTensorSort(
                                                       param_sort=PPReal(),
                                                       shape=[
                                                           PPDimConst(value=1),
                                                           PPDimConst(value=1)
                                                       ]))),
                          PPTermNT(name='Z',
                                   sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1)
                                                     ]))
                      ]),
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPListSort(
                             param_sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=28),
                                                         PPDimConst(value=28)
                                                     ]))
                     ],
                                     rtpe=PPListSort(param_sort=PPSortVar(
                                         name='A'))))
        ]))

    printTerm(t)
    # lib.compose(lib.fold((Z: ((Tensor[real][1,1], A) --> Tensor[real][1,1])), (Z: Tensor[real][1,1])), (Z: (List[Tensor[real][1,1,28,28]] --> List[A])))

    t = Rules.expandToUnk(t, 1)
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(fn=PPVar(name='lib.fold'),
                      args=[
                          PPTermUnk(
                              name='Unk',
                              sort=PPFuncSort(args=[
                                  PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ]),
                                  PPSortVar(name='A')
                              ],
                                              rtpe=PPTensorSort(
                                                  param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
                          PPTermNT(name='Z',
                                   sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1)
                                                     ]))
                      ]),
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPListSort(
                             param_sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=28),
                                                         PPDimConst(value=28)
                                                     ]))
                     ],
                                     rtpe=PPListSort(param_sort=PPSortVar(
                                         name='A'))))
        ]))
    printTerm(t)
    # lib.compose(lib.fold((Unk: ((Tensor[real][1,1], A) --> Tensor[real][1,1])), (Z: Tensor[real][1,1])), (Z: (List[Tensor[real][1,1,28,28]] --> List[A])))

    t = Rules.expandToFuncApp(libSynth, t, 1, 'zeros')
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(
                fn=PPVar(name='lib.fold'),
                args=[
                    PPTermUnk(name='Unk',
                              sort=PPFuncSort(args=[
                                  PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ]),
                                  PPSortVar(name='A')
                              ],
                                              rtpe=PPTensorSort(
                                                  param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
                    PPFuncApp(
                        fn=PPVar(name='lib.zeros'),
                        args=[PPTermNT(name='Z', sort=PPDimConst(value=1))])
                ]),
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPListSort(
                             param_sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=28),
                                                         PPDimConst(value=28)
                                                     ]))
                     ],
                                     rtpe=PPListSort(param_sort=PPSortVar(
                                         name='A'))))
        ]))
    printTerm(t)
    # lib.compose(lib.fold((Unk: ((Tensor[real][1,1], A) --> Tensor[real][1,1])), lib.zeros((Z: 1))), (Z: (List[Tensor[real][1,1,28,28]] --> List[A])))

    t = Rules.expandDimConst(t, 1)
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(fn=PPVar(name='lib.fold'),
                      args=[
                          PPTermUnk(
                              name='Unk',
                              sort=PPFuncSort(args=[
                                  PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ]),
                                  PPSortVar(name='A')
                              ],
                                              rtpe=PPTensorSort(
                                                  param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
                          PPFuncApp(fn=PPVar(name='lib.zeros'),
                                    args=[PPIntConst(value=1)])
                      ]),
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPListSort(
                             param_sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=28),
                                                         PPDimConst(value=28)
                                                     ]))
                     ],
                                     rtpe=PPListSort(param_sort=PPSortVar(
                                         name='A'))))
        ]))
    printTerm(t)
    # lib.compose(lib.fold((Unk: ((Tensor[real][1,1], A) --> Tensor[real][1,1])), lib.zeros(1)), (Z: (List[Tensor[real][1,1,28,28]] --> List[A])))

    t = Rules.expandToFuncApp(libSynth, t, 1, 'map')
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(fn=PPVar(name='lib.fold'),
                      args=[
                          PPTermUnk(
                              name='Unk',
                              sort=PPFuncSort(args=[
                                  PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ]),
                                  PPSortVar(name='B')
                              ],
                                              rtpe=PPTensorSort(
                                                  param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
                          PPFuncApp(fn=PPVar(name='lib.zeros'),
                                    args=[PPIntConst(value=1)])
                      ]),
            PPFuncApp(fn=PPVar(name='lib.map'),
                      args=[
                          PPTermNT(name='Z',
                                   sort=PPFuncSort(args=[
                                       PPTensorSort(param_sort=PPReal(),
                                                    shape=[
                                                        PPDimConst(value=1),
                                                        PPDimConst(value=1),
                                                        PPDimConst(value=28),
                                                        PPDimConst(value=28)
                                                    ])
                                   ],
                                                   rtpe=PPSortVar(name='B')))
                      ])
        ]))
    printTerm(t)
    # lib.compose(lib.fold((Unk: ((Tensor[real][1,1], B) --> Tensor[real][1,1])), lib.zeros(1)), lib.map((Z: (Tensor[real][1,1,28,28] --> B))))

    t = Rules.expandToVar(libSynth, t, 1, 'classify_digit')
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(fn=PPVar(name='lib.fold'),
                      args=[
                          PPTermUnk(
                              name='Unk',
                              sort=PPFuncSort(args=[
                                  PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ]),
                                  PPTensorSort(param_sort=PPBool(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=10)
                                               ])
                              ],
                                              rtpe=PPTensorSort(
                                                  param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
                          PPFuncApp(fn=PPVar(name='lib.zeros'),
                                    args=[PPIntConst(value=1)])
                      ]),
            PPFuncApp(fn=PPVar(name='lib.map'),
                      args=[PPVar(name='lib.classify_digit')])
        ]))
    printTerm(t)
Exemple #26
0
def test_count_5s():
    libSynth = getLib()

    tr1_1_28_28 = mkRealTensorSort([1, 1, 28, 28])
    tr1_1 = mkRealTensorSort([1, 1])
    tb1_1 = mkBoolTensorSort([1, 1])

    libSynth.addItems([
        PPLibItem('recog_5', func(tr1_1_28_28, tb1_1), None),
    ])

    fn_sort = func(lst(tr1_1_28_28), tr1_1)

    t = PPTermNT("Z", fn_sort)
    assert (t == PPTermNT(
        name='Z',
        sort=PPFuncSort(args=[
            PPListSort(param_sort=PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=28),
                                                   PPDimConst(value=28)
                                               ]))
        ],
                        rtpe=PPTensorSort(
                            param_sort=PPReal(),
                            shape=[PPDimConst(value=1),
                                   PPDimConst(value=1)]))))
    printTerm(t)
    # (Z: (List[Tensor[real][1,1,28,28]] --> Tensor[real][1,1]))

    t = Rules.expandToFuncApp(libSynth, t, 1, 'compose')
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[PPSortVar(name='B')],
                                     rtpe=PPTensorSort(param_sort=PPReal(),
                                                       shape=[
                                                           PPDimConst(value=1),
                                                           PPDimConst(value=1)
                                                       ]))),
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPListSort(
                             param_sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=28),
                                                         PPDimConst(value=28)
                                                     ]))
                     ],
                                     rtpe=PPSortVar(name='B')))
        ]))
    printTerm(t)
    # lib.compose((Z: (B --> Tensor[real][1,1])), (Z: (List[Tensor[real][1,1,28,28]] --> B)))

    t = Rules.expandToFuncApp(libSynth, t, 1, 'fold')
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(fn=PPVar(name='lib.fold'),
                      args=[
                          PPTermNT(name='Z',
                                   sort=PPFuncSort(args=[
                                       PPTensorSort(param_sort=PPReal(),
                                                    shape=[
                                                        PPDimConst(value=1),
                                                        PPDimConst(value=1)
                                                    ]),
                                       PPSortVar(name='A')
                                   ],
                                                   rtpe=PPTensorSort(
                                                       param_sort=PPReal(),
                                                       shape=[
                                                           PPDimConst(value=1),
                                                           PPDimConst(value=1)
                                                       ]))),
                          PPTermNT(name='Z',
                                   sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1)
                                                     ]))
                      ]),
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPListSort(
                             param_sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=28),
                                                         PPDimConst(value=28)
                                                     ]))
                     ],
                                     rtpe=PPListSort(param_sort=PPSortVar(
                                         name='A'))))
        ]))
    printTerm(t)
    # lib.compose(lib.fold((Z: ((Tensor[real][1,1], A) --> Tensor[real][1,1])), (Z: Tensor[real][1,1])), (Z: (List[Tensor[real][1,1,28,28]] --> List[A])))

    t = Rules.expandToUnk(t, 1)
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(fn=PPVar(name='lib.fold'),
                      args=[
                          PPTermUnk(
                              name='Unk',
                              sort=PPFuncSort(args=[
                                  PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ]),
                                  PPSortVar(name='A')
                              ],
                                              rtpe=PPTensorSort(
                                                  param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
                          PPTermNT(name='Z',
                                   sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1)
                                                     ]))
                      ]),
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPListSort(
                             param_sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=28),
                                                         PPDimConst(value=28)
                                                     ]))
                     ],
                                     rtpe=PPListSort(param_sort=PPSortVar(
                                         name='A'))))
        ]))
    printTerm(t)
    # lib.compose(lib.fold((Unk: ((Tensor[real][1,1], A) --> Tensor[real][1,1])), (Z: Tensor[real][1,1])), (Z: (List[Tensor[real][1,1,28,28]] --> List[A])))

    t = Rules.expandToFuncApp(libSynth, t, 1, 'zeros')
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(
                fn=PPVar(name='lib.fold'),
                args=[
                    PPTermUnk(name='Unk',
                              sort=PPFuncSort(args=[
                                  PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ]),
                                  PPSortVar(name='A')
                              ],
                                              rtpe=PPTensorSort(
                                                  param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
                    PPFuncApp(
                        fn=PPVar(name='lib.zeros'),
                        args=[PPTermNT(name='Z', sort=PPDimConst(value=1))])
                ]),
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPListSort(
                             param_sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=28),
                                                         PPDimConst(value=28)
                                                     ]))
                     ],
                                     rtpe=PPListSort(param_sort=PPSortVar(
                                         name='A'))))
        ]))
    printTerm(t)
    # lib.compose(lib.fold((Unk: ((Tensor[real][1,1], A) --> Tensor[real][1,1])), lib.zeros((Z: 1))), (Z: (List[Tensor[real][1,1,28,28]] --> List[A])))

    t = Rules.expandDimConst(t, 1)
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(fn=PPVar(name='lib.fold'),
                      args=[
                          PPTermUnk(
                              name='Unk',
                              sort=PPFuncSort(args=[
                                  PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ]),
                                  PPSortVar(name='A')
                              ],
                                              rtpe=PPTensorSort(
                                                  param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
                          PPFuncApp(fn=PPVar(name='lib.zeros'),
                                    args=[PPIntConst(value=1)])
                      ]),
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPListSort(
                             param_sort=PPTensorSort(param_sort=PPReal(),
                                                     shape=[
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=1),
                                                         PPDimConst(value=28),
                                                         PPDimConst(value=28)
                                                     ]))
                     ],
                                     rtpe=PPListSort(param_sort=PPSortVar(
                                         name='A'))))
        ]))
    printTerm(t)
    # lib.compose(lib.fold((Unk: ((Tensor[real][1,1], A) --> Tensor[real][1,1])), lib.zeros(1)), (Z: (List[Tensor[real][1,1,28,28]] --> List[A])))

    t = Rules.expandToFuncApp(libSynth, t, 1, 'map')
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(fn=PPVar(name='lib.fold'),
                      args=[
                          PPTermUnk(
                              name='Unk',
                              sort=PPFuncSort(args=[
                                  PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ]),
                                  PPSortVar(name='B')
                              ],
                                              rtpe=PPTensorSort(
                                                  param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
                          PPFuncApp(fn=PPVar(name='lib.zeros'),
                                    args=[PPIntConst(value=1)])
                      ]),
            PPFuncApp(fn=PPVar(name='lib.map'),
                      args=[
                          PPTermNT(name='Z',
                                   sort=PPFuncSort(args=[
                                       PPTensorSort(param_sort=PPReal(),
                                                    shape=[
                                                        PPDimConst(value=1),
                                                        PPDimConst(value=1),
                                                        PPDimConst(value=28),
                                                        PPDimConst(value=28)
                                                    ])
                                   ],
                                                   rtpe=PPSortVar(name='B')))
                      ])
        ]))
    printTerm(t)
    # lib.compose(lib.fold((Unk: ((Tensor[real][1,1], B) --> Tensor[real][1,1])), lib.zeros(1)), lib.map((Z: (Tensor[real][1,1,28,28] --> B))))

    t = Rules.expandToVar(libSynth, t, 1, 'recog_5')
    assert (t == PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPFuncApp(fn=PPVar(name='lib.fold'),
                      args=[
                          PPTermUnk(
                              name='Unk',
                              sort=PPFuncSort(args=[
                                  PPTensorSort(param_sort=PPReal(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ]),
                                  PPTensorSort(param_sort=PPBool(),
                                               shape=[
                                                   PPDimConst(value=1),
                                                   PPDimConst(value=1)
                                               ])
                              ],
                                              rtpe=PPTensorSort(
                                                  param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
                          PPFuncApp(fn=PPVar(name='lib.zeros'),
                                    args=[PPIntConst(value=1)])
                      ]),
            PPFuncApp(fn=PPVar(name='lib.map'),
                      args=[PPVar(name='lib.recog_5')])
        ]))
    printTerm(t)
Exemple #27
0
def add_to_lib(fn_sort, libSynth, tname):
    libSynth.addItem(PPLibItem(tname, fn_sort, None))
Exemple #28
0
def main():
    io_train, io_val = get_io_examples_count_digit_occ(5, 1200, 1200)
    # Task Name: count_digit_occ_5s
    prog = PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPTermUnk(
                name='nn_fun_3254',
                sort=PPFuncSort(args=[
                    PPListSort(param_sort=PPListSort(param_sort=PPTensorSort(
                        param_sort=PPBool(),
                        shape=[PPDimConst(
                            value=1), PPDimConst(value=1)])))
                ],
                                rtpe=PPTensorSort(param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
            PPFuncApp(fn=PPVar(name='lib.conv_l'),
                      args=[
                          PPFuncApp(fn=PPVar(name='lib.map_l'),
                                    args=[PPVar(name='lib.nn_fun_1')])
                      ])
        ])
    unkSortMap = {
        'nn_fun_3254':
        PPFuncSort(args=[
            PPListSort(param_sort=PPListSort(param_sort=PPTensorSort(
                param_sort=PPBool(),
                shape=[PPDimConst(
                    value=1), PPDimConst(value=1)])))
        ],
                   rtpe=PPTensorSort(
                       param_sort=PPReal(),
                       shape=[PPDimConst(value=1),
                              PPDimConst(value=1)]))
    }
    lib = FnLibrary()
    lib.addItems([
        PPLibItem(name='compose',
                  sort=PPFuncSort(args=[
                      PPFuncSort(args=[PPSortVar(name='B')],
                                 rtpe=PPSortVar(name='C')),
                      PPFuncSort(args=[PPSortVar(name='A')],
                                 rtpe=PPSortVar(name='B'))
                  ],
                                  rtpe=PPFuncSort(args=[PPSortVar(name='A')],
                                                  rtpe=PPSortVar(name='C'))),
                  obj=None),
        PPLibItem(name='repeat',
                  sort=PPFuncSort(args=[
                      PPEnumSort(start=2, end=50),
                      PPFuncSort(args=[PPSortVar(name='A')],
                                 rtpe=PPSortVar(name='A'))
                  ],
                                  rtpe=PPFuncSort(args=[PPSortVar(name='A')],
                                                  rtpe=PPSortVar(name='A'))),
                  obj=None),
        PPLibItem(name='map_l',
                  sort=PPFuncSort(
                      args=[
                          PPFuncSort(args=[PPSortVar(name='A')],
                                     rtpe=PPSortVar(name='B'))
                      ],
                      rtpe=PPFuncSort(
                          args=[PPListSort(param_sort=PPSortVar(name='A'))],
                          rtpe=PPListSort(param_sort=PPSortVar(name='B')))),
                  obj=None),
        PPLibItem(
            name='fold_l',
            sort=PPFuncSort(args=[
                PPFuncSort(args=[PPSortVar(name='B'),
                                 PPSortVar(name='A')],
                           rtpe=PPSortVar(name='B')),
                PPSortVar(name='B')
            ],
                            rtpe=PPFuncSort(args=[
                                PPListSort(param_sort=PPSortVar(name='A'))
                            ],
                                            rtpe=PPSortVar(name='B'))),
            obj=None),
        PPLibItem(name='conv_l',
                  sort=PPFuncSort(
                      args=[
                          PPFuncSort(args=[
                              PPListSort(param_sort=PPSortVar(name='A'))
                          ],
                                     rtpe=PPSortVar(name='B'))
                      ],
                      rtpe=PPFuncSort(
                          args=[PPListSort(param_sort=PPSortVar(name='A'))],
                          rtpe=PPListSort(param_sort=PPSortVar(name='B')))),
                  obj=None),
        PPLibItem(name='zeros',
                  sort=PPFuncSort(args=[PPDimVar(name='a')],
                                  rtpe=PPTensorSort(param_sort=PPReal(),
                                                    shape=[
                                                        PPDimConst(value=1),
                                                        PPDimVar(name='a')
                                                    ])),
                  obj=None),
        PPLibItem(name='nn_fun_1',
                  sort=PPFuncSort(args=[
                      PPTensorSort(param_sort=PPReal(),
                                   shape=[
                                       PPDimConst(value=1),
                                       PPDimConst(value=1),
                                       PPDimConst(value=28),
                                       PPDimConst(value=28)
                                   ])
                  ],
                                  rtpe=PPTensorSort(param_sort=PPBool(),
                                                    shape=[
                                                        PPDimConst(value=1),
                                                        PPDimConst(value=1)
                                                    ])),
                  obj=None),
        PPLibItem(name='nn_fun_2230',
                  sort=PPFuncSort(args=[
                      PPTensorSort(param_sort=PPReal(),
                                   shape=[
                                       PPDimConst(value=1),
                                       PPDimConst(value=1),
                                       PPDimConst(value=28),
                                       PPDimConst(value=28)
                                   ])
                  ],
                                  rtpe=PPTensorSort(param_sort=PPBool(),
                                                    shape=[
                                                        PPDimConst(value=1),
                                                        PPDimConst(value=1)
                                                    ])),
                  obj=None)
    ])
    fn_sort = PPFuncSort(args=[
        PPListSort(param_sort=PPTensorSort(param_sort=PPReal(),
                                           shape=[
                                               PPDimConst(value=1),
                                               PPDimConst(value=1),
                                               PPDimConst(value=28),
                                               PPDimConst(value=28)
                                           ]))
    ],
                         rtpe=PPTensorSort(
                             param_sort=PPReal(),
                             shape=[PPDimConst(value=1),
                                    PPDimConst(value=1)]))

    print(ReprUtils.repr_py_ann(prog))
    print(ReprUtils.repr_py_sort(lib.items['nn_fun_1'].sort))
    NeuralSynthesizer.is_evaluable(prog)