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_sgld(self): with self.test_session(): N, D, W_1, W_2, W_3, b_1, b_2, X, y, X_train, y_train = self._test( ) T = 1 # number of MCMC samples qW_1 = Empirical(params=tf.Variable(tf.random_normal([T, D, 20]))) qW_2 = Empirical(params=tf.Variable(tf.random_normal([T, 20, 15]))) qW_3 = Empirical(params=tf.Variable(tf.random_normal([T, 15, 1]))) qb_1 = Empirical(params=tf.Variable(tf.random_normal([T, 20]))) qb_2 = Empirical(params=tf.Variable(tf.random_normal([T, 15]))) inference = ed.SGLD( { W_1: qW_1, b_1: qb_1, W_2: qW_2, b_2: qb_2, W_3: qW_3 }, data={ y: y_train, X: X_train }) inference.run()
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.SGLD({mu: qmu}, data={x: x_data}) else: inference = ed.SGLD([mu], data={x: x_data}) qmu = inference.latent_vars[mu] inference.run(step_size=0.10) 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_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 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_float64(self): with self.test_session() as sess: x_data = np.array([0.0] * 50, dtype=np.float64) 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) qmu = Empirical( params=tf.Variable(tf.ones(5000, dtype=tf.float64))) # analytic solution: N(loc=0.0, scale=\sqrt{1/51}=0.140) inference = ed.SGLD({mu: qmu}, data={x: x_data}) inference.run(step_size=0.10) 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)
""" 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)
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') )
def test_monte_carlo(self): tf.InteractiveSession() ed.set_seed(42) # DATA X_train = np.zeros([500, 100]) y_train = np.zeros(500) N = X_train.shape[0] # data points D = X_train.shape[1] # feature T = 1 # number of MCMC samples # MODEL W_1 = Normal(mu=tf.zeros([D, 20]), sigma=tf.ones([D, 20]) * 100) W_2 = Normal(mu=tf.zeros([20, 15]), sigma=tf.ones([20, 15]) * 100) W_3 = Normal(mu=tf.zeros([15, 1]), sigma=tf.ones([15, 1]) * 100) b_1 = Normal(mu=tf.zeros(20), sigma=tf.ones(20) * 100) b_2 = Normal(mu=tf.zeros(15), sigma=tf.ones(15) * 100) x_ph = tf.placeholder(tf.float32, [N, D]) y = Bernoulli(logits=four_layer_nn(x_ph, W_1, W_2, W_3, b_1, b_2)) # INFERENCE qW_1 = Empirical(params=tf.Variable(tf.random_normal([T, D, 20]))) qW_2 = Empirical(params=tf.Variable(tf.random_normal([T, 20, 15]))) qW_3 = Empirical(params=tf.Variable(tf.random_normal([T, 15, 1]))) qb_1 = Empirical(params=tf.Variable(tf.random_normal([T, 20]))) qb_2 = Empirical(params=tf.Variable(tf.random_normal([T, 15]))) # note ideally these would be separate test methods; there's an # issue with the tensorflow graph when re-running the above # unfortunately inference = ed.HMC( { W_1: qW_1, b_1: qb_1, W_2: qW_2, b_2: qb_2, W_3: qW_3 }, data={ y: y_train, x_ph: X_train }) inference.run() inference = ed.SGLD( { W_1: qW_1, b_1: qb_1, W_2: qW_2, b_2: qb_2, W_3: qW_3 }, data={ y: y_train, x_ph: X_train }) inference.run() inference = ed.MetropolisHastings( { W_1: qW_1, b_1: qb_1, W_2: qW_2, b_2: qb_2, W_3: qW_3 }, { W_1: W_1, b_1: b_1, W_2: W_2, b_2: b_2, W_3: W_3 }, data={ y: y_train, x_ph: X_train }) inference.run()