Example #1
0
    def test_function(self):
        with self.compiler.graph.as_default():
            shape_scope = {
                'mu/1': TensorFluentShape([24], batch=False),
                'sigma/1': TensorFluentShape([24], batch=False)
            }

            scope = {
                'mu/1': TensorFluent(tf.zeros([24]), scope=['?x'], batch=False),
                'sigma/1': TensorFluent(tf.ones([24]), scope=['?x'], batch=False)
            }

        noise1 = get_reparameterization(self.exp_2, scope=shape_scope)
        self._test_reparameterization_dist(noise1, [])
        self._test_reparameterized_expression(self.exp_2, scope=scope, noise=noise1, name='noise1')

        noise2 = get_reparameterization(self.exp_z, scope=shape_scope)
        self._test_reparameterization_dist(noise2, [('Normal', [1])])
        self._test_reparameterized_expression(self.exp_z, scope=scope, noise=noise2, name='noise2')

        noise3 = get_reparameterization(self.exp_x1, scope=shape_scope)
        self._test_reparameterization_dist(noise3, [('Normal', [24])])
        self._test_reparameterized_expression(self.exp_x1, scope=scope, noise=noise3, name='noise3')

        noise4 = get_reparameterization(self.y1, scope=shape_scope)
        self._test_reparameterization_dist(noise4, [('Normal', [1]), ('Normal', [1])])
        self._test_reparameterized_expression(self.y1, scope=scope, noise=noise4, name='noise4')

        noise5 = get_reparameterization(self.y2, scope=shape_scope)
        self._test_reparameterization_dist(noise5, [('Normal', [1]), ('Normal', [24])])
        self._test_reparameterized_expression(self.y2, scope=scope, noise=noise5, name='noise5')

        noise6 = get_reparameterization(self.y3, scope=shape_scope)
        self._test_reparameterization_dist(noise6, [('Normal', [24]), ('Normal', [24])])
        self._test_reparameterized_expression(self.y3, scope=scope, noise=noise6, name='noise6')
Example #2
0
 def test_normal_fluent(self):
     mean = self.zero
     variance = self.one
     dist, fluent = TensorFluent.Normal(mean, variance, self.batch_size)
     self.assertIsInstance(dist, tf.distributions.Normal)
     self._test_op_name(fluent, r'^Normal[^/]*/sample')
     self._test_fluent(fluent, tf.float32, [self.batch_size], [], True)
Example #3
0
    def test_gamma(self):
        # rainfall(?r) = Gamma(RAIN_SHAPE(?r), RAIN_SCALE(?r));
        with self.compiler.graph.as_default():
            shape_scope = {
                'shape/1': TensorFluentShape((32, 8), batch=True),
                'scale/1': TensorFluentShape((32, 8), batch=True)
            }

            scope = {
                'shape/1': TensorFluent(tf.ones((32, 8)), scope=['?r'], batch=True),
                'scale/1': TensorFluent(tf.ones((32, 8)), scope=['?r'], batch=True)
            }

        noise1 = get_reparameterization(self.gamma1, scope=shape_scope)
        self._test_reparameterization_dist(noise1, [('Gamma', [32, 8])])
        self._test_reparameterized_expression(self.gamma1, scope=scope, noise=noise1, name='noise1')
Example #4
0
    def test_arithmetic(self):
        with self.compiler.graph.as_default():
            shape_scope = {
                'mu/1': TensorFluentShape([32], batch=False),
                'sigma/1': TensorFluentShape([32], batch=False)
            }

            scope = {
                'mu/1': TensorFluent(tf.zeros([32]), scope=['?x'], batch=False),
                'sigma/1': TensorFluent(tf.ones([32]), scope=['?x'], batch=False)
            }

        noise1 = get_reparameterization(self.two, scope={})
        self._test_reparameterization_dist(noise1, [])
        self._test_reparameterized_expression(self.two, scope={}, noise=noise1, name='noise1')

        noise2 = get_reparameterization(self.z_times_z, scope={})
        self._test_reparameterization_dist(noise2, [('Normal', [1]), ('Normal', [1])])
        self._test_reparameterized_expression(self.z_times_z, scope={}, noise=noise2, name='noise2')

        noise3 = get_reparameterization(self.x2_times_x2, scope=shape_scope)
        self._test_reparameterization_dist(noise3, [('Normal', [32]), ('Normal', [32])])
        self._test_reparameterized_expression(self.x2_times_x2, scope=scope, noise=noise3, name='noise3')

        noise4 = get_reparameterization(self.mu_plus_z, scope=shape_scope)
        self._test_reparameterization_dist(noise4, [('Normal', [1])])
        self._test_reparameterized_expression(self.mu_plus_z, scope=scope, noise=noise4, name='noise4')

        noise5 = get_reparameterization(self.z_plus_mu, scope=shape_scope)
        self._test_reparameterization_dist(noise5, [('Normal', [1])])
        self._test_reparameterized_expression(self.z_plus_mu, scope=scope, noise=noise5, name='noise5')

        noise6 = get_reparameterization(self.mu_plus_x2, scope=shape_scope)
        self._test_reparameterization_dist(noise6, [('Normal', [32])])
        self._test_reparameterized_expression(self.mu_plus_x2, scope=scope, noise=noise6, name='noise6')

        noise7 = get_reparameterization(self.x2_plus_mu, scope=shape_scope)
        self._test_reparameterization_dist(noise7, [('Normal', [32])])
        self._test_reparameterized_expression(self.x2_plus_mu, scope=scope, noise=noise7, name='noise7')

        noise8 = get_reparameterization(self.x1_plus_z, scope=shape_scope)
        self._test_reparameterization_dist(noise8, [('Normal', [32]), ('Normal', [1])])
        self._test_reparameterized_expression(self.x1_plus_z, scope=scope, noise=noise8, name='noise8')

        noise9 = get_reparameterization(self.z_plus_x1, scope=shape_scope)
        self._test_reparameterization_dist(noise9, [('Normal', [1]), ('Normal', [32])])
        self._test_reparameterized_expression(self.z_plus_x1, scope=scope, noise=noise9, name='noise9')
Example #5
0
    def test_multivariate_normal(self):
        with self.compiler.graph.as_default():
            shape_scope = {
                'mu/1': TensorFluentShape([32], batch=False),
                'sigma/1': TensorFluentShape([32], batch=False)
            }

            scope = {
                'mu/1': TensorFluent(tf.zeros([32]), scope=['?x'], batch=False),
                'sigma/1': TensorFluent(tf.ones([32]), scope=['?x'], batch=False)
            }

        noise1 = get_reparameterization(self.x1, scope=shape_scope)
        self._test_reparameterization_dist(noise1, [('Normal', [32])])
        self._test_reparameterized_expression(self.x1, scope=scope, noise=noise1, name='noise1')

        noise2 = get_reparameterization(self.x2, scope=shape_scope)
        self._test_reparameterization_dist(noise2, [('Normal', [32])])
        self._test_reparameterized_expression(self.x2, scope=scope, noise=noise2, name='noise2')

        noise3 = get_reparameterization(self.x3, scope=shape_scope)
        self._test_reparameterization_dist(noise3, [('Normal', [32])])
        self._test_reparameterized_expression(self.x3, scope=scope, noise=noise3, name='noise3')

        noise4 = get_reparameterization(self.x4, scope=shape_scope)
        self._test_reparameterization_dist(noise4, [('Normal', [1]), ('Normal', [1])])
        self._test_reparameterized_expression(self.x4, scope=scope, noise=noise4, name='noise4')

        noise5 = get_reparameterization(self.x5, scope=shape_scope)
        self._test_reparameterization_dist(noise5, [('Normal', [32]), ('Normal', [32])])
        self._test_reparameterized_expression(self.x5, scope=scope, noise=noise5, name='noise5')

        noise6 = get_reparameterization(self.x6, scope=shape_scope)
        self._test_reparameterization_dist(noise6, [('Normal', [32]), ('Normal', [32])])
        self._test_reparameterized_expression(self.x6, scope=scope, noise=noise6, name='noise6')

        noise7 = get_reparameterization(self.x7, scope=shape_scope)
        self._test_reparameterization_dist(noise7, [('Normal', [1]), ('Normal', [1]), ('Normal', [1])])
        self._test_reparameterized_expression(self.x7, scope=scope, noise=noise7, name='noise7')

        noise8 = get_reparameterization(self.x8, scope=shape_scope)
        self._test_reparameterization_dist(noise8, [('Normal', [1]), ('Normal', [1]), ('Normal', [32])])
        self._test_reparameterized_expression(self.x8, scope=scope, noise=noise8, name='noise8')

        noise9 = get_reparameterization(self.x9, scope=shape_scope)
        self._test_reparameterization_dist(noise9, [('Normal', [32]), ('Normal', [1]), ('Normal', [1]), ('Normal', [32])])
        self._test_reparameterized_expression(self.x9, scope=scope, noise=noise9, name='noise9')
Example #6
0
    def test_stop_gradient(self):
        mean = self.zero
        variance = self.one
        _, sample = TensorFluent.Normal(mean, variance, self.batch_size)
        fluent = TensorFluent.stop_gradient(sample)
        self._test_fluent(fluent, sample.dtype, sample.shape.as_list(), sample.scope.as_list(), sample.batch)
        self._test_op_name(fluent, r'^StopGradient')

        grad_before, = tf.gradients(ys=tf.reduce_sum(sample.tensor), xs=mean.tensor)
        grad_after, = tf.gradients(ys=tf.reduce_sum(fluent.tensor), xs=mean.tensor)

        self.assertIsInstance(grad_before, tf.Tensor)
        self.assertIsNone(grad_after)

        with tf.Session() as sess:
            f1, f2 = sess.run([sample.tensor, fluent.tensor])
            self.assertListEqual(list(f1), list(f2))

            g1 = sess.run(grad_before)
            self.assertEqual(g1, self.batch_size)
Example #7
0
    def test_exponential(self):
        # rainfall(?r) = Exponential(RAIN_RATE(?r));
        with self.compiler.graph.as_default():
            shape_scope = {
                'rate/1': TensorFluentShape((32, 8), batch=True)
            }

            scope = {
                'rate/1': TensorFluent(tf.ones((32, 8)), scope=['?r'], batch=True)
            }

        noise1 = get_reparameterization(self.exp1, scope=shape_scope)
        self._test_reparameterization_dist(noise1, [('Uniform', [32, 8])])
        self._test_reparameterized_expression(self.exp1, scope=scope, noise=noise1, name='noise1')
Example #8
0
    def test_batch_normal(self):
        with self.compiler.graph.as_default():
            shape_scope = {
                'mu/1': TensorFluentShape((64, 16), batch=True),
                'sigma/1': TensorFluentShape((64, 16), batch=True)
            }

            scope = {
                'mu/1': TensorFluent(tf.zeros([64, 16]), scope=['?x'], batch=True),
                'sigma/1': TensorFluent(tf.ones([64, 16]), scope=['?x'], batch=True)
            }

        noise1 = get_reparameterization(self.x1, scope=shape_scope)
        self._test_reparameterization_dist(noise1, [('Normal', [64, 16])])
        self._test_reparameterized_expression(self.x1, scope=scope, noise=noise1, name='noise1')

        noise2 = get_reparameterization(self.x2, scope=shape_scope)
        self._test_reparameterization_dist(noise2, [('Normal', [64, 16])])
        self._test_reparameterized_expression(self.x2, scope=scope, noise=noise2, name='noise2')

        noise3 = get_reparameterization(self.x3, scope=shape_scope)
        self._test_reparameterization_dist(noise3, [('Normal', [64, 16])])
        self._test_reparameterized_expression(self.x3, scope=scope, noise=noise3, name='noise3')
Example #9
0
    def test_stop_batch_gradient(self):
        mean = TensorFluent(tf.zeros([self.batch_size]), [], True)
        variance = TensorFluent(tf.ones([self.batch_size]), [], True)
        _, sample = TensorFluent.Normal(mean, variance)

        stop_batch = tf.cast(tf.distributions.Bernoulli(probs=0.5).sample(self.batch_size), tf.bool)

        fluent = TensorFluent.stop_batch_gradient(sample, stop_batch)
        self._test_fluent(fluent, sample.dtype, sample.shape.as_list(), sample.scope.as_list(), sample.batch)
        self._test_op_name(fluent, r'^Select')

        grad_before, = tf.gradients(ys=tf.reduce_sum(sample.tensor), xs=mean.tensor)
        grad_after, = tf.gradients(ys=tf.reduce_sum(fluent.tensor), xs=mean.tensor)

        self.assertIsInstance(grad_before, tf.Tensor)
        self.assertIsInstance(grad_after, tf.Tensor)

        with tf.Session() as sess:
            f1, f2 = sess.run([sample.tensor, fluent.tensor])
            self.assertListEqual(list(f1), list(f2))

            g1, g2, stop = sess.run([grad_before, grad_after, stop_batch])
            self.assertTrue(all(g1 == np.ones([self.batch_size])))
            self.assertTrue(all(g2 == (~stop).astype(np.float32)))
Example #10
0
    def test_if_then_else(self):
        # if (abs[x - gx] > 0.0) then
        #     (u * u) * 0.01
        # else
        #     0.0
        x = tf.random_normal([self.batch_size, 1])
        x = TensorFluent(x, scope=[], batch=True)
        gx = tf.random_normal([])
        gx = TensorFluent(gx, scope=[], batch=False)
        u = tf.random_normal([self.batch_size,])
        u = TensorFluent(u, scope=[], batch=True)
        c0 = TensorFluent.constant(0.01)
        cond = TensorFluent.abs(x - gx) > self.zero
        true_case = (u * u) * c0
        false_case = self.zero
        ite = TensorFluent.if_then_else(cond, true_case, false_case)
        self._test_fluent(ite, tf.float32, [self.batch_size, 1], [], True)

        # if ((u * u) * 0.01 > 1.0) then
        #     abs[x - gx]
        # else
        #     0.0
        cond = ((u * u) * c0) > self.one
        true_case = TensorFluent.abs(x - gx)
        false_case = self.zero
        ite = TensorFluent.if_then_else(cond, true_case, false_case)
        self._test_fluent(ite, tf.float32, [self.batch_size, 1], [], True)

        # if (gx < 1.0) then
        #     1.0 + u
        # else
        #     0.05 * (u * u)
        c1 = TensorFluent.constant(0.05)
        cond = (gx < self.one)
        true_case = self.one + u
        false_case = c1 * (u * u)
        ite = TensorFluent.if_then_else(cond, true_case, false_case)
        self._test_fluent(ite, tf.float32, [self.batch_size], [], True)

        # if (gx < 0.0) then
        #     1.0
        # else
        #     abs[x - gx]
        cond = (gx < self.zero)
        true_case = self.one
        false_case = TensorFluent.abs(x - gx)
        ite = TensorFluent.if_then_else(cond, true_case, false_case)
        self._test_fluent(ite, tf.float32, [self.batch_size, 1], [], True)

        # if (visited(?wpt)) then
        #     (1.0)
        # else
        #     (0.0)
        visited = tf.stack([[True, False, True]] * self.batch_size, axis=0)
        visited = TensorFluent(visited, scope=['?wpt'], batch=True)
        cond = visited
        true_case = self.one
        false_case = self.zero
        ite = TensorFluent.if_then_else(cond, true_case, false_case)
        self._test_fluent(ite, tf.float32, [self.batch_size, 3], ['?wpt'], True)

        # if (rlevel'(?r)<=LOWER_BOUND(?r)) then
        #     LOW_PENALTY(?r)*(LOWER_BOUND(?r)-rlevel'(?r))
        # else
        #     HIGH_PENALTY(?r)*(rlevel'(?r)-UPPER_BOUND(?r))];
        r_size = 8
        low_penalty = TensorFluent.constant(-5.0)
        high_penalty = TensorFluent.constant(-10.0)
        lower_bound = tf.random_normal([r_size], mean=50.0, stddev=10.0)
        lower_bound = TensorFluent(lower_bound, scope=['?r'], batch=False)
        upper_bound = tf.random_normal([r_size], mean=200.0, stddev=10.0)
        upper_bound = TensorFluent(upper_bound, scope=['?r'], batch=False)
        rlevel = tf.random_normal([self.batch_size, r_size], mean=100.0, stddev=10.0)
        rlevel = TensorFluent(rlevel, scope=['?r'], batch=True)
        cond = (rlevel <= lower_bound)
        true_case = low_penalty * (lower_bound - rlevel)
        false_case = high_penalty * (rlevel - upper_bound)
        ite = TensorFluent.if_then_else(cond, true_case, false_case)
        self._test_fluent(ite, tf.float32, [self.batch_size, r_size], ['?r'], True)

        # if (snapPicture) then
        #    (time + 0.25)
        # else
        #    (time + abs[xMove] + abs[yMove]);
        time = tf.fill([self.batch_size, 1], 12.0)
        time = TensorFluent(time, scope=[], batch=True)
        snapPicture = tf.fill([self.batch_size], True)
        snapPicture = TensorFluent(snapPicture, scope=[], batch=True)
        xMove = tf.fill([self.batch_size], 10.0)
        xMove = TensorFluent(xMove, scope=[], batch=True)
        yMove = tf.fill([self.batch_size], -3.0)
        yMove = TensorFluent(yMove, scope=[], batch=True)
        cond = snapPicture
        true_case = (time + TensorFluent.constant(0.25))
        false_case = (time + TensorFluent.abs(xMove) + TensorFluent.abs(yMove))
        ite = TensorFluent.if_then_else(cond, true_case, false_case)
        self._test_fluent(ite, tf.float32, [self.batch_size, 1], [], True)

        # if (alive(?x,?y)) then
        #     Bernoulli(1.0 - NOISE-PROB(?x,?y))
        # else
        #     Bernoulli(NOISE-PROB(?x,?y));
        x_size, y_size = 3, 3
        alive = tf.distributions.Bernoulli(probs=0.7, dtype=tf.bool).\
            sample([self.batch_size, x_size, y_size])
        alive = TensorFluent(alive, scope=['?x', '?y'], batch=True)
        noise_prob = tf.random_uniform([x_size, y_size], dtype=tf.float32)
        noise_prob = TensorFluent(noise_prob, scope=['?x', '?y'], batch=False)
        cond = alive
        true_case = TensorFluent.Bernoulli(self.one - noise_prob, batch_size=self.batch_size)[1]
        false_case = TensorFluent.Bernoulli(noise_prob, batch_size=self.batch_size)[1]
        ite = TensorFluent.if_then_else(cond, true_case, false_case)
        self._test_fluent(ite, tf.bool, [self.batch_size, 3, 3], ['?x', '?y'], True)

        # if (reboot(?x)) then
        #     KronDelta(true)
        # else
        #     ~running(?x);
        x_size = 128
        mean = tf.random_uniform([x_size], dtype=tf.float32)
        mean = TensorFluent(mean, scope=['?x'])
        running = TensorFluent.Bernoulli(mean, batch_size=self.batch_size)[1]
        reboot = TensorFluent.Bernoulli(self.one - mean, batch_size=self.batch_size)[1]
        cond = reboot
        true_case = TensorFluent.constant(True, dtype=tf.bool)
        false_case = ~running
        ite = TensorFluent.if_then_else(cond, true_case, false_case)
        self._test_fluent(ite, tf.bool, [self.batch_size, x_size], ['?x'], True)

        # if (running(?x)) then
        #     Bernoulli(.45 + .5 * running(?x))
        # else
        #     Bernoulli(REBOOT-PROB);
        c0 = TensorFluent.constant(0.45)
        c1 = TensorFluent.constant(0.5)
        reboot_prob = TensorFluent.constant(0.15)
        cond = running
        true_case = TensorFluent.Bernoulli(c0 + c1 * running)[1]
        false_case = TensorFluent.Bernoulli(reboot_prob)[1]
        ite = TensorFluent.if_then_else(cond, true_case, false_case)
        self._test_fluent(ite, tf.bool, [self.batch_size, x_size], ['?x'], True)
Example #11
0
 def setUp(self):
     self.batch_size = 32
     tf.reset_default_graph()
     self.zero = TensorFluent.constant(0.0)
     self.one = TensorFluent.constant(1)