예제 #1
0
def __venture_start__(ripl):
    ripl.execute_program('''
        assume gp_cov_wn = (c) -> {
            gp_cov_scale(c, gp_cov_bump(1e-9, 1e-11))
        };
        define gp_cov_wn = (c) -> {
            gp_cov_scale(c, gp_cov_bump(1e-9, 1e-11))
        };
    ''')
    ripl.bind_foreign_inference_sp(
        'sort',
        deterministic_typed(np.sort, [
            vt.ArrayUnboxedType(vt.NumberType()),
        ],
                            vt.ArrayUnboxedType(vt.NumberType()),
                            min_req_args=1))
    ripl.bind_foreign_inference_sp(
        'get_mean',
        deterministic_typed(np.mean, [
            vt.ArrayUnboxedType(vt.NumberType()),
        ],
                            vt.NumberType(),
                            min_req_args=1))
    ripl.bind_foreign_inference_sp(
        'load_csv',
        deterministic_typed(load_csv, [vt.StringType()],
                            vt.ArrayUnboxedType(vt.NumberType()),
                            min_req_args=1))
    ripl.bind_foreign_sp(
        'compile_ast_to_venturescript',
        deterministic_typed(compile_ast_to_embedded_dsl, [vt.AnyType()],
                            vt.StringType(),
                            min_req_args=1))
    ripl.bind_foreign_sp(
        'eval_expr',
        deterministic_typed(interpret_embedded_dsl, [vt.StringType()],
                            gp.gpType,
                            min_req_args=1))
예제 #2
0
def __venture_start__(ripl):
    start = time.time()
    # NOTE: these are all currently inference SPs
    ripl.bind_foreign_inference_sp(
        "make_symbol",
        deterministic_typed(make_name,
                            [t.SymbolType(), t.NumberType()], t.SymbolType()))
    ripl.bind_foreign_inference_sp(
        "logsumexp",
        deterministic_typed(logsumexp, [t.ArrayUnboxedType(t.NumberType())],
                            t.NumberType()))
    ripl.bind_foreign_inference_sp(
        "concatenate",
        deterministic_typed(concatenate, [
            t.ArrayUnboxedType(t.NumberType()),
            t.ArrayUnboxedType(t.NumberType())
        ], t.ArrayUnboxedType(t.NumberType())))
    ripl.bind_foreign_inference_sp(
        "sum",
        deterministic_typed(sum_sp, [t.ArrayUnboxedType(t.NumberType())],
                            t.NumberType()))
    ripl.bind_foreign_inference_sp(
        "mean",
        deterministic_typed(mean_sp, [t.ArrayUnboxedType(t.NumberType())],
                            t.NumberType()))
    ripl.bind_foreign_inference_sp(
        "stderr",
        deterministic_typed(stderr, [t.ArrayUnboxedType(t.NumberType())],
                            t.NumberType()))
    ripl.bind_foreign_inference_sp(
        "random_string",
        deterministic_typed(random_string, [t.IntegerType()], t.StringType()))
    ripl.bind_foreign_inference_sp(
        "cat_string",
        deterministic_typed(cat_string,
                            [t.StringType(), t.StringType()], t.StringType()))
    ripl.bind_foreign_inference_sp(
        "start_timer", deterministic_typed(start_timer, [], t.NumberType()))
    ripl.bind_foreign_inference_sp(
        "time_elapsed",
        deterministic_typed(time_elapsed, [t.NumberType()], t.NumberType()))
    ripl.execute_program("define new_trace = proc() { run(new_model()) };")
    ripl.execute_program(
        "define run_in_trace = proc(trace, program) { first(run(in_model(trace, program))) };"
    )
    ripl.execute_program(
        "define parallel_mapv = proc(f, l) { run(parallel_mapv_action(f, l, 4)) };"
    )
예제 #3
0
from venture.lite.sp_help import deterministic_typed
from venture.lite.sp_help import type_test
from venture.lite.sp_registry import registerBuiltinSP
import venture.lite.value as vv
import venture.lite.types as t
import venture.lite.utils as u

registerBuiltinSP("array",
  deterministic_typed(lambda *args: np.array(args),
    [t.AnyType()], t.ArrayType(), variadic=True,
    sim_grad=lambda args, direction: direction.getArray(),
    descr="array returns an array initialized with its arguments"))

registerBuiltinSP("vector",
  deterministic_typed(lambda *args: np.array(args),
    [t.NumberType()], t.ArrayUnboxedType(t.NumberType()), variadic=True,
    sim_grad=lambda args, direction: direction.getArray(),
    descr="vector returns an unboxed numeric array initialized with its arguments"))

registerBuiltinSP("is_array", type_test(t.ArrayType()))
registerBuiltinSP("is_vector", type_test(t.ArrayUnboxedType(t.NumberType())))

registerBuiltinSP("to_array",
  deterministic_typed(lambda seq: seq.getArray(),
    [t.HomogeneousSequenceType(t.AnyType())], t.ArrayType(),
    descr="to_array converts its argument sequence to an array"))

registerBuiltinSP("to_vector",
  deterministic_typed(lambda seq: np.array(seq.getArray(t.NumberType())),
    [t.HomogeneousSequenceType(t.NumberType())],
    t.ArrayUnboxedType(t.NumberType()),
예제 #4
0
def __venture_start__(ripl):
    ripl.execute_program('''
        define set_value_at_scope_block = (scope, block, value) -> {
            set_value_at2(scope, block, value)
        };
    ''')
    ripl.bind_foreign_inference_sp(
        'sort',
        deterministic_typed(
            np.sort,
            [
                vt.ArrayUnboxedType(vt.NumberType()),
            ],
            vt.ArrayUnboxedType(vt.NumberType()),
            min_req_args=1
        )
    )
    ripl.bind_foreign_inference_sp(
        'get_mean',
        deterministic_typed(
            np.mean,
            [
                vt.ArrayUnboxedType(vt.NumberType()),
            ],
            vt.NumberType(),
            min_req_args=1
        )
    )
    ripl.bind_foreign_inference_sp(
        'get_predictive_mean',
        deterministic_typed(
            lambda x: np.mean(x, axis=0),
            [
                vt.ArrayUnboxedType(vt.ArrayUnboxedType(vt.NumberType())),
            ],
            vt.ArrayUnboxedType(vt.NumberType()),
            min_req_args=1
        )
    )
    ripl.bind_foreign_inference_sp(
        'load_csv',
        deterministic_typed(
            load_csv,
            [vt.StringType()],
            vt.ArrayUnboxedType(vt.NumberType()),
            min_req_args=1
        )
    )
    ripl.bind_foreign_inference_sp(
        'concatenate',
        deterministic_typed(
            concatenate,
            [
                vt.ArrayUnboxedType(vt.NumberType()),
                vt.ArrayUnboxedType(vt.NumberType()),
            ],
            vt.ArrayUnboxedType(vt.NumberType()),
            min_req_args=2
        )
    )
    ripl.bind_foreign_inference_sp(
        'scatter_plot',
        deterministic_typed(
            scatter_plot,
            [
                vt.ArrayUnboxedType(vt.NumberType()),
                vt.ArrayUnboxedType(vt.NumberType()),
                vt.HomogeneousDictType(vt.StringType(), vt.AnyType())
            ],
            vt.NilType(),
            min_req_args=2
        )
    )
    ripl.bind_foreign_inference_sp(
        'line_plot',
        deterministic_typed(
            line_plot,
            [
                vt.ArrayUnboxedType(vt.NumberType()),
                vt.ArrayUnboxedType(vt.NumberType()),
                vt.HomogeneousDictType(vt.StringType(), vt.AnyType())
                ],
            vt.NilType(),
            min_req_args=2
        )
    )
    ripl.bind_foreign_inference_sp(
        'legend',
        deterministic_typed(
            legend,
            [vt.StringType()],
            vt.NilType(),
            min_req_args=0
        )
    )
    ripl.bind_foreign_inference_sp(
        'square_heatmap',
        deterministic_typed(
            square_heatmap,
            [
                vt.ArrayUnboxedType(vt.NumberType()),
                vt.ArrayUnboxedType(vt.NumberType()),
                vt.HomogeneousDictType(vt.StringType(), vt.AnyType())
            ],
            vt.NilType(),
            min_req_args=2
        )
    )
    ripl.bind_foreign_sp(
        'gp_cov_cp',
        _cov_sp(
            change_point,
            [
                vt.NumberType(),
                vt.NumberType(),
                GPCovarianceType('K'),
                GPCovarianceType('H')
            ]
        )
    )
예제 #5
0
def symbolic_zero_left(n, obj):
    assert n == 0, "Cannot add non-zero integer %r to %r" % (n, obj)
    return obj


def symbolic_zero_right(obj, n):
    assert n == 0, "Cannot add non-zero integer %r to %r" % (n, obj)
    return obj


generic_add = dispatching_psp([
    SPType([t.Int], t.Int, variadic=True),
    SPType([t.Int, t.Number], t.Number),
    SPType([t.Number, t.Int], t.Number),
    SPType([t.NumberType()], t.NumberType(), variadic=True),
    SPType([t.ArrayUnboxedType(t.NumberType()),
            t.NumberType()], t.ArrayUnboxedType(t.NumberType())),
    SPType([t.NumberType(), t.ArrayUnboxedType(t.NumberType())],
           t.ArrayUnboxedType(t.NumberType())),
    SPType([t.ArrayUnboxedType(t.NumberType())],
           t.ArrayUnboxedType(t.NumberType()),
           variadic=True),
    SPType([t.Int, t.Object], t.Object),
    SPType([t.Object, t.Int], t.Object),
    SPType([t.Object, t.Object], t.Object),
], [
    deterministic_psp(
        lambda *args: sum(args),
        sim_grad=lambda args, direction: [direction for _ in args],
        descr="add returns the sum of all its arguments"),
    deterministic_psp(lambda a, b: a + b),
예제 #6
0
 def gradient_type(self):
     return t.ArrayUnboxedType(t.NumericArrayType())
예제 #7
0
        x = args.operandValues()
        return _gp_gradientOfLogDensity(self.mean, self.covariance, samples,
                                        [x], [o])

    def incorporate(self, o, args):
        samples = args.spaux().samples
        x = args.operandValues()[0]
        samples[x] = o

    def unincorporate(self, _o, args):
        samples = args.spaux().samples
        x = args.operandValues()[0]
        del samples[x]


gpType = SPType([t.ArrayUnboxedType(t.NumericArrayType())],
                t.ArrayUnboxedType(t.NumberType()))

gp1Type = SPType([t.NumberType()], t.NumberType())


class GPSPAux(SPAux):
    def __init__(self, samples):
        self.samples = samples

    def copy(self):
        return GPSPAux(copy.copy(self.samples))

    def asVentureValue(self):
        def encode(xy):
            # (x,y) = xy