예제 #1
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
예제 #2
0
    def __init__(self, settings, digit, seq, dbg_learn_parameters):
        input_type = mkRealTensorSort([1, 1, 28, 28])
        output_type = mkBoolTensorSort([1, 1])
        fn_sort = mkFuncSort(input_type, output_type)

        super(RecognizeDigitTask, self).__init__(fn_sort, settings, seq, dbg_learn_parameters)
        self.digit = digit
예제 #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
예제 #4
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
예제 #5
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))
예제 #6
0
    def _mkNSynth(self):
        ea_synthesis_mode = self.settings.synthesizer == 'evolutionary'
        interpreter = Interpreter(self.seq.lib,
                                  epochs=self.settings.epochs,
                                  batch_size=self.settings.batch_size)
        nnprefix = self.seq.sname() + self.sname()

        if self.settings.synthesizer == 'enumerative':
            # concrete_types = [mkRealTensorSort([1, 64, 4, 4]), mkRealTensorSort([1, 50])]
            concreteTypes = [
                mkRealTensorSort([1, 64, 4, 4]),
                mkBoolTensorSort([1, 1]),
                mkRealTensorSort([1, 50])
            ]
            synth = SymbolicSynthesizer(self.seq.lib, self.fn_sort, nnprefix,
                                        concreteTypes)

            ns_settings = NeuralSynthesizerSettings(self.settings.N,
                                                    self.settings.M,
                                                    self.settings.K)
            assert self.seq.lib is not None
            nsynth = NeuralSynthesizer(interpreter, synth, self.seq.lib,
                                       self.fn_sort,
                                       self.settings.dbg_learn_parameters,
                                       ns_settings)
            return nsynth
        elif self.settings.synthesizer == 'evolutionary':
            concreteTypes = [
                mkRealTensorSort([1, 64, 4, 4]),
                mkBoolTensorSort([1, 1]),
                mkRealTensorSort([1, 50])
            ]
            synth = SymbolicSynthesizerEA(self.seq.lib, self.fn_sort, nnprefix,
                                          concreteTypes)

            # TODO: Do not hardcode G
            NUM_GENERATIIONS = 100
            ns_settings = NeuralSynthesizerEASettings(G=NUM_GENERATIIONS,
                                                      M=self.settings.M,
                                                      K=self.settings.K)
            assert self.seq.lib is not None
            nsynth = NeuralSynthesizerEA(interpreter, synth, self.seq.lib,
                                         self.fn_sort, ns_settings)
            return nsynth
예제 #7
0
def main():
    input_type = mkRealTensorSort([1, 1, 28, 28])
    output_type = mkBoolTensorSort([1, 1])
    fn_sort = mkFuncSort(input_type, output_type)
    lib = mkDefaultLib()
    pg = ProgramGenerator(lib)
    progs = []
    while len(progs) < 30:
        prog = pg.genProg(fn_sort)
        if prog is not None and prog not in progs:
            print(repr_py(prog))
            progs.append(prog)
        else:
            pass
예제 #8
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))
예제 #9
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)
예제 #10
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)
예제 #11
0
def test_fold():
    tr15 = mkRealTensorSort([1, 5])
    tb51 = mkBoolTensorSort([1, 5])
    ti15 = mkIntTensorSort([1, 5])

    libSynth = getLib()

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

    printTerm(t)
    # lib.fold((Z: ((Tensor[real][1,5], Tensor[real][1,5]) --> Tensor[real][1,5])), (Z: Tensor[real][1,5]))
    t = Rules.expandToFuncApp(libSynth, t, 2, 'zeros')

    assert (t == PPFuncApp(
        fn=PPVar(name='lib.fold'),
        args=[
            PPTermNT(name='Z',
                     sort=PPFuncSort(args=[
                         PPTensorSort(
                             param_sort=PPReal(),
                             shape=[PPDimConst(value=1),
                                    PPDimConst(value=5)]),
                         PPTensorSort(
                             param_sort=PPReal(),
                             shape=[PPDimConst(value=1),
                                    PPDimConst(value=5)])
                     ],
                                     rtpe=PPTensorSort(param_sort=PPReal(),
                                                       shape=[
                                                           PPDimConst(value=1),
                                                           PPDimConst(value=5)
                                                       ]))),
            PPFuncApp(fn=PPVar(name='lib.zeros'),
                      args=[PPTermNT(name='Z', sort=PPDimConst(value=5))])
        ]))

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

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

    printTerm(t)
예제 #12
0
        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


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


def test_repeat():
    libSynth = getLib()
    t = PPTermNT("Z", func(tr5, tr5))
    assert (t == PPTermNT(name='Z',
                          sort=PPFuncSort(args=[
                              PPTensorSort(param_sort=PPReal(),
                                           shape=[PPDimConst(value=5)])
                          ],
                                          rtpe=PPTensorSort(
                                              param_sort=PPReal(),
                                              shape=[PPDimConst(value=5)]))))
    printTerm(t)
예제 #13
0
def addRecogFive2(lib):
    inType = mkRealTensorSort([1, 1, 28, 28])
    outType = mkBoolTensorSort([1, 1])
    recogDigitType = mkFuncSort(inType, outType)
    lib.addItem(PPLibItem('recogFive2', recogDigitType, None))
예제 #14
0
def test_search_space():
    # type shortcuts
    timg = mkRealTensorSort([1, 1, 28, 28])
    treal = mkRealTensorSort([1, 1])
    tbool = mkBoolTensorSort([1, 1])
    tbool10 = mkBoolTensorSort([1, 10])
    treal2 = mkRealTensorSort([1, 2])

    def mklst(t):
        return mkListSort(t)

    def mkfn(t1, t2):
        return mkFuncSort(t1, t2)

    def mkgr(t):
        return mkGraphSort(t)

    targetSize = 6

    print('start')
    print('targetSize = %d' % targetSize)
    add_sort = mkFuncSort(treal, tbool, treal)

    testSeqs = ['cs1']

    if 'cs1' in testSeqs:
        sname = 'cs1'
        libSynth = getBaseLibrary()

        tname = sname + '_recog_digit_d1'
        print(tname)
        fn_sort = mkfn(timg, tbool)
        print_progs(fn_sort, libSynth, targetSize)
        lfn = fn_sort
        add_to_lib(lfn, libSynth, tname)

        tname = sname + '_recog_digit_d2'
        print(tname)
        fn_sort = mkfn(timg, tbool)
        print_progs(fn_sort, libSynth, targetSize)
        lfn = fn_sort
        add_to_lib(lfn, libSynth, tname)

        tname = sname + '_count_digit_d1'
        print(tname)
        fn_sort = mkfn(mklst(timg), treal)
        print_progs(fn_sort, libSynth, targetSize)
        lfn = add_sort
        add_to_lib(lfn, libSynth, 'nn_add')

        tname = sname + '_count_digit_d2'
        print(tname)
        fn_sort = mkfn(mklst(timg), treal)
        print_progs(fn_sort, libSynth, targetSize)

    if 'cs2' in testSeqs:
        sname = 'cs2'
        libSynth = getBaseLibrary()

        tname = sname + '_recog_digit_d1'
        print(tname)
        fn_sort = mkfn(timg, tbool)
        print_progs(fn_sort, libSynth, targetSize)
        lfn = fn_sort
        add_to_lib(lfn, libSynth, tname)

        tname = sname + '_count_digit_d1'
        print(tname)
        fn_sort = mkfn(mklst(timg), treal)
        print_progs(fn_sort, libSynth, targetSize)
        lfn = add_sort
        add_to_lib(fn_sort, libSynth, 'nn_add')

        tname = sname + '_count_digit_d2'
        print(tname)
        fn_sort = mkfn(mklst(timg), treal)
        print_progs(fn_sort, libSynth, targetSize)
        lfn = mkfn(timg, tbool)
        add_to_lib(fn_sort, libSynth, 'nn_recog_digit_d2')

        tname = sname + '_recog_digit_d2'
        print(tname)
        fn_sort = mkfn(timg, tbool)
        print_progs(fn_sort, libSynth, targetSize)

    if 'ss' in testSeqs:
        sname = 'ss'
        libSynth = getBaseLibrary()

        tname = sname + '_classify_digit'
        print(tname)
        fn_sort = mkfn(timg, tbool10)
        print_progs(fn_sort, libSynth, targetSize)
        lfn = fn_sort
        add_to_lib(fn_sort, libSynth, tname)

        tname = sname + '_sum_digits'
        print(tname)
        fn_sort = mkfn(mklst(timg), treal)
        print_progs(fn_sort, libSynth, targetSize)

    if 'gs1' in testSeqs:
        sname = 'gs1'
        libSynth = getBaseLibrary()

        tname = sname + '_regress_speed'
        print(tname)
        fn_sort = mkfn(timg, treal2)
        print_progs(fn_sort, libSynth, targetSize)
        lfn = fn_sort
        add_to_lib(fn_sort, libSynth, tname)

        tname = sname + '_shortest_path_street'
        print(tname)
        fn_sort = mkfn(mkgr(timg), mkgr(treal2))
        print_progs(fn_sort, libSynth, targetSize)

    if 'gs2' in testSeqs:
        sname = 'gs2'
        libSynth = getBaseLibrary()

        tname = sname + '_regress_mnist'
        print(tname)
        fn_sort = mkfn(timg, treal2)
        # print_progs(fn_sort, libSynth, targetSize)
        lfn = fn_sort
        add_to_lib(lfn, libSynth, tname)

        tname = sname + '_shortest_path_mnist'
        print(tname)
        fn_sort = mkfn(mkgr(timg), mkgr(treal2))
        # print_progs(fn_sort, libSynth, targetSize)
        lfn = mkfn(mklst(treal2), treal2)
        add_to_lib(lfn, libSynth, 'nn_relax')

        tname = sname + '_shortest_path_street'
        print(tname)
        fn_sort = mkfn(mkgr(timg), mkgr(treal2))
        print_progs(fn_sort, libSynth, targetSize)