Beispiel #1
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)
Beispiel #2
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))
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'):
    arr_Cloudy = np.array([ 0.5,  0.5])
    ten_Cloudy = tf.convert_to_tensor(arr_Cloudy, dtype=tf.float32)
    p_Cloudy = ten_Cloudy[:]
    Cloudy = edm.Categorical(
        probs=p_Cloudy, 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 = ten_Rain[Cloudy, :]
    Rain = edm.Categorical(
        probs=p_Rain, name='Rain')

    arr_Sprinkler = np.array([[ 0.2,  0.8],
       [ 0.7,  0.3]])
    ten_Sprinkler = tf.convert_to_tensor(arr_Sprinkler, dtype=tf.float32)
    p_Sprinkler = ten_Sprinkler[Cloudy, :]
    Sprinkler = edm.Categorical(
        probs=p_Sprinkler, name='Sprinkler')
Beispiel #4
0
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)])
    WetGrass = edm.Categorical(probs=p_WetGrass, name='WetGrass')