def array(self, length=None, elt_dist=None, **kwargs): if length is None: length = npr.randint(0, 10) if elt_dist is None: elt_dist = DefaultRandomVentureValue( "object") # TODO reuse class of self return v.VentureArray( [elt_dist.generate(**kwargs) for _ in range(length)])
def asVentureValue(self): def encode(xy): # (x,y) = xy # Since we are assuming the domain of the GP is numeric, the # following suffices: return v.VentureArray(map(v.VentureNumber, xy)) return v.VentureArray([encode(xy) for xy in self.samples.items()])
def testGradientOfSimulateOfLookup(): from venture.lite.sp_registry import builtInSPs sp = builtInSPs()["lookup"] args = [ vv.VentureArray([vv.VentureNumber(0), vv.VentureNumber(0)]), vv.VentureNumber(1) ] grad = sp.outputPSP.gradientOfSimulate(MockArgs(args, sp.constructSPAux()), vv.VentureNumber(0), vv.VentureNumber(1)) assert grad[0].lookup(vv.VentureNumber(0)) == 0 assert grad[0].lookup(vv.VentureNumber(1)) == vv.VentureNumber(1) assert grad[1] == 0
def asVentureValue(self, thing): if isinstance(thing, bool) or isinstance(thing, np.bool_): return vv.VentureBool(thing) if isinstance(thing, int): return vv.VentureInteger(thing) if isinstance(thing, numbers.Number): return vv.VentureNumber(thing) if isinstance(thing, vv.VentureAtom): return thing if isinstance(thing, str): return vv.VentureSymbol(thing) if hasattr(thing, "__getitem__"): # Already not a string return vv.VentureArray([self.asVentureValue(val) for val in thing]) if isinstance(thing, vv.VentureValue): return thing else: raise Exception("Cannot convert Python object %r to a Venture " \ "Expression" % thing)
def asVentureValue(self, thing): if isinstance(thing, bool) or isinstance(thing, np.bool_): return vv.VentureBool(thing) if isinstance(thing, int): return vv.VentureInteger(thing) if isinstance(thing, numbers.Number): return vv.VentureNumber(thing) if isinstance(thing, str): return vv.VentureString(thing) if isinstance(thing, dict): return vv.VentureDict( OrderedDict([(self.asVentureValue(key), self.asVentureValue(val)) for (key, val) in thing.iteritems()])) if hasattr(thing, "__getitem__"): # Already not a string return vv.VentureArray([self.asVentureValue(val) for val in thing]) if isinstance(thing, vv.VentureValue): return thing else: raise Exception("Cannot convert Python object %r to a Venture " \ "Expression" % thing)
def testGPAux(): # Make sure the GP's aux is properly maintained. It should be an # array of all pairs (x,y) such that the GP has been called with # input x and returned output y. ripl = get_ripl() prep_ripl(ripl) def check_firsts(stats, firsts): eq_(len(stats), len(firsts)) eq_(set([xy[0] for xy in stats]), set(firsts)) ripl.assume('gp', '(make_gp zero sq_exp)') ripl.predict('(gp (array 1.0 3.0))') check_firsts(ripl.infer('(extract_stats gp)'), {1.0, 3.0}) ripl.observe('(gp (array 5.0))', v.VentureArray(map(v.VentureNumber, [8.8])), label='obs') check_firsts(ripl.infer('(extract_stats gp)'), {1.0, 3.0, 5.0}) ripl.forget('obs') check_firsts(ripl.infer('(extract_stats gp)'), {1.0, 3.0})
def encode(xy): # (x,y) = xy # Since we are assuming the domain of the GP is numeric, the # following suffices: return v.VentureArray(map(v.VentureNumber, xy))