def grad_logisticv(args, direction): # XXX The direction is a Venture value, but the deriv is a Python # (numpy) array :( [x] = args (_, deriv) = T_logistic(x) answer = direction.array * deriv # print "Gradient of logistic got", x, deriv, direction.array, answer return [v.VentureArrayUnboxed(answer, t.Number)]
def asVentureValue(self, thing): thing = np.asarray(thing) if len(thing.shape) == 0: return vv.VentureNumber(float(thing)) elif len(thing.shape) == 1: subtype = NumberType(thing.dtype.name) else: subtype = self return vv.VentureArrayUnboxed(thing, subtype)
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 array_unboxed(self, length=None, elt_type=None, **kwargs): if length is None: length = npr.randint(0, 10) if elt_type is None: elt_type = t.NumberType( ) # TODO Do I want to test on a screwy class of unboxed arrays in general? return v.VentureArrayUnboxed([ elt_type.asPython( elt_type.distribution(self.__class__, **kwargs).generate()) for _ in range(length) ], elt_type)
def asVentureValue(self, thing): return vv.VentureArrayUnboxed(thing, self.subtype)
def array(xs): return v.VentureArrayUnboxed(np.array(xs), gp.xType)
def matrix_times_vector_grad(args, direction): (mat, vec) = args dm = np.outer(direction.array, vec) dv = np.transpose(np.multiply(np.transpose(mat), direction.array))[0] # print "Matrix times vector gradient", mat, vec, direction, dm, dv return [vv.VentureMatrix(dm), vv.VentureArrayUnboxed(dv, t.Number)]
def grad_vector_times_scalar(args, direction): dot_prod = v.vv_dot_product(v.VentureArrayUnboxed(args[0], t.NumberType()), direction) return [direction * args[1], v.VentureNumber(dot_prod)]