def _check_elbo(x_mean, x_std): # check their elbo def log_joint(observed): norm = Normal(mean=x_mean, std=x_std) return norm.log_prob(observed['x']) lower_bound = elbo(log_joint, observed={}, latent={'x': [qx_samples, log_qx]}, axis=0) analytic_lower_bound = -_kl_normal_normal(0., 1., x_mean, x_std) with self.test_session(use_gpu=True) as sess: a = sess.run(lower_bound) b = sess.run(analytic_lower_bound) # print(a, b) self.assertNear(a, b, 1e-2)
def _check_monotonous_elbo(x_mean, x_std): # check their elbo def log_joint(observed): norm = Normal(mean=x_mean, std=x_std) return norm.log_prob(observed['x']) lower_bound = importance_weighted_objective( log_joint, observed={}, latent={'x': [qx_samples_n3, log_qx_n3]}, axis=0) lower_bound = tf.reduce_mean(lower_bound) analytic_lower_bound = -_kl_normal_normal(0., 1., x_mean, x_std) with self.session(use_gpu=True) as sess: a = sess.run(lower_bound) b = sess.run(analytic_lower_bound) self.assertTrue(a > b - 1e-6)
def _check_importance(x_mean, x_std, threshold): def log_joint(observed): p = Normal(mean=x_mean, std=x_std) return p.log_prob(observed['x']) klpq_obj = klpq(log_joint, observed={}, latent={'x': [qx_samples, log_qx]}, axis=0) cost = klpq_obj.importance() importance_grads = tf.gradients(cost, [mu, sigma]) true_cost = _kl_normal_normal(x_mean, x_std, mu, sigma) true_grads = tf.gradients(true_cost, [mu, sigma]) with self.session(use_gpu=True) as sess: g1 = sess.run(importance_grads) g2 = sess.run(true_grads) # print('importance_grads:', g1) # print('true_grads:', g2) self.assertAllClose(g1, g2, threshold, threshold)
def _check_sgvb(x_mean, x_std, threshold): def log_joint(observed): norm = Normal(mean=x_mean, std=x_std) return norm.log_prob(observed['x']) lower_bound = elbo(log_joint, observed={}, latent={'x': [qx_samples, log_qx]}, axis=0) sgvb_cost = lower_bound.sgvb() sgvb_grads = tf.gradients(sgvb_cost, [mu, sigma]) true_cost = _kl_normal_normal(mu, sigma, x_mean, x_std) true_grads = tf.gradients(true_cost, [mu, sigma]) with self.test_session(use_gpu=True) as sess: g1 = sess.run(sgvb_grads) g2 = sess.run(true_grads) # print('sgvb_grads:', g1) # print('true_grads:', g2) self.assertAllClose(g1, g2, threshold, threshold)
def _check_reinforce(x_mean, x_std, threshold): def log_joint(observed): norm = Normal(mean=x_mean, std=x_std) return norm.log_prob(observed['x']) lower_bound = elbo(log_joint, observed={}, latent={'x': [qx_samples, log_qx]}, axis=0) # TODO: Check grads when use variance reduction and baseline reinforce_cost = lower_bound.reinforce(variance_reduction=False) reinforce_grads = tf.gradients(reinforce_cost, [mu, sigma]) true_cost = _kl_normal_normal(mu, sigma, x_mean, x_std) true_grads = tf.gradients(true_cost, [mu, sigma]) with self.test_session(use_gpu=True) as sess: sess.run(tf.global_variables_initializer()) g1 = sess.run(reinforce_grads) g2 = sess.run(true_grads) # print('reinforce_grads:', g1) # print('true_grads:', g2) self.assertAllClose(g1, g2, threshold, threshold)