Skip to content

Instantly share code, notes, and snippets.

@sromano
Last active May 16, 2017 19:15
Show Gist options
  • Select an option

  • Save sromano/694532cf37c2c51e651be9272a4b0293 to your computer and use it in GitHub Desktop.

Select an option

Save sromano/694532cf37c2c51e651be9272a4b0293 to your computer and use it in GitHub Desktop.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import edward as ed
from edward.models import Bernoulli, Beta, Binomial
#Model
p = Beta(1.0, 1.0)
b = tf.constant("B")
condition = lambda i: tf.cast(Bernoulli(probs=p), tf.bool)
a = lambda next: tf.string_join([tf.constant("A"),next])
result = tf.while_loop(condition, a, [b])
shaped_result = tf.stack([result])
#Observations
data = b"AAAAAAAAAAAAAAAAAAB"
##Infer
qp_a = tf.Variable(1.0)
qp_b = tf.Variable(1.0)
qp = Beta(qp_a, qp_b)
sess = ed.get_session()
inference = ed.KLqp({p: qp}, {shaped_result: [data]})
inference.initialize()
tf.global_variables_initializer().run()
for _ in range(inference.n_iter):
info_dict = inference.update()
#inference.print_progress(info_dict)
inference.finalize()
#Results
qp_samples = qp.sample(1000)
mean = tf.reduce_mean(qp_samples)
print(sess.run(mean))
plt.hist(sess.run(qp_samples))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment