Exemplo n.º 1
0
def make_inteq(spec, context, scope, type_def):
    context['depth'] += 1
    arg1, context, ok1 = mk_expression_of_type(spec, context, scope, IntType())
    arg2, context, ok2 = mk_expression_of_type(spec, context, scope, IntType())
    context['depth'] -= 1
    return Expression(constructor='inteq',
                      type_def=IntType(),
                      arg1=arg1,
                      arg2=arg2), context, ok1 and ok2
Exemplo n.º 2
0
def make_app_of_type(spec, context, scope, type_def):
    arg_type = ForallType('a')
    fun_type = FunType(arg_type, type_def)
    context['depth'] += 1
    fun, context, ok1 = mk_expression_of_type(spec, context, scope, fun_type)
    arg_type = fun.type_def.in_type
    arg, context, ok2 = mk_expression_of_type(spec, context, scope, arg_type)
    context['depth'] -= 1
    return Expression(constructor='app', type_def=type_def, fun=fun,
                      arg=arg), context, ok1 and ok2
Exemplo n.º 3
0
def make_ifthenelse_of_type(spec, context, scope, type_def):
    context['depth'] += 1
    prop, context, ok0 = mk_expression_of_type(spec, context, scope,
                                               BoolType())
    true_branch, context, ok1 = mk_expression_of_type(spec, context, scope,
                                                      type_def)
    false_branch, context, ok2 = mk_expression_of_type(spec, context, scope,
                                                       type_def)
    context['depth'] -= 1
    return Expression(constructor='ifthenelse',
                      type_def=type_def,
                      prop=prop,
                      true_branch=true_branch,
                      false_branch=false_branch), context, ok0 and ok1 and ok2
Exemplo n.º 4
0
def make_lam_of_type(spec, context, scope, type_def):
    #typedef must be function(a, b)
    in_type = type_def.in_type
    out_type = type_def.out_type
    name, context = mk_new_name_of_type(spec, context, scope, in_type)
    new_scope = set(scope)
    new_scope.add(name)
    context['depth'] += 1
    body, context, ok = mk_expression_of_type(spec, context, new_scope,
                                              out_type)
    context['depth'] -= 1
    return Expression(constructor='lam',
                      type_def=type_def,
                      name=name,
                      body=body), context, ok
Exemplo n.º 5
0
def mk_expression_of_type(spec, context, scope, type_def):
    if context['depth'] > context['max_depth']:
        return Expression('depth_exceeded', type_def=type_def), context, False
    rule_ndxs = [
        i for i in range(len(spec['rules']))
        if spec['rules'][i].can_make(scope, type_def)
    ]
    vrule_ndxs = Variable(np.array(rule_ndxs, dtype=np.int32))
    rule_embeddings = context['model'].rule_embeddings(vrule_ndxs)
    rule_lprobs = F.matmul(rule_embeddings, F.transpose(context['state']))
    normalizer = context['model'].normalize(rule_lprobs)
    rule_lprobs = rule_lprobs - F.BroadcastTo((len(rule_ndxs), 1))(normalizer)
    rps = np.exp(rule_lprobs.data)[:, 0]
    rps /= np.sum(rps)
    ndx = np.random.choice(range(len(rps)), p=rps)
    lp = rule_lprobs[ndx, :]
    rule_ndx = rule_ndxs[ndx]
    rule_embedding = rule_embeddings[[ndx], :]
    rule = spec['rules'][rule_ndx]
    context['lp'] += lp
    context['state'] = context['model'].state2state(
        context['state']) + context['model'].choice2state(rule_embedding)
    context['state'] = F.tanh(context['state'])
    return rule.make_expr(spec, context, scope, type_def)
Exemplo n.º 6
0
def make_var_of_type(spec, context, scope, type_def):
    name, context = choose_var_of_type(spec, context, scope, type_def)
    return Expression(constructor='var', type_def=type_def,
                      name=name), context, True
Exemplo n.º 7
0
def make_int_negate(spec, context, scope, type_def):
    context['depth'] += 1
    arg, context, ok = mk_expression_of_type(spec, context, scope, IntType())
    context['depth'] -= 1
    return Expression(constructor='int_negate', type_def=IntType(),
                      arg=arg), context, ok
Exemplo n.º 8
0
def make_int(spec, context, scope, type_def):
    val, context = mk_new_int(spec, context)
    return Expression(constructor='int', type_def=IntType(),
                      val=val), context, True
Exemplo n.º 9
0
def make_not(spec, context, scope, type_def):
    context['depth'] += 1
    arg, context, ok = mk_expression_of_type(spec, context, scope, BoolType())
    context['depth'] -= 1
    return Expression(constructor='not', type_def=BoolType(),
                      arg=arg), context, ok
Exemplo n.º 10
0
def make_bool(spec, context, scope, type_def):
    val, context = mk_new_bool(spec, context)
    return Expression(constructor='bool', type_def=BoolType(),
                      val=val), context, True
Exemplo n.º 11
0
 optimizer.zero_grads()
 n_ok = 0
 tries = 0
 while not (n_ok >= 10 and tries >= 100):
     context['depth'] = 0
     context['state'] = model.init_state(
         Variable(np.array([0], dtype=np.int32)))
     context['lp'] = 0
     e, context, ok = mk_expression_of_type(
         spec, context, set(),
         FunType(IntType(), FunType(IntType(), IntType())))
     errs = []
     if ok:
         print '{:>10}'.format(tries), e.pstr()
         for x1, x2, y in zip(x1s, x2s, ys):
             x1e = Expression(constructor='int', type_def=IntType(), val=x1)
             x2e = Expression(constructor='int', type_def=IntType(), val=x2)
             outer_app = Expression(constructor='app',
                                    type_def=FunType(IntType(), IntType()),
                                    fun=e,
                                    arg=x1e)
             inner_app = Expression(constructor='app',
                                    type_def=IntType(),
                                    fun=outer_app,
                                    arg=x2e)
             yhat = app.eval_expr(eval_spec, inner_app, empty_env())
             errs.append((yhat - y)**2)
         n_ok += 1
     tries += 1
     lcs.append(LossComputer(ok, context['lp'], errs))
 ok_errs = [lc.errs for lc in lcs if lc.ok]