Exemple #1
0
def get_numeric_gradient(stochastic, pvalue):
    e = 1e-9
    initial_value = pvalue.value
    i_shape = shape(initial_value)
    i_size = size(initial_value)

    initial_logp = utils.logp_of_set(stochastic)
    numeric_gradient = zeros(i_shape)
    if not (pvalue.dtype in float_dtypes):
        return numeric_gradient

    for i in range(i_size):

        delta = zeros(i_shape)

        try:
            delta[unravel_index(i, i_shape or (1, ))] += e
        except IndexError:
            delta += e

        pvalue.value = initial_value + delta
        logp = utils.logp_of_set(stochastic)

        try:
            numeric_gradient[unravel_index(i, i_shape or
                                           (1, ))] = (logp - initial_logp) / e
        except IndexError:
            numeric_gradient = (logp - initial_logp) / e

    pvalue.value = initial_value
    return numeric_gradient
Exemple #2
0
def get_numeric_gradient(stochastic, pvalue):
    e = 1e-9
    initial_value = pvalue.value
    i_shape = shape(initial_value)
    i_size = size(initial_value)

    initial_logp = utils.logp_of_set(stochastic)
    numeric_gradient = zeros(i_shape)
    if not (pvalue.dtype in float_dtypes):
        return numeric_gradient

    for i in range(i_size):

        delta = zeros(i_shape)

        try:
            delta[unravel_index(i, i_shape or (1,))] += e
        except IndexError:
            delta += e

        pvalue.value = initial_value + delta
        logp = utils.logp_of_set(stochastic)

        try:
            numeric_gradient[unravel_index(i, i_shape or (1,))] = (logp - initial_logp) / e
        except IndexError:
            numeric_gradient = (logp - initial_logp) / e

    pvalue.value = initial_value
    return numeric_gradient
Exemple #3
0
 def test_other_err(self):
     self.B.rand()
     for i in xrange(1000):
         try:
             utils.logp_of_set(set([self.A,self.B,self.D,self.E]))
         except:
             cls,  inst, tb = sys.exc_info()
             assert(cls is RuntimeError)
Exemple #4
0
 def test_ZeroProb(self):
     self.B.value = -1
     for i in xrange(1000):
         try:
             utils.logp_of_set(set([self.A,self.B,self.D, self.E]))
         except:
             cls,  inst, tb = sys.exc_info()
             assert(cls is ZeroProbability)
Exemple #5
0
 def test_other_err(self):
     self.B.rand()
     for i in xrange(1000):
         try:
             utils.logp_of_set(set([self.A, self.B, self.D, self.E]))
         except:
             cls, inst, tb = sys.exc_info()
             assert (cls is RuntimeError)
Exemple #6
0
 def test_ZeroProb(self):
     self.B.value = -1
     for i in xrange(1000):
         try:
             utils.logp_of_set(set([self.A, self.B, self.D, self.E]))
         except:
             cls, inst, tb = sys.exc_info()
             assert (cls is ZeroProbability)
Exemple #7
0
 def test_logp(self):
     self.B.rand()
     lp1 = utils.logp_of_set(set([self.A,self.B,self.D]))
     assert_equal(lp1, self.A.logp+self.B.logp+self.D.logp)
Exemple #8
0
 def test_logp(self):
     self.B.rand()
     lp1 = utils.logp_of_set(set([self.A, self.B, self.D]))
     assert_almost_equal(lp1, self.A.logp + self.B.logp + self.D.logp, 10)