Example #1
0
    def test_mog(self):
        x_val = np.array([1.1, 1.2, 2.1, 4.4, 5.5, 7.3, 6.8], np.float32)
        z_val = np.array([0, 0, 0, 1, 1, 2, 2], np.int32)
        pi_val = np.array([0.2, 0.3, 0.5], np.float32)
        mu_val = np.array([1.0, 5.0, 7.0], np.float32)

        N = x_val.shape[0]
        K = z_val.max() + 1

        pi_alpha = 1.3 + np.zeros(K, dtype=np.float32)
        mu_sigma = 4.0
        sigmasq = 2.0**2

        pi = rvs.Dirichlet(pi_alpha)
        mu = rvs.Normal(0.0, mu_sigma, sample_shape=[K])

        x = rvs.ParamMixture(pi, {
            'loc': mu,
            'scale': tf.sqrt(sigmasq)
        },
                             rvs.Normal,
                             sample_shape=N)
        z = x.cat

        mu_cond = ed.complete_conditional(mu)
        pi_cond = ed.complete_conditional(pi)
        z_cond = ed.complete_conditional(z)

        with self.test_session() as sess:
            pi_cond_alpha, mu_cond_mu, mu_cond_sigma, z_cond_p = (sess.run(
                [
                    pi_cond.concentration, mu_cond.loc, mu_cond.scale,
                    z_cond.probs
                ], {
                    z: z_val,
                    x: x_val,
                    pi: pi_val,
                    mu: mu_val
                }))

        true_pi = pi_alpha + np.unique(z_val, return_counts=True)[1]
        self.assertAllClose(pi_cond_alpha, true_pi)
        for k in range(K):
            sigmasq_true = (1.0 / 4**2 + 1.0 / sigmasq *
                            (z_val == k).sum())**-1
            mu_true = sigmasq_true * (1.0 / sigmasq * x_val[z_val == k].sum())
            self.assertAllClose(np.sqrt(sigmasq_true), mu_cond_sigma[k])
            self.assertAllClose(mu_true, mu_cond_mu[k])
        true_log_p_z = np.log(pi_val) - 0.5 / sigmasq * (x_val[:, np.newaxis] -
                                                         mu_val)**2
        true_log_p_z -= true_log_p_z.max(1, keepdims=True)
        true_p_z = np.exp(true_log_p_z)
        true_p_z /= true_p_z.sum(1, keepdims=True)
        self.assertAllClose(z_cond_p, true_p_z)
Example #2
0
  def test_blanket_changes(self):
    pi = rvs.Dirichlet(tf.ones(3))
    mu = rvs.Normal(0.0, 1.0)
    z = rvs.Categorical(p=pi)

    pi1_cond = ed.complete_conditional(pi, [z, pi])
    pi2_cond = ed.complete_conditional(pi, [z, mu, pi])

    self.assertIsInstance(pi1_cond, rvs.Dirichlet)
    self.assertIsInstance(pi2_cond, rvs.Dirichlet)

    with self.test_session() as sess:
      alpha1_val, alpha2_val = sess.run([pi1_cond.alpha, pi2_cond.alpha])

    self.assertAllClose(alpha1_val, alpha2_val)
Example #3
0
  def test_dirichlet_multinomial(self):
    x_data = np.array([4, 3, 2, 1], np.int32)
    N = x_data.sum()
    D = x_data.shape[0]

    alpha = np.zeros(D).astype(np.float32) + 2.0

    theta = rvs.Dirichlet(alpha)
    x = rvs.Multinomial(n=tf.cast(N, tf.float32), p=theta)

    theta_cond = ed.complete_conditional(theta, [theta, x])

    with self.test_session() as sess:
      alpha_val = sess.run(theta_cond.alpha, {x: x_data})

    self.assertAllClose(alpha_val, np.array([6.0, 5.0, 4.0, 3.0], np.float32))
Example #4
0
  def test_dirichlet_categorical(self):
    x_data = np.array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3], np.int32)
    N = x_data.shape[0]
    D = x_data.max() + 1

    alpha = np.zeros(D).astype(np.float32) + 2.0

    theta = rvs.Dirichlet(alpha)
    x = rvs.Categorical(p=theta, sample_shape=N)

    theta_cond = ed.complete_conditional(theta, [theta, x])

    with self.test_session() as sess:
      alpha_val = sess.run(theta_cond.alpha, {x: x_data})

    self.assertAllClose(alpha_val, np.array([6.0, 5.0, 4.0, 3.0], np.float32))
Example #5
0
import numpy as np
import tensorflow as tf
import edward as ed
import edward.models as edm

# node names in lexicographic (alphabetic) order
nd_names_lex_ord = ['Cloudy', 'Rain', 'Sprinkler', 'WetGrass']

# node names in topological (chronological) order
nd_names_topo_ord = ['Cloudy', 'Rain', 'Sprinkler', 'WetGrass']

with tf.name_scope('model'):
    Cloudy = tf.placeholder(tf.int32, shape=[sam_size], name="Cloudy")

    alpha_Rain = np.array([[1., 1.], [1., 1.]])
    probs_Rain = edm.Dirichlet(alpha_Rain.astype(np.float32),
                               name='probs_Rain')
    p_Rain = tf.stack([probs_Rain[Cloudy[j], :] for j in range(sam_size)])
    Rain = edm.Categorical(probs=p_Rain, name='Rain')

    alpha_Sprinkler = np.array([[1., 1.], [1., 1.]])
    probs_Sprinkler = edm.Dirichlet(alpha_Sprinkler.astype(np.float32),
                                    name='probs_Sprinkler')
    p_Sprinkler = tf.stack(
        [probs_Sprinkler[Cloudy[j], :] for j in range(sam_size)])
    Sprinkler = edm.Categorical(probs=p_Sprinkler, name='Sprinkler')

    arr_WetGrass = np.array([[[0.99, 0.01], [0.01, 0.99]],
                             [[0.01, 0.99], [0.01, 0.99]]])
    ten_WetGrass = tf.convert_to_tensor(arr_WetGrass, dtype=tf.float32)
    p_WetGrass = tf.stack(
        [ten_WetGrass[Sprinkler[j], Rain[j], :] for j in range(sam_size)])
Example #6
0
# dominant, most common state
def domi(rv):
    return tf.argmax(tf.bincount(rv))


with tf.name_scope('model'):
    Cloudy = tf.placeholder(tf.int32, shape=[sam_size], name="Cloudy")

    arr_Rain = np.array([[0.4, 0.6], [0.5, 0.5]])
    ten_Rain = tf.convert_to_tensor(arr_Rain, dtype=tf.float32)
    p_Rain = tf.stack([ten_Rain[Cloudy[j], :] for j in range(sam_size)])
    Rain = edm.Categorical(probs=p_Rain, name='Rain')

    alpha_Sprinkler = np.array([[1., 1.], [1., 1.]])
    probs_Sprinkler = edm.Dirichlet(alpha_Sprinkler.astype(np.float32),
                                    name='probs_Sprinkler')
    p_Sprinkler = probs_Sprinkler[domi(Cloudy), :]
    Sprinkler = edm.Categorical(probs=p_Sprinkler, name='Sprinkler')

    arr_WetGrass = np.array([[[0.99, 0.01], [0.01, 0.99]],
                             [[0.01, 0.99], [0.01, 0.99]]])
    ten_WetGrass = tf.convert_to_tensor(arr_WetGrass, dtype=tf.float32)
    p_WetGrass = tf.stack(
        [ten_WetGrass[Sprinkler, Rain[j], :] for j in range(sam_size)])
    WetGrass = edm.Categorical(probs=p_WetGrass, name='WetGrass')

with tf.name_scope('posterior'):
    # Cloudy = placeholder

    Rain_ph = tf.placeholder(tf.int32, shape=[sam_size], name="Rain_ph")