Exemplo n.º 1
0
 def test_all_finite_raises(self):
   with self.test_session():
     x = np.inf * tf.constant([-1.0, -2.0, -3.0, -4.0])
     with self.assertRaisesOpError('Inf'):
       cumprod(x).eval()
     x = tf.constant([-1.0, np.nan, -3.0, -4.0])
     with self.assertRaisesOpError('NaN'):
       cumprod(x).eval()
Exemplo n.º 2
0
 def test_all_finite_raises(self):
     with self.test_session():
         x = np.inf * tf.constant([-1.0, -2.0, -3.0, -4.0])
         with self.assertRaisesOpError('Inf'):
             cumprod(x).eval()
         x = tf.constant([-1.0, np.nan, -3.0, -4.0])
         with self.assertRaisesOpError('NaN'):
             cumprod(x).eval()                
Exemplo n.º 3
0
 def mapping(self, x):
     # Transform a real (K-1)-vector to K-dimensional simplex.
     pi = Variable("pi", [self.num_factors, self.K-1])
     eq = -tf.log(tf.cast(self.K - 1 - tf.range(self.K-1), dtype=tf.float32))
     z = tf.sigmoid(eq + pi)
     pil = tf.concat(1, [z, tf.ones([self.num_factors, 1])])
     piu = tf.concat(1, [tf.ones([self.num_factors, 1]), 1.0 - z])
     # cumulative product along 1st axis
     S = tf.pack([cumprod(piu_x) for piu_x in tf.unpack(piu)])
     return [S * pil]
Exemplo n.º 4
0
 def mapping(self, x):
     # Transform a real (K-1)-vector to K-dimensional simplex.
     pi = Variable("pi", [self.num_factors, self.K - 1])
     eq = -tf.log(
         tf.cast(self.K - 1 - tf.range(self.K - 1), dtype=tf.float32))
     z = tf.sigmoid(eq + pi)
     pil = tf.concat(1, [z, tf.ones([self.num_factors, 1])])
     piu = tf.concat(1, [tf.ones([self.num_factors, 1]), 1.0 - z])
     # cumulative product along 1st axis
     S = tf.pack([cumprod(piu_x) for piu_x in tf.unpack(piu)])
     return [S * pil]
Exemplo n.º 5
0
    def __init__(self, shape, pi=None):
        num_factors = shape[0]
        K = shape[-1]
        if K == 1:
            raise ValueError("Multinomial is not supported for K=1. Use Bernoulli.")

        Distribution.__init__(self, num_factors)
        self.num_vars = K*num_factors
        self.num_params = K*num_factors
        self.K = K # dimension of each factor
        self.sample_tensor = False

        if pi is None:
            # Transform a real (K-1)-vector to K-dimensional simplex.
            pi_unconst = tf.Variable(tf.random_normal([self.num_factors, self.K-1]))
            eq = -tf.log(tf.cast(self.K - 1 - tf.range(self.K-1), dtype=tf.float32))
            x = tf.sigmoid(eq + pi_unconst)
            pil = tf.concat(1, [x, tf.ones([self.num_factors, 1])])
            piu = tf.concat(1, [tf.ones([self.num_factors, 1]), 1.0 - x])
            # cumulative product along 1st axis
            S = tf.pack([cumprod(piu_x) for piu_x in tf.unpack(piu)])
            pi = S * pil

        self.pi = pi
Exemplo n.º 6
0
 def test_cumprod_2d(self):
   with self.test_session():
     x = tf.constant([[-1.0], [-2.0], [-3.0], [-4.0]])
     self.assertAllClose(cumprod(x).eval(),
                         np.array([[-1.], [2.], [-6.], [24.]]))
Exemplo n.º 7
0
 def test_cumprod_1d(self):
   with self.test_session():
     x = tf.constant([-1.0, -2.0, -3.0, -4.0])
     self.assertAllEqual(cumprod(x).eval(),
                         np.array([-1., 2., -6., 24.]))
Exemplo n.º 8
0
 def test_cumprod_2d(self):
     with self.test_session():
         x = tf.constant([[-1.0], [-2.0], [-3.0], [-4.0]])
         self.assertAllClose(cumprod(x).eval(), 
                             np.array([ [-1.],   [2.],  [-6.],  [24.]]))             
Exemplo n.º 9
0
 def test_cumprod_1d(self):
     with self.test_session():
         x = tf.constant([-1.0, -2.0, -3.0, -4.0])
         self.assertAllEqual(cumprod(x).eval(), 
                             np.array([ -1.,   2.,  -6.,  24.]))