def main(_): ed.set_seed(42) # DATA x_data = build_toy_dataset(FLAGS.N) # MODEL pi = Dirichlet(concentration=tf.ones(FLAGS.K)) mu = Normal(0.0, 1.0, sample_shape=[FLAGS.K, FLAGS.D]) sigma = InverseGamma(concentration=1.0, rate=1.0, sample_shape=[FLAGS.K, FLAGS.D]) c = Categorical(logits=tf.log(pi) - tf.log(1.0 - pi), sample_shape=FLAGS.N) x = Normal(loc=tf.gather(mu, c), scale=tf.gather(sigma, c)) # INFERENCE qpi = Empirical(params=tf.get_variable( "qpi/params", [FLAGS.T, FLAGS.K], initializer=tf.constant_initializer(1.0 / FLAGS.K))) qmu = Empirical(params=tf.get_variable("qmu/params", [FLAGS.T, FLAGS.K, FLAGS.D], initializer=tf.zeros_initializer())) qsigma = Empirical(params=tf.get_variable("qsigma/params", [FLAGS.T, FLAGS.K, FLAGS.D], initializer=tf.ones_initializer())) qc = Empirical(params=tf.get_variable("qc/params", [FLAGS.T, FLAGS.N], initializer=tf.zeros_initializer(), dtype=tf.int32)) gpi = Dirichlet(concentration=tf.constant([1.4, 1.6])) gmu = Normal(loc=tf.constant([[1.0, 1.0], [-1.0, -1.0]]), scale=tf.constant([[0.5, 0.5], [0.5, 0.5]])) gsigma = InverseGamma(concentration=tf.constant([[1.1, 1.1], [1.1, 1.1]]), rate=tf.constant([[1.0, 1.0], [1.0, 1.0]])) gc = Categorical(logits=tf.zeros([FLAGS.N, FLAGS.K])) inference = ed.MetropolisHastings( latent_vars={pi: qpi, mu: qmu, sigma: qsigma, c: qc}, proposal_vars={pi: gpi, mu: gmu, sigma: gsigma, c: gc}, data={x: x_data}) inference.initialize() sess = ed.get_session() tf.global_variables_initializer().run() for _ in range(inference.n_iter): info_dict = inference.update() inference.print_progress(info_dict) t = info_dict['t'] if t == 1 or t % inference.n_print == 0: qpi_mean, qmu_mean = sess.run([qpi.mean(), qmu.mean()]) print("") print("Inferred membership probabilities:") print(qpi_mean) print("Inferred cluster means:") print(qmu_mean)
def _test_linear_regression(self, default, dtype): def build_toy_dataset(N, w, noise_std=0.1): D = len(w) x = np.random.randn(N, D) y = np.dot(x, w) + np.random.normal(0, noise_std, size=N) return x, y with self.test_session() as sess: N = 40 # number of data points D = 10 # number of features w_true = np.random.randn(D) X_train, y_train = build_toy_dataset(N, w_true) X_test, y_test = build_toy_dataset(N, w_true) X = tf.placeholder(dtype, [N, D]) w = Normal(loc=tf.zeros(D, dtype=dtype), scale=tf.ones(D, dtype=dtype)) b = Normal(loc=tf.zeros(1, dtype=dtype), scale=tf.ones(1, dtype=dtype)) y = Normal(loc=ed.dot(X, w) + b, scale=0.1 * tf.ones(N, dtype=dtype)) n_samples = 2000 if not default: qw = Empirical( tf.Variable(tf.zeros([n_samples, D], dtype=dtype))) qb = Empirical( tf.Variable(tf.zeros([n_samples, 1], dtype=dtype))) inference = ed.SGLD({ w: qw, b: qb }, data={ X: X_train, y: y_train }) else: inference = ed.SGLD([w, b], data={X: X_train, y: y_train}) qw = inference.latent_vars[w] qb = inference.latent_vars[b] inference.run(step_size=0.001) self.assertAllClose(qw.mean().eval(), w_true, rtol=5e-1, atol=5e-1) self.assertAllClose(qb.mean().eval(), [0.0], rtol=5e-1, atol=5e-1) old_t, old_n_accept = sess.run([inference.t, inference.n_accept]) if not default: self.assertEqual(old_t, n_samples) else: self.assertEqual(old_t, 1e4) self.assertGreater(old_n_accept, 0.1) sess.run(inference.reset) new_t, new_n_accept = sess.run([inference.t, inference.n_accept]) self.assertEqual(new_t, 0) self.assertEqual(new_n_accept, 0)
def _test_linear_regression(self, default, dtype): def build_toy_dataset(N, w, noise_std=0.1): D = len(w) x = np.random.randn(N, D) y = np.dot(x, w) + np.random.normal(0, noise_std, size=N) return x, y with self.test_session() as sess: N = 40 # number of data points D = 10 # number of features w_true = np.random.randn(D) X_train, y_train = build_toy_dataset(N, w_true) X_test, y_test = build_toy_dataset(N, w_true) X = tf.placeholder(dtype, [N, D]) w = Normal(loc=tf.zeros(D, dtype=dtype), scale=tf.ones(D, dtype=dtype)) b = Normal(loc=tf.zeros(1, dtype=dtype), scale=tf.ones(1, dtype=dtype)) y = Normal(loc=ed.dot(X, w) + b, scale=0.1 * tf.ones(N, dtype=dtype)) proposal_w = Normal(loc=w, scale=0.5 * tf.ones(D, dtype=dtype)) proposal_b = Normal(loc=b, scale=0.5 * tf.ones(1, dtype=dtype)) n_samples = 2000 if not default: qw = Empirical(tf.Variable(tf.zeros([n_samples, D], dtype=dtype))) qb = Empirical(tf.Variable(tf.zeros([n_samples, 1], dtype=dtype))) inference = ed.ReplicaExchangeMC( {w: qw, b: qb}, {w: proposal_w, b: proposal_b}, data={X: X_train, y: y_train}) else: inference = ed.ReplicaExchangeMC( [w, b], {w: proposal_w, b: proposal_b}, data={X: X_train, y: y_train}) qw = inference.latent_vars[w] qb = inference.latent_vars[b] inference.run() self.assertAllClose(qw.mean().eval(), w_true, rtol=5e-1, atol=5e-1) self.assertAllClose(qb.mean().eval(), [0.0], rtol=5e-1, atol=5e-1) old_t, old_n_accept = sess.run([inference.t, inference.n_accept]) if not default: self.assertEqual(old_t, n_samples) else: self.assertEqual(old_t, 1e4) self.assertGreater(old_n_accept, 0.1) sess.run(inference.reset) new_t, new_n_accept = sess.run([inference.t, inference.n_accept]) self.assertEqual(new_t, 0) self.assertEqual(new_n_accept, 0)
def main(_): ed.set_seed(42) # DATA x_data = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 1]) # MODEL p = Beta(1.0, 1.0) x = Bernoulli(probs=p, sample_shape=10) # INFERENCE qp = Empirical(params=tf.get_variable( "qp/params", [1000], initializer=tf.constant_initializer(0.5))) proposal_p = Beta(3.0, 9.0) inference = ed.MetropolisHastings({p: qp}, {p: proposal_p}, data={x: x_data}) inference.run() # CRITICISM # exact posterior has mean 0.25 and std 0.12 sess = ed.get_session() mean, stddev = sess.run([qp.mean(), qp.stddev()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior stddev:") print(stddev) x_post = ed.copy(x, {p: qp}) tx_rep, tx = ed.ppc( lambda xs, zs: tf.reduce_mean(tf.cast(xs[x_post], tf.float32)), data={x_post: x_data}) ed.ppc_stat_hist_plot( tx[0], tx_rep, stat_name=r'$T \equiv$mean', bins=10) plt.show()
def test_normalnormal_run(self): with self.test_session() as sess: x_data = np.array([0.0] * 50, dtype=np.float32) mu = Normal(loc=0.0, scale=1.0) x = Normal(loc=tf.ones(50) * mu, scale=1.0) n_samples = 2000 qmu = Empirical(params=tf.Variable(tf.ones(n_samples))) proposal_mu = Normal(loc=0.0, scale=1.0) # analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140) inference = ed.MetropolisHastings({mu: qmu}, {mu: proposal_mu}, data={x: x_data}) inference.run() self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-2, atol=1e-2) self.assertAllClose(qmu.stddev().eval(), np.sqrt(1 / 51), rtol=1e-1, atol=1e-1) old_t, old_n_accept = sess.run([inference.t, inference.n_accept]) self.assertEqual(old_t, n_samples) self.assertGreater(old_n_accept, 0.1) sess.run(inference.reset) new_t, new_n_accept = sess.run([inference.t, inference.n_accept]) self.assertEqual(new_t, 0) self.assertEqual(new_n_accept, 0)
def main(_): ed.set_seed(42) # MODEL z = MultivariateNormalTriL( loc=tf.ones(2), scale_tril=tf.cholesky(tf.constant([[1.0, 0.8], [0.8, 1.0]]))) # INFERENCE qz = Empirical(params=tf.get_variable("qz/params", [1000, 2])) inference = ed.HMC({z: qz}) inference.run() # CRITICISM sess = ed.get_session() mean, stddev = sess.run([qz.mean(), qz.stddev()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior stddev:") print(stddev) fig, ax = plt.subplots() trace = sess.run(qz.params) ax.scatter(trace[:, 0], trace[:, 1], marker=".") mvn_plot_contours(z, ax=ax) plt.show()
def _test_normal_normal(self, default, dtype): with self.test_session() as sess: x_data = np.array([0.0] * 50, dtype=np.float32) mu = Normal(loc=tf.constant(0.0, dtype=dtype), scale=tf.constant(1.0, dtype=dtype)) x = Normal(loc=mu, scale=tf.constant(1.0, dtype=dtype), sample_shape=50) n_samples = 2000 # analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140) if not default: qmu = Empirical(params=tf.Variable(tf.ones(n_samples, dtype=dtype))) inference = ed.HMC({mu: qmu}, data={x: x_data}) else: inference = ed.HMC([mu], data={x: x_data}) qmu = inference.latent_vars[mu] inference.run() self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-1, atol=1e-1) self.assertAllClose(qmu.stddev().eval(), np.sqrt(1 / 51), rtol=1e-1, atol=1e-1) old_t, old_n_accept = sess.run([inference.t, inference.n_accept]) if not default: self.assertEqual(old_t, n_samples) else: self.assertEqual(old_t, 1e4) self.assertGreater(old_n_accept, 0.1) sess.run(inference.reset) new_t, new_n_accept = sess.run([inference.t, inference.n_accept]) self.assertEqual(new_t, 0) self.assertEqual(new_n_accept, 0)
def test_normalnormal_float32(self): with self.test_session() as sess: x_data = np.array([0.0] * 50, dtype=np.float32) mu = Normal(loc=0.0, scale=1.0) x = Normal(loc=mu, scale=1.0, sample_shape=50) n_samples = 2000 qmu = Empirical(params=tf.Variable(tf.ones(n_samples))) # analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140) inference = ed.MetropolisHastings({mu: qmu}, {mu: mu}, data={x: x_data}) inference.run() self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-1, atol=1e-1) self.assertAllClose(qmu.stddev().eval(), np.sqrt(1 / 51), rtol=1e-1, atol=1e-1) old_t, old_n_accept = sess.run([inference.t, inference.n_accept]) self.assertEqual(old_t, n_samples) self.assertGreater(old_n_accept, 0.1) sess.run(inference.reset) new_t, new_n_accept = sess.run([inference.t, inference.n_accept]) self.assertEqual(new_t, 0) self.assertEqual(new_n_accept, 0)
def _test_normal_normal(self, default, dtype): with self.test_session() as sess: x_data = np.array([0.0] * 50, dtype=np.float32) mu = Normal(loc=tf.constant(0.0, dtype=dtype), scale=tf.constant(1.0, dtype=dtype)) x = Normal(loc=mu, scale=tf.constant(1.0, dtype=dtype), sample_shape=50) n_samples = 2000 # analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140) if not default: qmu = Empirical(params=tf.Variable(tf.ones(n_samples, dtype=dtype))) inference = ed.Gibbs({mu: qmu}, data={x: x_data}) else: inference = ed.Gibbs([mu], data={x: x_data}) qmu = inference.latent_vars[mu] inference.run() self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-1, atol=1e-1) self.assertAllClose(qmu.stddev().eval(), np.sqrt(1 / 51), rtol=1e-1, atol=1e-1) old_t, old_n_accept = sess.run([inference.t, inference.n_accept]) if not default: self.assertEqual(old_t, n_samples) else: self.assertEqual(old_t, 1e4) self.assertGreater(old_n_accept, 0.1) sess.run(inference.reset) new_t, new_n_accept = sess.run([inference.t, inference.n_accept]) self.assertEqual(new_t, 0) self.assertEqual(new_n_accept, 0)
def main(_): # DATA trait_true = np.random.normal(size=[FLAGS.nsubj, 1]) thresh_true = np.random.normal(size=[1, FLAGS.nitem]) X_data = np.random.binomial(1, expit(trait_true - thresh_true)) # MODEL trait = Normal(loc=0.0, scale=1.0, sample_shape=[FLAGS.nsubj, 1]) thresh = Normal(loc=0.0, scale=1.0, sample_shape=[1, FLAGS.nitem]) X = Bernoulli(logits=trait - thresh) # INFERENCE q_trait = Empirical(params=tf.get_variable("q_trait/params", [FLAGS.T, FLAGS.nsubj, 1])) q_thresh = Empirical(params=tf.get_variable("q_thresh/params", [FLAGS.T, 1, FLAGS.nitem])) inference = ed.HMC({trait: q_trait, thresh: q_thresh}, data={X: X_data}) inference.run(step_size=0.1) # Alternatively, use variational inference. # q_trait = Normal( # loc=tf.get_variable("q_trait/loc", [FLAGS.nsubj, 1]), # scale=tf.nn.softplus( # tf.get_variable("q_trait/scale", [FLAGS.nsubj, 1]))) # q_thresh = Normal( # loc=tf.get_variable("q_thresh/loc", [1, FLAGS.nitem]), # scale=tf.nn.softplus( # tf.get_variable("q_thresh/scale", [1, FLAGS.nitem]))) # inference = ed.KLqp({trait: q_trait, thresh: q_thresh}, data={X: X_data}) # inference.run(n_iter=2500, n_samples=10) # CRITICISM # Check that the inferred posterior mean captures the true traits. plt.scatter(trait_true, q_trait.mean().eval()) plt.show() print("MSE between true traits and inferred posterior mean:") print(np.mean(np.square(trait_true - q_trait.mean().eval())))
def test_data_tensor(self): with self.test_session() as sess: x_data = tf.zeros(50) mu = Normal(0.0, 1.0) x = Normal(mu, 1.0, sample_shape=50) qmu = Empirical(tf.Variable(tf.ones(1000))) # analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140) inference = ed.Gibbs({mu: qmu}, data={x: x_data}) inference.run() self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-2, atol=1e-2) self.assertAllClose(qmu.stddev().eval(), np.sqrt(1 / 51), rtol=1e-2, atol=1e-2)
def test_normalnormal_run(self): with self.test_session() as sess: x_data = np.array([0.0] * 50, dtype=np.float32) mu = Normal(loc=0.0, scale=1.0) x = Normal(loc=tf.ones(50) * mu, scale=1.0) qmu = Empirical(params=tf.Variable(tf.ones(2000))) # analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140) inference = ed.HMC({mu: qmu}, data={x: x_data}) inference.run() self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-2, atol=1e-2) self.assertAllClose(qmu.stddev().eval(), np.sqrt(1 / 51), rtol=1e-2, atol=1e-2)
def test_normalnormal_run(self): with self.test_session() as sess: x_data = np.array([0.0] * 50, dtype=np.float32) mu = Normal(mu=0.0, sigma=1.0) x = Normal(mu=tf.ones(50) * mu, sigma=1.0) qmu = Empirical(params=tf.Variable(tf.ones(5000))) # analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140) inference = ed.SGLD({mu: qmu}, data={x: x_data}) inference.run(step_size=0.2) self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-2, atol=1e-2) self.assertAllClose(qmu.std().eval(), np.sqrt(1 / 51), rtol=5e-2, atol=5e-2)
def test_normalnormal_float32(self): with self.test_session() as sess: x_data = np.array([0.0] * 50, dtype=np.float32) mu = Normal(loc=0.0, scale=1.0) x = Normal(loc=mu, scale=1.0, sample_shape=50) qmu = Empirical(params=tf.Variable(tf.ones(2000))) # analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140) inference = ed.HMC({mu: qmu}, data={x: x_data}) inference.run() self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-2, atol=1e-2) self.assertAllClose(qmu.stddev().eval(), np.sqrt(1 / 51), rtol=1e-2, atol=1e-2)
def test_beta_bernoulli(self): with self.test_session() as sess: x_data = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 1]) p = Beta(1.0, 1.0) x = Bernoulli(probs=p, sample_shape=10) qp = Empirical(tf.Variable(tf.zeros(1000))) inference = ed.Gibbs({p: qp}, data={x: x_data}) inference.run() true_posterior = Beta(3.0, 9.0) val_est, val_true = sess.run([qp.mean(), true_posterior.mean()]) self.assertAllClose(val_est, val_true, rtol=1e-2, atol=1e-2) val_est, val_true = sess.run([qp.variance(), true_posterior.variance()]) self.assertAllClose(val_est, val_true, rtol=1e-2, atol=1e-2)
def main(_): ed.set_seed(42) # MODEL z = MultivariateNormalTriL(loc=tf.ones(2), scale_tril=tf.cholesky( tf.constant([[1.0, 0.8], [0.8, 1.0]]))) # INFERENCE qz = Empirical(params=tf.get_variable("qz/params", [2000, 2])) inference = ed.SGLD({z: qz}) inference.run(step_size=5.0) # CRITICISM sess = ed.get_session() mean, stddev = sess.run([qz.mean(), qz.stddev()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior stddev:") print(stddev)
def main(_): ed.set_seed(42) # MODEL z = MultivariateNormalTriL( loc=tf.ones(2), scale_tril=tf.cholesky(tf.constant([[1.0, 0.8], [0.8, 1.0]]))) # INFERENCE qz = Empirical(params=tf.get_variable("qz/params", [2000, 2])) inference = ed.SGLD({z: qz}) inference.run(step_size=5.0) # CRITICISM sess = ed.get_session() mean, stddev = sess.run([qz.mean(), qz.stddev()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior stddev:") print(stddev)
def test_normalnormal_float32(self): with self.test_session() as sess: x_data = np.array([0.0] * 50, dtype=np.float32) mu = Normal(loc=tf.constant(0.0, dtype=tf.float64), scale=tf.constant(1.0, dtype=tf.float64)) x = Normal(loc=mu, scale=tf.constant(1.0, dtype=tf.float64), sample_shape=50) n_samples = 2000 qmu = Empirical(params=tf.Variable(tf.ones(n_samples, dtype=tf.float64))) # analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140) inference = ed.MetropolisHastings({mu: qmu}, {mu: mu}, data={x: x_data}) inference.run() self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-1, atol=1e-1) self.assertAllClose(qmu.stddev().eval(), np.sqrt(1 / 51), rtol=1e-1, atol=1e-1)
def main(_): ed.set_seed(42) # DATA x_data = np.array([0.0] * 50) # MODEL: Normal-Normal with known variance mu = Normal(loc=0.0, scale=1.0) x = Normal(loc=mu, scale=1.0, sample_shape=50) # INFERENCE qmu = Empirical(params=tf.get_variable("qmu/params", [1000], initializer=tf.zeros_initializer())) # analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140) inference = ed.HMC({mu: qmu}, data={x: x_data}) inference.run() # CRITICISM sess = ed.get_session() mean, stddev = sess.run([qmu.mean(), qmu.stddev()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior stddev:") print(stddev) # Check convergence with visual diagnostics. samples = sess.run(qmu.params) # Plot histogram. plt.hist(samples, bins='auto') plt.show() # Trace plot. plt.plot(samples) plt.show()
def main(_): # Data generation (known mean) xn_data = np.random.normal(FLAGS.loc, FLAGS.scale, FLAGS.N) print("scale: {}".format(FLAGS.scale)) # Prior definition alpha = 0.5 beta = 0.7 # Posterior inference # Probabilistic model ig = InverseGamma(alpha, beta) xn = Normal(FLAGS.loc, tf.sqrt(ig), sample_shape=FLAGS.N) # Inference qig = Empirical(params=tf.get_variable( "qig/params", [1000], initializer=tf.constant_initializer(0.5))) proposal_ig = InverseGamma(2.0, 2.0) inference = ed.MetropolisHastings({ig: qig}, {ig: proposal_ig}, data={xn: xn_data}) inference.run() sess = ed.get_session() print("Inferred scale: {}".format(sess.run(tf.sqrt(qig.mean()))))
x = Normal(loc=tf.ones(50) * mu, scale=1.0) # INFERENCE qmu = Empirical(params=tf.Variable(tf.zeros([1000]))) proposal_mu = Normal(loc=0.0, scale=tf.sqrt(1.0 / 51.0)) # analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140) inference = ed.MetropolisHastings({mu: qmu}, {mu: proposal_mu}, data={x: x_data}) inference.run() # CRITICISM # Check convergence with visual diagnostics. sess = ed.get_session() mean, stddev = sess.run([qmu.mean(), qmu.stddev()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior stddev:") print(stddev) # Check convergence with visual diagnostics. samples = sess.run(qmu.params) # Plot histogram. plt.hist(samples, bins='auto') plt.show() # Trace plot. plt.plot(samples) plt.show()
import numpy as np import tensorflow as tf from edward.models import Bernoulli, Beta, Empirical ed.set_seed(42) # DATA x_data = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 1]) # MODEL p = Beta(1.0, 1.0) x = Bernoulli(probs=p, sample_shape=10) # INFERENCE qp = Empirical(params=tf.Variable(tf.zeros([1000]) + 0.5)) proposal_p = Beta(3.0, 9.0) inference = ed.MetropolisHastings({p: qp}, {p: proposal_p}, data={x: x_data}) inference.run() # CRITICISM # exact posterior has mean 0.25 and std 0.12 sess = ed.get_session() mean, stddev = sess.run([qp.mean(), qp.stddev()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior stddev:") print(stddev)
def main(_): ed.set_seed(42) # DATA x_data = build_toy_dataset(FLAGS.N) # MODEL pi = Dirichlet(concentration=tf.ones(FLAGS.K)) mu = Normal(0.0, 1.0, sample_shape=[FLAGS.K, FLAGS.D]) sigma = InverseGamma(concentration=1.0, rate=1.0, sample_shape=[FLAGS.K, FLAGS.D]) c = Categorical(logits=tf.log(pi) - tf.log(1.0 - pi), sample_shape=FLAGS.N) x = Normal(loc=tf.gather(mu, c), scale=tf.gather(sigma, c)) # INFERENCE qpi = Empirical( params=tf.get_variable("qpi/params", [FLAGS.T, FLAGS.K], initializer=tf.constant_initializer(1.0 / FLAGS.K))) qmu = Empirical( params=tf.get_variable("qmu/params", [FLAGS.T, FLAGS.K, FLAGS.D], initializer=tf.zeros_initializer())) qsigma = Empirical( params=tf.get_variable("qsigma/params", [FLAGS.T, FLAGS.K, FLAGS.D], initializer=tf.ones_initializer())) qc = Empirical(params=tf.get_variable("qc/params", [FLAGS.T, FLAGS.N], initializer=tf.zeros_initializer(), dtype=tf.int32)) gpi = Dirichlet(concentration=tf.constant([1.4, 1.6])) gmu = Normal(loc=tf.constant([[1.0, 1.0], [-1.0, -1.0]]), scale=tf.constant([[0.5, 0.5], [0.5, 0.5]])) gsigma = InverseGamma(concentration=tf.constant([[1.1, 1.1], [1.1, 1.1]]), rate=tf.constant([[1.0, 1.0], [1.0, 1.0]])) gc = Categorical(logits=tf.zeros([FLAGS.N, FLAGS.K])) inference = ed.MetropolisHastings(latent_vars={ pi: qpi, mu: qmu, sigma: qsigma, c: qc }, proposal_vars={ pi: gpi, mu: gmu, sigma: gsigma, c: gc }, data={x: x_data}) inference.initialize() sess = ed.get_session() tf.global_variables_initializer().run() for _ in range(inference.n_iter): info_dict = inference.update() inference.print_progress(info_dict) t = info_dict['t'] if t == 1 or t % inference.n_print == 0: qpi_mean, qmu_mean = sess.run([qpi.mean(), qmu.mean()]) print("") print("Inferred membership probabilities:") print(qpi_mean) print("Inferred cluster means:") print(qmu_mean)
def bayes_mult_cmd(table_file, metadata_file, formula, output_file): #metadata = _type_cast_to_float(metadata.copy()) metadata = pd.read_table(metadata_file, index_col=0) G_data = dmatrix(formula, metadata, return_type='dataframe') table = load_table(table_file) # basic filtering parameters soil_filter = lambda val, id_, md: id_ in metadata.index read_filter = lambda val, id_, md: np.sum(val) > 10 #sparse_filter = lambda val, id_, md: np.mean(val > 0) > 0.1 sample_filter = lambda val, id_, md: np.sum(val) > 1000 table = table.filter(soil_filter, axis='sample') table = table.filter(sample_filter, axis='sample') table = table.filter(read_filter, axis='observation') #table = table.filter(sparse_filter, axis='observation') print(table.shape) y_data = pd.DataFrame(np.array(table.matrix_data.todense()).T, index=table.ids(axis='sample'), columns=table.ids(axis='observation')) y_data, G_data = y_data.align(G_data, axis=0, join='inner') psi = _gram_schmidt_basis(y_data.shape[1]) G_data = G_data.values y_data = y_data.values N, D = y_data.shape p = G_data.shape[1] # number of covariates r = G_data.shape[1] # rank of covariance matrix psi = tf.convert_to_tensor(psi, dtype=tf.float32) n = tf.convert_to_tensor(y_data.sum(axis=1), dtype=tf.float32) # hack to get multinomial working def _sample_n(self, n=1, seed=None): # define Python function which returns samples as a Numpy array def np_sample(p, n): return multinomial.rvs(p=p, n=n, random_state=seed).astype(np.float32) # wrap python function as tensorflow op val = tf.py_func(np_sample, [self.probs, n], [tf.float32])[0] # set shape from unknown shape batch_event_shape = self.batch_shape.concatenate(self.event_shape) shape = tf.concat( [tf.expand_dims(n, 0), tf.convert_to_tensor(batch_event_shape)], 0) val = tf.reshape(val, shape) return val Multinomial._sample_n = _sample_n # dummy variable for gradient G = tf.placeholder(tf.float32, [N, p]) b = Exponential(rate=1.0) B = Normal(loc=tf.zeros([p, D-1]), scale=tf.ones([p, D-1]) ) # Factorization of covariance matrix # http://edwardlib.org/tutorials/klqp l = Exponential(rate=1.0) L = Normal(loc=tf.zeros([p, D-1]), scale=tf.ones([p, D-1]) ) z = Normal(loc=tf.zeros([N, p]), scale=tf.ones([N, p])) # Cholesky trick to get multivariate normal v = tf.matmul(G, B) + tf.matmul(z, L) # get clr transformed values eta = tf.matmul(v, psi) Y = Multinomial(total_count=n, logits=eta) T = 100000 # the number of mixin samples from MCMC sampling qb = PointMass(params=tf.Variable(tf.random_normal([]))) qB = PointMass(params=tf.Variable(tf.random_normal([p, D-1]))) qz = Empirical(params=tf.Variable(tf.random_normal([T, N, p]))) ql = PointMass(params=tf.Variable(tf.random_normal([]))) qL = PointMass(params=tf.Variable(tf.random_normal([p, D-1]))) # Imputation inference_z = ed.SGLD( {z: qz}, data={G: G_data, Y: y_data, B: qB, L: qL} ) # Maximization inference_BL = ed.MAP( {B: qB, L: qL, b: qb, l: ql}, data={G: G_data, Y: y_data, z: qz} ) inference_z.initialize(step_size=1e-10) inference_BL.initialize(n_iter=1000) sess = ed.get_session() saver = tf.train.Saver() tf.global_variables_initializer().run() for i in range(inference_BL.n_iter): inference_z.update() # e-step # will need to compute the expectation of z info_dict = inference_BL.update() # m-step inference_BL.print_progress(info_dict) save_path = saver.save(sess, output_file) print("Model saved in file: %s" % save_path) pickle.dump({'qB': sess.run(qB.mean()), 'qL': sess.run(qL.mean()), 'qz': sess.run(qz.mean())}, open(output_file + '.params.pickle', 'wb') )
from edward.models import Bernoulli, Normal, Empirical from scipy.special import expit # DATA nsubj = 200 nitem = 25 trait_true = np.random.normal(size=[nsubj, 1]) thresh_true = np.random.normal(size=[1, nitem]) X_data = np.random.binomial(1, expit(trait_true - thresh_true)) # MODEL trait = Normal(loc=tf.zeros([nsubj, 1]), scale=tf.ones([nsubj, 1])) thresh = Normal(loc=tf.zeros([1, nitem]), scale=tf.ones([1, nitem])) X = Bernoulli(logits=trait - thresh) # INFERENCE T = 5000 # number of posterior samples q_trait = Empirical(params=tf.Variable(tf.zeros([T, nsubj, 1]))) q_thresh = Empirical(params=tf.Variable(tf.zeros([T, 1, nitem]))) inference = ed.HMC({trait: q_trait, thresh: q_thresh}, data={X: X_data}) inference.run(step_size=0.1) # CRITICISM # Check that the inferred posterior mean captures the true traits. plt.scatter(trait_true, q_trait.mean().eval()) plt.show() print("MSE between true traits and inferred posterior mean:") print(np.mean(np.square(trait_true - q_trait.mean().eval())))
import numpy as np import tensorflow as tf from edward.models import Bernoulli, Beta, Empirical ed.set_seed(42) # DATA x_data = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 1]) # MODEL p = Beta(1.0, 1.0) x = Bernoulli(tf.ones(10) * p) # INFERENCE qp = Empirical(params=tf.Variable(tf.zeros([1000]) + 0.5)) proposal_p = Beta(3.0, 9.0) inference = ed.MetropolisHastings({p: qp}, {p: proposal_p}, data={x: x_data}) inference.run() # CRITICISM # exact posterior has mean 0.25 and std 0.12 sess = ed.get_session() mean, stddev = sess.run([qp.mean(), qp.stddev()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior stddev:") print(stddev)
import tensorflow as tf from edward.models import Bernoulli, Beta, Empirical ed.set_seed(42) # DATA x_data = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 1]) # MODEL p = Beta(a=1.0, b=1.0) x = Bernoulli(p=tf.ones(10) * p) # INFERENCE qp = Empirical(params=tf.Variable(tf.zeros([1000]) + 0.5)) proposal_p = Beta(a=3.0, b=9.0) data = {x: x_data} inference = ed.MetropolisHastings({p: qp}, {p: proposal_p}, data) inference.run() # CRITICISM # exact posterior has mean 0.25 and std 0.12 sess = ed.get_session() mean, std = sess.run([qp.mean(), qp.std()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior std:") print(std)
import tensorflow as tf from edward.models import InverseGamma, Normal, Empirical N = 1000 # Data generation (known mean) mu = 7.0 sigma = 0.7 xn_data = np.random.normal(mu, sigma, N) print('sigma={}'.format(sigma)) # Prior definition alpha = tf.Variable(0.5, dtype=tf.float32, trainable=False) beta = tf.Variable(0.7, dtype=tf.float32, trainable=False) # Posterior inference # Probabilistic model ig = InverseGamma(alpha=alpha, beta=beta) xn = Normal(mu=mu, sigma=tf.ones([N]) * tf.sqrt(ig)) # Inference qig = Empirical(params=tf.Variable(tf.zeros(1000) + 0.5)) proposal_ig = InverseGamma(alpha=2.0, beta=2.0) inference = ed.MetropolisHastings({ig: qig}, {ig: proposal_ig}, data={xn: xn_data}) inference.run() sess = ed.get_session() print('Inferred sigma={}'.format(sess.run(tf.sqrt(qig.mean()))))
""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import edward as ed import tensorflow as tf from edward.models import Empirical, MultivariateNormalFull ed.set_seed(42) # MODEL z = MultivariateNormalFull( mu=tf.ones(2), sigma=tf.constant([[1.0, 0.8], [0.8, 1.0]])) # INFERENCE qz = Empirical(params=tf.Variable(tf.random_normal([2000, 2]))) inference = ed.SGLD({z: qz}) inference.run(step_size=5.0) # CRITICISM sess = ed.get_session() mean, std = sess.run([qz.mean(), qz.std()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior std:") print(std)
pi: qpi, mu: qmu, sigma: qsigma, c: qc }, proposal_vars={ pi: gpi, mu: gmu, sigma: gsigma, c: gc }, data={x: x_data}) inference.initialize() sess = ed.get_session() tf.global_variables_initializer().run() for _ in range(inference.n_iter): info_dict = inference.update() inference.print_progress(info_dict) t = info_dict['t'] if t == 1 or t % inference.n_print == 0: qpi_mean, qmu_mean = sess.run([qpi.mean(), qmu.mean()]) print("") print("Inferred membership probabilities:") print(qpi_mean) print("Inferred cluster means:") print(qmu_mean)
import numpy as np import tensorflow as tf from edward.models import Bernoulli, Empirical, Normal from scipy.special import expit ed.set_seed(123) N = 5810 # number of data points D = 54 # number of features # DATA w_true = np.random.randn(D) X_data = np.random.randn(N, D) p = expit(np.dot(X_data, w_true)) y_data = np.array([np.random.binomial(1, i) for i in p]) # MODEL X = tf.Variable(X_data.astype(np.float32), trainable=False) w = Normal(mu=tf.zeros(D), sigma=tf.ones(D)) y = Bernoulli(logits=ed.dot(X, w)) # INFERENCE T = 5000 qw = Empirical(params=tf.Variable(tf.zeros([T, D]))) inference = ed.HMC({w: qw}, data={y: y_data}) inference.run(step_size=0.05) # CRITICISM print("Mean squared error in true values to inferred posterior mean:") print(tf.reduce_mean(tf.square(w_true - qw.mean())).eval())
import tensorflow as tf from edward.models import InverseGamma, Normal, Empirical N = 1000 # Data generation (known mean) loc = 7.0 scale = 0.7 xn_data = np.random.normal(loc, scale, N) print('sigma={}'.format(sigma)) # Prior definition alpha = tf.Variable(0.5, trainable=False) beta = tf.Variable(0.7, trainable=False) # Posterior inference # Probabilistic model ig = InverseGamma(alpha, beta) xn = Normal(loc, tf.ones([N]) * tf.sqrt(ig)) # Inference qig = Empirical(params=tf.Variable(tf.zeros(1000) + 0.5)) proposal_ig = InverseGamma(2.0, 2.0) inference = ed.MetropolisHastings({ig: qig}, {ig: proposal_ig}, data={xn: xn_data}) inference.run() sess = ed.get_session() print('Inferred sigma={}'.format(sess.run(tf.sqrt(qig.mean()))))
x = Normal(mu=tf.ones(50) * mu, sigma=1.0) # INFERENCE qmu = Empirical(params=tf.Variable(tf.zeros([1000]))) proposal_mu = Normal(mu=0.0, sigma=tf.sqrt(1.0 / 51.0)) # analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140) inference = ed.MetropolisHastings({mu: qmu}, {mu: proposal_mu}, data={x: x_data}) inference.run() # CRITICISM # Check convergence with visual diagnostics. sess = ed.get_session() mean, std = sess.run([qmu.mean(), qmu.std()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior std:") print(std) # Check convergence with visual diagnostics. samples = sess.run(qmu.params) # Plot histogram. plt.hist(samples, bins='auto') plt.show() # Trace plot. plt.plot(samples) plt.show()
from edward.models import Bernoulli, Normal, Empirical from scipy.special import expit # DATA nsubj = 200 nitem = 25 trait_true = np.random.normal(size=[nsubj, 1]) thresh_true = np.random.normal(size=[1, nitem]) X_data = np.random.binomial(1, expit(trait_true - thresh_true)) # MODEL trait = Normal(mu=tf.zeros([nsubj, 1]), sigma=tf.ones([nsubj, 1])) thresh = Normal(mu=tf.zeros([1, nitem]), sigma=tf.ones([1, nitem])) X = Bernoulli(logits=tf.sub(trait, thresh)) # INFERENCE T = 5000 # number of posterior samples q_trait = Empirical(params=tf.Variable(tf.zeros([T, nsubj, 1]))) q_thresh = Empirical(params=tf.Variable(tf.zeros([T, 1, nitem]))) inference = ed.HMC({trait: q_trait, thresh: q_thresh}, data={X: X_data}) inference.run(step_size=0.1) # CRITICISM # Check that the inferred posterior mean captures the true traits. plt.scatter(trait_true, q_trait.mean().eval()) plt.show() print("MSE between true traits and inferred posterior mean:") print(np.mean(np.square(trait_true - q_trait.mean().eval())))
""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import edward as ed import tensorflow as tf from edward.models import Empirical, MultivariateNormalTriL ed.set_seed(42) # MODEL z = MultivariateNormalTriL( loc=tf.ones(2), scale_tril=tf.cholesky(tf.constant([[1.0, 0.8], [0.8, 1.0]]))) # INFERENCE qz = Empirical(params=tf.Variable(tf.random_normal([2000, 2]))) inference = ed.SGLD({z: qz}) inference.run(step_size=5.0) # CRITICISM sess = ed.get_session() mean, stddev = sess.run([qz.mean(), qz.stddev()]) print("Inferred posterior mean:") print(mean) print("Inferred posterior stddev:") print(stddev)
qw = Empirical(params=tf.Variable(tf.random_normal([T, D]))) qb = Empirical(params=tf.Variable(tf.random_normal([T, 1]))) inference = ed.SGHMC({w: qw, b: qb}, data={X: X_train, y: y_train}) inference.run(step_size=1e-3) # CRITICISM # Plot posterior samples. sns.jointplot(qb.params.eval()[nburn:T:stride], qw.params.eval()[nburn:T:stride]) plt.show() # Posterior predictive checks. y_post = ed.copy(y, {w: qw.mean(), b: qb.mean()}) # This is equivalent to # y_post = Normal(mu=ed.dot(X, qw.mean()) + qb.mean(), sigma=tf.ones(N)) print("Mean squared error on test data:") print(ed.evaluate('mean_squared_error', data={X: X_test, y_post: y_test})) print("Displaying prior predictive samples.") n_prior_samples = 10 w_prior = w.sample(n_prior_samples).eval() b_prior = b.sample(n_prior_samples).eval() plt.scatter(X_train, y_train) inputs = np.linspace(-1, 10, num=400, dtype=np.float32)
inference = ed.MetropolisHastings(latent_vars={ pi: qpi, mu: qmu, sigma: qsigma, c: qc }, proposal_vars={ pi: gpi, mu: gmu, sigma: gsigma, c: gc }, data={x: x_data}) inference.initialize() sess = ed.get_session() init = tf.initialize_all_variables() init.run() for _ in range(T): info_dict = inference.update() t = info_dict['t'] if t == 1 or t % inference.n_print == 0: accept_rate = info_dict['accept_rate'] print("iter {:d} accept rate {:.2f}".format(t, accept_rate)) print("Inferred membership probabilities:") print(sess.run(qpi.mean())) print("Inferred cluster means:") print(sess.run(qmu.mean()))