def asArgsObject(args): seed = args['seed'] assert seed is not None prng = random.Random(seed) return MockArgs(map(fromStackDict, args.get('operandValues')), args.get('spaux'), py_rng=random.Random(prng.randint(1, 2**31 - 1)), np_rng=npr.RandomState(prng.randint(1, 2**31 - 1)), madeSPAux=args.get('madeSPAux'))
def testGradientOfSimulateOfLookup2(): from venture.lite.sp_registry import builtInSPs sp = builtInSPs()["lookup"] args = [vv.VentureArrayUnboxed([0, 0], t.Number), vv.VentureNumber(1)] grad = sp.outputPSP.gradientOfSimulate(MockArgs(args, sp.constructSPAux()), vv.VentureNumber(0), vv.VentureNumber(1)) assert grad[0].lookup(vv.VentureNumber(0)) == vv.VentureNumber(0) assert grad[0].lookup(vv.VentureNumber(1)) == vv.VentureNumber(1) assert grad[1] == 0
def log_density_crp_joint(assignments, alpha): aux = CRPSPAux() args = MockArgs([], aux) psp = CRPOutputPSP(alpha, 0) # No dispersion def log_d_sample(datum): val = psp.logDensity(datum, args) psp.incorporate(datum, args) return val return sum(map(log_d_sample, assignments))
def simulate_fully_uncurried(name, sp, args_lists, py_rng, np_rng): if isinstance(sp, VentureSPRecord): sp, aux = sp.sp, sp.spAux else: aux = carefully(sp.constructSPAux) if not isinstance(sp.requestPSP, NullRequestPSP): raise SkipTest("SP %s returned a requesting SP" % name) args = MockArgs(args_lists[0], aux, py_rng, np_rng) answer = carefully(sp.outputPSP.simulate, args) if len(args_lists) == 1: return answer else: return simulate_fully_uncurried(name, answer, args_lists[1:], py_rng, np_rng)
def draw_crp_samples(n, alpha, np_rng=None): """Jointly draw n samples from CRP(alpha). This returns an assignment of n objects to clusters, given by a length-n list of cluster ids. """ aux = CRPSPAux() args = MockArgs([], aux, np_rng=np_rng) psp = CRPOutputPSP(alpha, 0) # No dispersion def draw_sample(): ans = psp.simulate(args) psp.incorporate(ans, args) return ans ans = [draw_sample() for _ in range(n)] return ans
def propGradientOfSimulate(args_lists, name, sp): if final_return_type( sp.venture_type().gradient_type()).__class__ == t.ZeroType: # Do not test gradients of things that return elements of # 0-dimensional vector spaces return if not len(args_lists) == 1: raise SkipTest( "TODO: Write the code for testing simulation gradients of curried SPs" ) if name == "mul" and len(args_lists[0]) is not 2: raise ArgumentsNotAppropriate( "TODO mul only has a gradient in its binary form") py_rng = random.Random() np_rng = npr.RandomState() args = MockArgs(args_lists[0], sp.constructSPAux(), py_rng, np_rng) randomness = FixedRandomness(py_rng, np_rng) with randomness: value = carefully(sp.outputPSP.simulate, args) # Use the value itself as the test direction in order to avoid # having to coordinate compound types (like the length of the list # that 'list' returns being the same as the number of arguments) direction = asGradient(value) expected_grad_type = sp.venture_type().gradient_type().args_types try: computed_gradient = carefully(sp.outputPSP.gradientOfSimulate, args, value, direction) for (g, tp) in zip(computed_gradient, expected_grad_type): if g == 0: pass # OK else: assert g in tp except VentureBuiltinSPMethodError: raise SkipTest( "%s does not support computing gradient of simulate :(" % name) def sim_displacement_func(): with randomness: ans = sp.outputPSP.simulate(args) return vv.vv_dot_product(direction, asGradient(ans)) numerical_gradient = carefully(num.gradient_from_lenses, sim_displacement_func, real_lenses(args_lists[0])) assert_gradients_close(numerical_gradient, computed_gradient)
def log_density_fully_uncurried(name, sp, args_lists, value): if isinstance(sp, VentureSPRecord): sp, aux = sp.sp, sp.spAux else: aux = carefully(sp.constructSPAux) if not isinstance(sp.requestPSP, NullRequestPSP): raise SkipTest("SP %s returned a requesting SP" % name) args = MockArgs(args_lists[0], aux) if len(args_lists) == 1: return carefully(sp.outputPSP.logDensity, value, args) else: # TODO Is there a good general interface to # log_density_fully_uncurried that would let the client put in # this check? if sp.outputPSP.isRandom(): raise SkipTest( "The log density of the result of random curried SP %s is not actually deterministic" % name) answer = carefully(sp.outputPSP.simulate, args) return log_density_fully_uncurried(name, answer, args_lists[1:], value)